file_detail.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. $(document).ready(function() {
  2. autoFlashHeight();
  3. $('#filing').height($(".sjs-height-0").height() - $('#add-slibing').parent().parent().height() - 10);
  4. class FilingObj {
  5. constructor(setting) {
  6. // 原始数据整理后的树结构,用来整理zTree显示
  7. this.dragTree = createDragTree({
  8. id: 'id',
  9. pid: 'tree_pid',
  10. level: 'tree_level',
  11. order: 'tree_order',
  12. rootId: '-1'
  13. });
  14. // 界面显示的zTree
  15. this.setting = setting;
  16. this.filingTree = null;
  17. this.pageCount = 15;
  18. this.expandKey = 'filing-' + window.location.pathname.split('/')[2];
  19. const cache = getLocalCache(this.expandKey);
  20. this.expandCache = cache ? _.uniq(cache.split(',')) : [];
  21. this.curFilingKey = 'cur-filing-' + window.location.pathname.split('/')[2];
  22. }
  23. calcTotalFileCount() {
  24. this.dragTree.recursiveFun(this.dragTree.children, x => {
  25. if (x.children && x.children.length > 0) {
  26. x.total_file_count = x.children.map(y => {
  27. return y.total_file_count;
  28. }).reduce((pre, value) => {
  29. return pre + value
  30. }, 0);
  31. } else {
  32. x.total_file_count = x.file_count;
  33. }
  34. });
  35. }
  36. _loadFilingSourceNode() {
  37. const self = this;
  38. const loadChildren = function(children) {
  39. for (const child of children) {
  40. if (child.children && child.children.length > 0) loadChildren(child.children);
  41. child.source_node = self.dragTree.getItems(child.id);
  42. }
  43. };
  44. const nodes = this.filingTree.getNodes();
  45. loadChildren(nodes);
  46. }
  47. loadFiling() {
  48. const self = this;
  49. if (this.filingTree) $.fn.zTree.destroy(this.setting.treeId);
  50. const sortNodes = this.dragTree.nodes.map(x => {
  51. const result = {
  52. id: x.id,
  53. tree_pid: x.tree_pid,
  54. name: x.name + (x.total_file_count > 0 ? `(${x.total_file_count})` : ''),
  55. spid: x.spid,
  56. };
  57. if (x.is_fixed) result.isParent = true;
  58. if (x.is_folder || x.is_fixed) result.open = self.expandCache.indexOf(result.id) >= 0;
  59. return result;
  60. });
  61. this.filingTree = $.fn.zTree.init($('#filing'), this.setting, sortNodes);
  62. this._loadFilingSourceNode();
  63. const curCache = getLocalCache(this.curFilingKey);
  64. const curNode = curCache ? this.filingTree.getNodeByParam('id', curCache) : null;
  65. if (curNode){
  66. this.filingTree.selectNode(curNode);
  67. filingObj.setCurFiling(curNode);
  68. }
  69. }
  70. analysisFiling(data) {
  71. this.dragTree.loadDatas(data);
  72. this.calcTotalFileCount();
  73. this.loadFiling();
  74. }
  75. _getFileNameHtml(file) {
  76. const editHtml = file.canEdit ? `<a href="javascript: void(0);" class="mr-1" name="edit-file" fid="${file.id}"><i class="fa fa-pencil fa-fw"></i></a>` : '';
  77. const viewHtml = file.viewpath ? `<a href="${file.viewpath}" class="mr-1" target="_blank"><i class="fa fa-eye fa-fw"></i></a>` : '';
  78. const downHtml = `<a href="javascript: void(0);" onclick="AliOss.downloadFile('${file.filepath}', '${file.filename + file.fileext}')" class="mr-1"><i class="fa fa-download fa-fw"></i></a>`;
  79. const delHtml = file.canEdit ? `<a href="javascript: void(0);" class="mr-1 text-danger" name="del-file" fid="${file.id}"><i class="fa fa-trash-o fa-fw"></i></a>` : '';
  80. return `<div class="d-flex justify-content-between align-items-center table-file"><div name="filename" fid="${file.id}">${file.filename}${file.fileext}</div><div class="btn-group-table" style="display: none;">${editHtml}${viewHtml}${downHtml}${delHtml}</div></div>`;
  81. }
  82. _getEditFileNameHtml(file) {
  83. const inputHtml = `<input type="text" class="form-control form-control-sm form-control-width" maxlength="100" value="${file.filename + file.fileext}" fid="${file.id}">`;
  84. const btnHtml = `<div class="btn-group-table" style="display: none;"><a href="javascript: void(0)" class="mr-1" name="edit-file-ok"><i class="fa fa-check fa-fw"></i></a><a href="javascript: void(0)" class="mr-1" name="edit-file-cancel"><i class="fa fa-remove fa-fw"></i></a></div>`;
  85. return `<div class="d-flex justify-content-between align-items-center table-file"><div>${inputHtml}</div>${btnHtml}</div>`;
  86. }
  87. _getFileHtml(file) {
  88. const html = [];
  89. html.push(`<tr fid="${file.id}">`);
  90. html.push(`<td class="text-center"><input type="checkbox" name="bd-check" fid="${file.id}"></td>`);
  91. html.push(`<td fid="${file.id}">${this._getFileNameHtml(file)}</td>`);
  92. html.push(`<td class="text-center">${file.user_name}</td>`);
  93. html.push(`<td class="text-center">${moment(file.create_time).format('YYYY-MM-DD HH:mm:ss')}</td>`);
  94. html.push(`<td class="text-center">${file.fileext_str}</td>`);
  95. html.push('</tr>');
  96. return html.join('');
  97. }
  98. refreshFilesTable() {
  99. const html = [];
  100. const files = this.curFiling.source_node.files;
  101. if (!files || files.length === 0) {
  102. $('#file-list').html('');
  103. return;
  104. }
  105. const startIndex = (this.curPage - 1)*this.pageCount;
  106. const endIndex = this.curPage*this.pageCount;
  107. for (const [i, f] of files.entries()) {
  108. if (i < startIndex || i >= endIndex) continue;
  109. html.push(this._getFileHtml(f));
  110. }
  111. $('#file-list').html(html.join(''));
  112. }
  113. refreshPages() {
  114. if (!filingObj.curFiling) return;
  115. filingObj.curTotalPage = Math.ceil(filingObj.curFiling.source_node.file_count / this.pageCount);
  116. $('#curPage').html(filingObj.curPage);
  117. $('#curTotalPage').html(filingObj.curTotalPage);
  118. if (filingObj.curTotalPage > 1) {
  119. $('#showPage').show();
  120. } else {
  121. $('#showPage').hide();
  122. }
  123. }
  124. async loadFiles(node, page) {
  125. if (node.source_node.children && node.source_node.children.length > 0) return;
  126. if (!node.source_node.files) node.source_node.files = [];
  127. if (!node.source_node.file_count) return;
  128. if (node.source_node.files && node.source_node.files.length === node.source_node.file_count) return;
  129. const needFiles = Math.min(page*this.pageCount, node.source_node.file_count);
  130. if (node.source_node.files && needFiles <= node.source_node.files.length) return;
  131. const files = await postDataAsync('file/load', { filing_id: node.id, page, count: this.pageCount });
  132. files.forEach(x => {
  133. const file = node.source_node.files.find(f => {return x.id === f.id; });
  134. if (file) {
  135. Object.assign(file, x);
  136. } else {
  137. node.source_node.files.push(x);
  138. }
  139. });
  140. node.source_node.files.sort((x, y) => {
  141. return x.create_time - y.create_time;
  142. });
  143. }
  144. addSiblingFiling(node) {
  145. const self = this;
  146. postData('filing/add', { tree_pid: node.tree_pid, tree_pre_id: node.id }, function(result) {
  147. const refreshData = self.dragTree.loadPostData(result);
  148. const newNode = refreshData.create[0];
  149. const nodes = self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid }]);
  150. nodes[0].source_node = newNode;
  151. });
  152. }
  153. addChildFiling(node) {
  154. const self = this;
  155. postData('filing/add', { tree_pid: node.id }, function(result) {
  156. const refreshData = self.dragTree.loadPostData(result);
  157. const newNode = refreshData.create[0];
  158. const nodes = self.filingTree.addNodes(node, -1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid}]);
  159. nodes[0].source_node = newNode;
  160. });
  161. }
  162. delFiling(node, callback) {
  163. const self = this;
  164. postData('filing/del', { id: node.id }, function(result) {
  165. self.dragTree.loadPostData(result);
  166. self.filingTree.removeNode(node);
  167. if (callback) callback();
  168. });
  169. }
  170. async renameFiling(node, newName) {
  171. const result = await postDataAsync('filing/save', { id: node.id, name: newName });
  172. node.source_node.name = newName;
  173. node.name = node.source_node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
  174. return result;
  175. }
  176. updateFilingFileCount(filing, count) {
  177. let differ = count - filing.source_node.file_count;
  178. filing.source_node.file_count = count;
  179. filing.source_node.total_file_count = count;
  180. filing.name = filing.source_node.name + (filing.source_node.total_file_count > 0 ? `(${filing.source_node.total_file_count})` : '');
  181. filingObj.filingTree.updateNode(filing);
  182. let parent = filing.getParentNode();
  183. while (!!parent) {
  184. parent.source_node.total_file_count = parent.source_node.total_file_count + differ;
  185. parent.name = parent.source_node.name + (parent.source_node.total_file_count > 0 ? `(${parent.source_node.total_file_count})` : '');
  186. filingObj.filingTree.updateNode(parent);
  187. parent = parent.getParentNode();
  188. }
  189. }
  190. uploadFiles(files, callback) {
  191. const formData = new FormData();
  192. formData.append('filing_id', filingObj.curFiling.id);
  193. for (const file of files) {
  194. if (file === undefined) {
  195. toastr.error('未选择上传文件。');
  196. return false;
  197. }
  198. if (file.size > 50 * 1024 * 1024) {
  199. toastr.error('上传文件大小超过50MB。');
  200. return false;
  201. }
  202. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  203. if (whiteList.indexOf(fileext) === -1) {
  204. toastr.error('仅支持office文档、图片、压缩包格式,请勿上传' + fileext + '格式文件。');
  205. return false;
  206. }
  207. formData.append('size', file.size);
  208. formData.append('file[]', file);
  209. }
  210. postDataWithFile('file/upload', formData, function (data) {
  211. filingObj.curFiling.source_node.files.unshift(...data.files);
  212. filingObj.updateFilingFileCount(filingObj.curFiling, data.filing.file_count);
  213. filingObj.refreshFilesTable();
  214. filingObj.refreshPages();
  215. if (callback) callback();
  216. });
  217. }
  218. delFiles(files, callback) {
  219. postData('file/del', { del: files }, async function(data) {
  220. for (const id of data.del) {
  221. const fIndex = filingObj.curFiling.source_node.files.findIndex(x => { return x.id === id });
  222. if (fIndex >= 0) filingObj.curFiling.source_node.files.splice(fIndex, 1);
  223. }
  224. filingObj.updateFilingFileCount(filingObj.curFiling, data.filing.file_count);
  225. await filingObj.loadFiles(filingObj.curFiling, filingObj.curPage);
  226. filingObj.refreshFilesTable();
  227. filingObj.refreshPages();
  228. if (callback) callback();
  229. });
  230. }
  231. renameFile(fileId, filename) {
  232. const self = this;
  233. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fileId });
  234. if (!file) return;
  235. const td = $(`td[fid=${fileId}]`);
  236. if (filename === file.filename + file.fileext) {
  237. td.html(this._getFileNameHtml(file));
  238. return;
  239. }
  240. postData('file/save', { id: fileId, filename }, function(data) {
  241. file.filename = data.filename;
  242. file.fileext = data.fileext;
  243. td.html(self._getFileNameHtml(file));
  244. }, function() {
  245. td.html(self._getFileNameHtml(file));
  246. });
  247. }
  248. relaFiles(files, callback) {
  249. postData('file/rela', { filing_id: this.curFiling.id, files: files }, async function(data) {
  250. filingObj.curFiling.source_node.files.unshift(...data.files);
  251. filingObj.updateFilingFileCount(filingObj.curFiling, data.filing.file_count);
  252. filingObj.refreshFilesTable();
  253. filingObj.refreshPages();
  254. if (callback) callback();
  255. });
  256. }
  257. async setCurFiling(node) {
  258. filingObj.curFiling = node;
  259. filingObj.curPage = 1;
  260. filingObj.refreshPages();
  261. if (filingObj.curFiling.source_node.children && filingObj.curFiling.source_node.children.length > 0) {
  262. $('#file-view').hide();
  263. } else {
  264. $('#file-view').show();
  265. await filingObj.loadFiles(node, 1);
  266. filingObj.refreshFilesTable();
  267. }
  268. if (filingObj.curFiling.source_node.filing_type === 5) {
  269. $('#rela-file-btn').show();
  270. } else {
  271. $('#rela-file-btn').hide();
  272. }
  273. setLocalCache(this.curFilingKey, filingObj.curFiling.id);
  274. }
  275. prePage() {
  276. if (this.curPage === 1) return;
  277. this.curPage = this.curPage - 1;
  278. this.refreshPages();
  279. this.refreshFilesTable();
  280. }
  281. async nextPage() {
  282. if (this.curPage === this.curTotalPage) return;
  283. await filingObj.loadFiles(this.curFiling, this.curPage + 1);
  284. this.curPage = this.curPage + 1;
  285. this.refreshPages();
  286. this.refreshFilesTable();
  287. }
  288. getCurFilingFullPath(){
  289. let cur = filingObj.curFiling;
  290. const result = [];
  291. while (cur) {
  292. result.unshift(cur.source_node.name);
  293. cur = cur.getParentNode();
  294. }
  295. return result.join('/');
  296. }
  297. expandFiling(node, expand) {
  298. if (expand) {
  299. this.expandCache.push(node.id);
  300. } else{
  301. this.expandCache = this.expandCache.filter(x => { return x !== node.id });
  302. }
  303. setLocalCache(this.expandKey, this.expandCache.join(','));
  304. }
  305. expandByLevel(level) {
  306. this.expandByCustom(x => {
  307. return x.level + 1 < level;
  308. })
  309. }
  310. expandByCustom(fun) {
  311. const self = this;
  312. const expandCache = [];
  313. const expandChildren = function(children) {
  314. for (const child of children) {
  315. if (!child.children || child.children.length === 0) continue;
  316. const expand = fun(child);
  317. if (expand) expandCache.push(child.id);
  318. self.filingTree.expandNode(child, expand, false, false);
  319. expandChildren(child.children);
  320. }
  321. };
  322. const nodes = this.filingTree.getNodes();
  323. expandChildren(nodes);
  324. this.expandCache = expandCache;
  325. setLocalCache(this.expandKey, this.expandCache.join(','));
  326. }
  327. moveFiling(node, tree_pid, tree_order) {
  328. if (tree_pid === node.source_node.tree_pid && tree_order === node.source_node.tree_order) return;
  329. const self = this;
  330. postData('filing/move', { id: node.id, tree_pid, tree_order }, function(result) {
  331. const refresh = self.dragTree.loadPostData(result);
  332. self.calcTotalFileCount();
  333. for (const u of refresh.update) {
  334. const node = self.filingTree.getNodeByParam('id', u.id);
  335. if (node) {
  336. node.name = node.source_node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
  337. filingObj.filingTree.updateNode(node);
  338. }
  339. }
  340. });
  341. }
  342. }
  343. const levelTreeSetting = {
  344. treeId: 'filing',
  345. view: {
  346. selectedMulti: false
  347. },
  348. data: {
  349. simpleData: {
  350. idKey: 'id',
  351. pIdKey: 'tree_pid',
  352. rootPId: '-1',
  353. enable: true,
  354. }
  355. },
  356. edit: {
  357. enable: true,
  358. showRemoveBtn: function(treeId, treeNode) {
  359. if (!canFiling) return false;
  360. return !treeNode.source_node.is_fixed;
  361. },
  362. showRenameBtn: function(treeId, treeNode) {
  363. if (!canFiling) return false;
  364. return !treeNode.source_node.is_fixed;
  365. },
  366. renameTitle: '编辑',
  367. drag: {
  368. isCopy: false,
  369. isMove: true,
  370. pre: true,
  371. next: true,
  372. inner: false,
  373. },
  374. editNameSelectAll: true,
  375. },
  376. callback: {
  377. onClick: async function (e, key, node) {
  378. if (filingObj.curFiling && filingObj.curFiling.id === node.id) return;
  379. filingObj.setCurFiling(node);
  380. },
  381. beforeEditName: function(key, node) {
  382. node.name = node.source_node.name;
  383. },
  384. beforeRename: async function(key, node, newName, isCancel) {
  385. if (!isCancel) await filingObj.renameFiling(node, newName);
  386. return true;
  387. },
  388. onRename: function(e, key, node, isCancel) {
  389. node.name = node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
  390. filingObj.filingTree.updateNode(node);
  391. },
  392. beforeRemove: function(e, key, node, isCancel) {
  393. $('#del-filing').modal('show');
  394. return false;
  395. },
  396. onExpand(e, key, node) {
  397. filingObj.expandFiling(node, true);
  398. },
  399. onCollapse: function(e, key, node) {
  400. filingObj.expandFiling(node, false);
  401. },
  402. beforeDrop: function(key, nodes, target, moveType, isCopy) {
  403. if (!target) return false;
  404. if (nodes[0].level < 1) {
  405. toastr.error('顶层节点请勿移动');
  406. return false;
  407. }
  408. if (nodes[0].source_node.filing_type !== target.source_node.filing_type) {
  409. toastr.error('请勿跨越最顶层节点移动');
  410. return false;
  411. }
  412. if (target.source_node.file_count > 0 && moveType === 'inner') {
  413. toastr.error(`节点[${target.source_node.name}]下存在文件,不可添加子级`);
  414. return false;
  415. }
  416. const order = nodes[0].getIndex() + 1;
  417. const targetOrder = target.getIndex() + 1;
  418. const targetMax = target.getParentNode().children.length;
  419. if (moveType === 'prev') {
  420. if (target.tree_pid === nodes[0].tree_pid) {
  421. if (targetOrder > order) {
  422. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
  423. } else {
  424. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  425. }
  426. } else {
  427. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  428. }
  429. } else if (moveType === 'next') {
  430. if (target.tree_pid === nodes[0].tree_pid) {
  431. if (targetOrder < order) {
  432. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
  433. } else {
  434. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
  435. }
  436. } else {
  437. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
  438. }
  439. } else if (moveType === 'inner') {
  440. filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
  441. }
  442. }
  443. }
  444. };
  445. const filingObj = new FilingObj(levelTreeSetting);
  446. filingObj.analysisFiling(filing);
  447. $('#add-slibing').click(() => {
  448. if (!filingObj.curFiling) return;
  449. if (filingObj.curFiling.source_node.is_fixed) {
  450. toastr.error('固定分类不可添加同级');
  451. return;
  452. }
  453. filingObj.addSiblingFiling(filingObj.curFiling);
  454. });
  455. $('#add-child').click(() => {
  456. if (!filingObj.curFiling) return;
  457. if (filingObj.curFiling.source_node.file_count > 0) {
  458. toastr.error('该分类下已导入文件,不可添加子级');
  459. return;
  460. }
  461. filingObj.addChildFiling(filingObj.curFiling);
  462. });
  463. // $('#del-filing-btn').click(() => {
  464. // if (!filingObj.curFiling) return;
  465. // if (filingObj.curFiling.source_node.is_fixed) {
  466. // toastr.error('固定分类不可删除');
  467. // return;
  468. // }
  469. //
  470. // $('#del-filing').modal('show');
  471. // });
  472. $('#del-filing-ok').click(() => {
  473. filingObj.delFiling(filingObj.curFiling, function() {
  474. $('#del-filing').modal('hide');
  475. });
  476. });
  477. $('#add-file-ok').click(() => {
  478. const input = $('#upload-file');
  479. filingObj.uploadFiles(input[0].files, function() {
  480. $(input).val('');
  481. $('#add-file').modal('hide');
  482. });
  483. });
  484. $('body').on('mouseenter', ".table-file", function(){
  485. $(this).children(".btn-group-table").css("display","block");
  486. });
  487. $('body').on('mouseleave', ".table-file", function(){
  488. $(this).children(".btn-group-table").css("display","none");
  489. });
  490. $('body').on('click', "a[name=del-file]", function() {
  491. const del = [this.getAttribute('fid')];
  492. filingObj.delFiles(del);
  493. });
  494. $('body').on('click', "a[name=edit-file]", function() {
  495. const check = $('[name=filename] input[fid]');
  496. if (check.length > 0 && check[0].getAttribute('fid') === this.getAttribute('fid')) return;
  497. const id = this.getAttribute('fid');
  498. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === id });
  499. $(`td[fid=${id}]`).html(filingObj._getEditFileNameHtml(file));
  500. });
  501. $('body').on('click', "a[name=edit-file-ok]", function() {
  502. const td = $(this).parent().parent().parent();
  503. const fid = td.attr('fid');
  504. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  505. if (!file) return;
  506. filingObj.renameFile(fid, $('input', td).val());
  507. });
  508. $('body').on('click', "a[name=edit-file-cancel]", function() {
  509. const td = $(this).parent().parent().parent();
  510. const fid = td.attr('fid');
  511. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  512. if (!file) return;
  513. td.html(filingObj._getFileNameHtml(file));
  514. });
  515. // $('body').on('blur', "[name=filename] input[fid]", function() {
  516. // filingObj.renameFile(this.getAttribute('fid'), this.value);
  517. // });
  518. // $('body').on('keypress', "[name=filename] input[fid]", function(e) {
  519. // if (e.keyCode == 13) {
  520. // filingObj.renameFile(this.getAttribute('fid'), this.value);
  521. // }
  522. // });
  523. $('.page-select').click(function() {
  524. const content = this.getAttribute('content');
  525. switch(content) {
  526. case 'pre': filingObj.prePage(); break;
  527. case 'next': filingObj.nextPage(); break;
  528. default: return;
  529. }
  530. });
  531. $('#batch-download').click(function () {
  532. const self = this;
  533. const files = [];
  534. const checkes = $('[name=bd-check]:checked');
  535. checkes.each(function() {
  536. const fid = this.getAttribute('fid');
  537. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid; });
  538. file && files.push(file);
  539. });
  540. if (files.length === 0) return;
  541. $(self).attr('disabled', 'true');
  542. AliOss.zipFiles(files, filingObj.curFiling.source_node.name + '.zip', (fails) => {
  543. $(self).removeAttr('disabled');
  544. if (fails.length === 0) {
  545. toastr.success('下载成功');
  546. } else {
  547. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  548. }
  549. }, () => {
  550. $(self).removeAttr('disabled');
  551. toastr.error('批量下载失败');
  552. });
  553. });
  554. $('#batch-del-file-btn').click(() => {
  555. const checkes = $('[name=bd-check]:checked');
  556. if (checkes.length === 0) {
  557. return;
  558. } else {
  559. for (const c of checkes) {
  560. const fid = c.getAttribute('fid');
  561. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  562. if (!file) continue;
  563. if (file.user_id !== userID) {
  564. toastr.error(`文件【${file.filename + file.fileext}】不是您上传的文件,请勿删除`);
  565. return;
  566. }
  567. }
  568. }
  569. $('#batch-del-file').modal('show');
  570. });
  571. $('#batch-del-file-ok').click(function() {
  572. const del = [];
  573. const checkes = $('[name=bd-check]:checked');
  574. checkes.each(function() {
  575. del.push(this.getAttribute('fid'));
  576. });
  577. filingObj.delFiles(del, function() {
  578. $('#batch-del-file').modal('hide');
  579. });
  580. });
  581. class RelaFileLoader {
  582. constructor() {
  583. const self = this;
  584. // 可导入的标段
  585. this.treeSetting = {
  586. view: {
  587. selectedMulti: false
  588. },
  589. data: {
  590. simpleData: {
  591. idKey: 'id',
  592. pIdKey: 'tree_pid',
  593. rootPId: '-1',
  594. enable: true,
  595. }
  596. },
  597. edit: {
  598. enable: false,
  599. },
  600. callback: {
  601. onClick: async function (e, key, node) {
  602. if (this.curTender && this.curTender.id === node.id) return;
  603. self.setCurTender(node);
  604. },
  605. }
  606. };
  607. $('body').on('click', '[name=rf-check]', function () {
  608. self.selectFile(this.getAttribute('rfid'), this.checked);
  609. });
  610. $('#tf-type').change(function() {
  611. self.selectTfType(this.value);
  612. });
  613. $('#tf-sub-type').change(function() {
  614. self.selectTfSubType(this.value);
  615. });
  616. $('#tf-stage').change(function() {
  617. self.selectTfStage(this.value);
  618. });
  619. $('#rela-file-ok').click(function() {
  620. const selectFiles = self.getSelectRelaFile();
  621. filingObj.relaFiles(selectFiles, function() {
  622. $('#rela-file').modal('hide');
  623. });
  624. });
  625. }
  626. clearFileSelect() {
  627. if (!this.tenderTree) return;
  628. const nodes = this.tenderTree.getNodes();
  629. nodes.forEach(node => {
  630. const x = node.source_node;
  631. x.selectFiles = [];
  632. if (x.att) x.att.forEach(la => { la.checked = false });
  633. if (x.advance) {
  634. x.advance.forEach(a => {
  635. if (a.files) a.files.forEach(aa => { aa.checked = false });
  636. });
  637. }
  638. if (x.stage) {
  639. x.stage.forEach(s => {
  640. if (s.att) s.att.forEach(sa => { sa.checked = false });
  641. })
  642. }
  643. });
  644. }
  645. refreshSelectHint(){
  646. if (this.curTender) {
  647. $('#cur-tender-hint').html(`当前标段,已选${this.curTender.source_node.selectFiles.length}文件`);
  648. } else {
  649. $('#cur-tender-hint').html('');
  650. }
  651. const nodes = this.tenderTree.getNodes();
  652. const selectTenders = nodes.filter(x => { return x.source_node.selectFiles.length > 0; });
  653. if (selectTenders.length > 0) {
  654. const count = selectTenders.reduce((rst, x) => { return rst + x.source_node.selectFiles.length; }, 0);
  655. $('#rela-file-hint').html(`已选择${selectTenders.length}个标段,共${count}个文件`);
  656. } else {
  657. $('#rela-file-hint').html('未选择标段、文件');
  658. }
  659. }
  660. selectFile(fileId, isSelect) {
  661. const file = this.curFiles.find(x => { return x.rf_id == fileId });
  662. if (file) {
  663. file.checked = isSelect;
  664. if (isSelect) {
  665. this.curTender.source_node.selectFiles.push(file);
  666. } else {
  667. const index = this.curTender.source_node.selectFiles.findIndex(x => { return x.rf_id === file.rf_id });
  668. this.curTender.source_node.selectFiles.splice(index, 1);
  669. }
  670. this.refreshSelectHint();
  671. }
  672. }
  673. async showRelaFile(){
  674. $('#rela-filing-hint').html(`当前目录:${filingObj.getCurFilingFullPath()}`);
  675. if (!this.tenderTree) {
  676. const tenders = await postDataAsync('file/rela/tender', {});
  677. const sortNodes = tenders.map(x => {
  678. return { id: x.id, tree_pid: -1, name: x.name, source_node: x };
  679. });
  680. this.tenderTree = this.filingTree = $.fn.zTree.init($('#rela-tender'), this.treeSetting, sortNodes);
  681. }
  682. this.clearFileSelect();
  683. this.refreshSelectHint();
  684. const firstNode = this.filingTree.getNodes()[0];
  685. if (firstNode) {
  686. this.filingTree.selectNode(firstNode);
  687. await this.setCurTender(firstNode);
  688. }
  689. }
  690. refreshTenderFileStage() {
  691. if (this.rfType.sub_type) {
  692. const type = this.tenderFileType.find(x => { return x.value === this.rfType.type});
  693. const subType = type.subType ? type.subType.find(x => { return x.value === this.rfType.sub_type; }) : null;
  694. if (subType) {
  695. this.rfType.stage = subType.stage[0].value;
  696. const html= [];
  697. for (const stage of subType.stage) {
  698. html.push(`<option value="${stage.value}">${stage.text}</option>`);
  699. }
  700. $('#tf-stage').html(html.join('')).show();
  701. } else {
  702. $('#tf-stage').html('').hide();
  703. }
  704. } else {
  705. $('#tf-stage').html('').hide();
  706. }
  707. }
  708. refreshTenderFileSubType() {
  709. const type = this.tenderFileType.find(x => { return x.value === this.rfType.type});
  710. if (type.subType && type.subType.length > 0) {
  711. this.rfType.sub_type = type.subType[0].value;
  712. const html= [];
  713. for (const tfst of type.subType) {
  714. html.push(`<option value="${tfst.value}">${tfst.text}</option>`);
  715. }
  716. $('#tf-sub-type').html(html.join('')).show();
  717. } else {
  718. $('#tf-sub-type').html('').hide();
  719. }
  720. }
  721. refreshTenderFileType() {
  722. const html= [];
  723. for (const tft of this.tenderFileType) {
  724. html.push(`<option value="${tft.value}">${tft.text}</option>`);
  725. }
  726. $('#tf-type').html(html.join(''));
  727. }
  728. refreshSelects(tender) {
  729. this.tenderFileType = [];
  730. this.tenderFileType.push({ value: 'ledger', text: '台账附件' });
  731. if (tender.stage && tender.stage.length > 0) {
  732. const stages = tender.stage.map(x => { return {value: x.id, text: `第${x.order}期`}; });
  733. this.tenderFileType.push({
  734. value: 'stage', text: '计量期',
  735. subType: [
  736. { value: 'att', text: '计量附件', stage: JSON.parse(JSON.stringify(stages)) },
  737. ],
  738. });
  739. }
  740. if (tender.advance && tender.advance.length > 0) {
  741. const advanceType = [];
  742. tender.advance.forEach(x => {
  743. let at = advanceType.find(y => { return y.value === x.type + '' });
  744. if (!at) {
  745. at = { value: x.type + '', text: x.type_str, stage: [] };
  746. advanceType.push(at);
  747. }
  748. at.stage.push({ value: x.id, text: `第${x.order}期`});
  749. });
  750. this.tenderFileType.push({
  751. value: 'advance', text: '预付款', subType: advanceType
  752. });
  753. }
  754. this.rfType = { type: this.tenderFileType[0].value };
  755. this.refreshTenderFileType();
  756. this.refreshTenderFileSubType();
  757. this.refreshTenderFileStage();
  758. }
  759. async selectTfStage(stage){
  760. this.rfType.stage = stage;
  761. await this.loadFiles();
  762. this.refreshFileTable();
  763. }
  764. async selectTfSubType(sub_type){
  765. this.rfType.sub_type = sub_type;
  766. this.refreshTenderFileStage();
  767. await this.loadFiles();
  768. this.refreshFileTable();
  769. }
  770. async selectTfType(type){
  771. this.rfType.type = type;
  772. this.refreshTenderFileSubType();
  773. this.refreshTenderFileStage();
  774. await this.loadFiles();
  775. this.refreshFileTable();
  776. }
  777. refreshFileTable() {
  778. const html = [];
  779. const typeStr = [];
  780. const selectOption = $('option:selected');
  781. selectOption.each((i, x) => {
  782. typeStr.push(x.innerText);
  783. });
  784. for (const f of this.curFiles) {
  785. html.push('<tr>');
  786. const checked = f.checked ? "checked" : '';
  787. html.push(`<td><input type="checkbox" name="rf-check" rfid="${f.rf_id}" ${checked}></td>`);
  788. html.push(`<td>${f.filename}${f.fileext}</td>`);
  789. html.push(`<td>${typeStr.join(' - ')}</td>`);
  790. html.push('</tr>');
  791. }
  792. $('#rf-files').html(html.join(''));
  793. };
  794. initFilesId(files){
  795. const tender_id = this.curTender.id;
  796. const rfType = this.rfType;
  797. files.forEach((f, i) => {
  798. f.rf_id = `${tender_id}-${rfType.type}-${rfType.sub_type}-${rfType.stage}-${i}`;
  799. f.rf_key = {
  800. tender_id, ...rfType, id: f.id,
  801. };
  802. });
  803. }
  804. async _loadRelaFiles(rfType) {
  805. return await postDataAsync('file/rela/files', { tender_id: this.curTender.id, ...rfType });
  806. }
  807. async _loadLedgerFile() {
  808. if (!this.curTender.source_node.att) this.curTender.source_node.att = await this._loadRelaFiles(this.rfType);
  809. this.curFiles = this.curTender.source_node.att;
  810. }
  811. async _loadStageFile() {
  812. const rfType = this.rfType;
  813. const stage = this.curTender.source_node.stage.find(x => {
  814. return x.id == rfType.stage;
  815. });
  816. if (!stage) {
  817. this.curFiles = [];
  818. return;
  819. }
  820. if (!stage[this.rfType.sub_type]) stage[this.rfType.sub_type] = await this._loadRelaFiles(rfType);
  821. this.curFiles = stage[this.rfType.sub_type];
  822. }
  823. async _loadAdvanceFile() {
  824. const rfType = this.rfType;
  825. const advance = this.curTender.source_node.advance.find(x => {
  826. return x.id == rfType.stage;
  827. });
  828. if (!advance) {
  829. this.curFiles = [];
  830. return;
  831. }
  832. if (!advance.files) advance.files = await this._loadRelaFiles(rfType);
  833. this.curFiles = advance.files;
  834. }
  835. async loadFiles() {
  836. switch (this.rfType.type) {
  837. case 'ledger': await this._loadLedgerFile(); break;
  838. case 'stage': await this._loadStageFile(); break;
  839. case 'advance': await this._loadAdvanceFile(); break;
  840. }
  841. this.initFilesId(this.curFiles);
  842. }
  843. async setCurTender(node) {
  844. this.curTender = node;
  845. this.refreshSelects(node.source_node);
  846. await this.loadFiles();
  847. this.refreshSelectHint();
  848. this.refreshFileTable();
  849. }
  850. getSelectRelaFile() {
  851. const data = [];
  852. const nodes = this.tenderTree.getNodes();
  853. nodes.forEach(node => {
  854. if (node.source_node.selectFiles.length === 0) return;
  855. node.source_node.selectFiles.forEach(x => {
  856. data.push({
  857. filename: x.filename, fileext: x.fileext, filepath: x.filepath, filesize: x.filesize,
  858. rela_info: x.rf_key,
  859. })
  860. });
  861. });
  862. return data;
  863. }
  864. }
  865. const relaFileLoader = new RelaFileLoader();
  866. $('#rela-file').on('show.bs.modal', function() {
  867. relaFileLoader.showRelaFile(this.getAttribute('content'));
  868. });
  869. // 授权相关
  870. class FilingPermission {
  871. constructor (setting) {
  872. this.setting = setting;
  873. const self = this;
  874. $(setting.modal).on('show.bs.modal', () => {
  875. self.loadPermission();
  876. });
  877. $(`${setting.modal}-ok`).click(() => {
  878. self.savePermission();
  879. });
  880. $('[name=ftName]').click(function () {
  881. const filingId = this.getAttribute('ftid');
  882. self.setCurFiling(filingId);
  883. });
  884. $('.book-list').on('click', 'dt', function () {
  885. const idx = $(this).find('.acc-btn').attr('data-groupid');
  886. const type = $(this).find('.acc-btn').attr('data-type');
  887. if (type === 'hide') {
  888. $(this).parent().find(`div[data-toggleid="${idx}"]`).show(() => {
  889. $(this).children().find('i').removeClass('fa-plus-square').addClass('fa-minus-square-o')
  890. $(this).find('.acc-btn').attr('data-type', 'show')
  891. })
  892. } else {
  893. $(this).parent().find(`div[data-toggleid="${idx}"]`).hide(() => {
  894. $(this).children().find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square')
  895. $(this).find('.acc-btn').attr('data-type', 'hide')
  896. })
  897. }
  898. return false;
  899. });
  900. $('dl').on('click', 'dd', function () {
  901. const type = $(this).data('type');
  902. if (type === 'all') {
  903. const cid = parseInt($(this).data('id'));
  904. const company = self.company.find(x => { return x.id === cid });
  905. for (const u of company.users) {
  906. if (u.filing_type.indexOf(self.curFiling) < 0) u.filing_type.push(self.curFiling);
  907. }
  908. } else {
  909. const uid = $(this).data('id');
  910. const pu = self.permissionUser.find(x => { return x.id === uid });
  911. if (pu.filing_type.indexOf(self.curFiling) < 0) pu.filing_type.push(self.curFiling);
  912. }
  913. self.loadCurFiling();
  914. });
  915. $('#sync-filing').click(function() {
  916. const selectFiling = $('[name=cbft]:checked');
  917. if (selectFiling.length === 0) {
  918. toastr.warning('请先选择文档类别');
  919. return;
  920. }
  921. const selectFilingId = [];
  922. selectFiling.each((i, x) => { selectFilingId.push(x.value); });
  923. self.syncFiling(self.curFiling, selectFilingId);
  924. toastr.success('同步成功');
  925. $('[name=cbft]').each((i, x) => { x.checked = false; });
  926. });
  927. $('#batch-del-filing').click(() => {
  928. const selectUser = $('[name=ftu-check]:checked');
  929. if (selectUser.length === 0) {
  930. toastr.warning('请先选择用户');
  931. return;
  932. }
  933. const userId = [];
  934. selectUser.each((i, x) => { userId.push(x.getAttribute('uid')); });
  935. self.delFiling(self.curFiling, userId);
  936. self.loadCurFiling();
  937. });
  938. $('body').on('click', '[name=del-filing]', function() {
  939. const id = this.getAttribute('uid');
  940. self.delFiling(self.curFiling, id);
  941. self.loadCurFiling();
  942. })
  943. }
  944. analysisFiling(data) {
  945. this.permissionUser = data;
  946. this.permissionUser.forEach(x => { x.filing_type = x.filing_type ? x.filing_type.split(',') : []; });
  947. this.company = [];
  948. for (const pu of this.permissionUser) {
  949. let c = this.company.find(x => { return x.company === pu.company });
  950. if (!c) {
  951. c = { id: this.company.length + 1, company: pu.company, users: [] };
  952. this.company.push(c);
  953. }
  954. c.users.push(pu);
  955. }
  956. }
  957. loadCurFiling() {
  958. const html = [];
  959. for (const f of this.permissionUser) {
  960. if (f.filing_type.indexOf(this.curFiling) < 0) continue;
  961. html.push('<tr>');
  962. html.push(`<td><input uid="${f.id}" type="checkbox" name="ftu-check"></td>`);
  963. html.push(`<td>${f.name}</td>`);
  964. html.push(`<td>${moment(f.create_time).format('YYYY-MM-DD HH:mm:ss')}</td>`);
  965. html.push(`<td>${f.file_permission}</td>`);
  966. html.push(`<td><button class="btn btn-sm btn-outline-danger" uid="${f.id}" name="del-filing">移除</button></td>`);
  967. html.push('</tr>');
  968. }
  969. $(this.setting.list).html(html.join(''));
  970. }
  971. setCurFiling(filingType) {
  972. this.curFiling = filingType;
  973. $('[name=ftName]').removeClass('bg-warning-50');
  974. $(`[ftid=${filingType}]`).addClass('bg-warning-50');
  975. this.loadCurFiling();
  976. }
  977. loadPermissionUser() {
  978. const html = [];
  979. for (const c of this.company) {
  980. html.push(`<dt><a href="javascript: void(0);" class="acc-btn" data-groupid="${c.id}" data-type="hide"><i class="fa fa-plus-square"></i></a> ${c.company}</dt>`);
  981. html.push(`<div class="dd-content" data-toggleid="${c.id}">`);
  982. html.push(`<dd class="border-bottom p-2 mb-0 " data-id="${c.id}" data-type="all"><p class="mb-0 d-flex"><span class="text-primary">添加单位下全部用户</span></p></dd>`);
  983. for (const u of c.users) {
  984. html.push(`<dd class="border-bottom p-2 mb-0 " data-id="${u.id}" >`);
  985. html.push(`<p class="mb-0 d-flex"><span class="text-primary">${u.name}</span><span class="ml-auto">${u.mobile}</span></p>`);
  986. html.push(`<span class="text-muted">${u.role}</span>`);
  987. html.push(`</dd>`);
  988. }
  989. html.push('</div>');
  990. }
  991. $('#puList').html(html.join(''));
  992. }
  993. loadPermission() {
  994. const self = this;
  995. postData('permission', {}, function(result) {
  996. self.analysisFiling(result);
  997. if (!self.curFiling) {
  998. self.setCurFiling($('[name=ftName]').attr('ftid'));
  999. } else {
  1000. self.loadCurFiling();
  1001. }
  1002. self.loadPermissionUser();
  1003. });
  1004. }
  1005. syncFiling(sourceId, targetIds) {
  1006. for (const pu of this.permissionUser) {
  1007. if (pu.filing_type.indexOf(sourceId) >= 0) {
  1008. targetIds.forEach(id => {
  1009. if (pu.filing_type.indexOf(id) < 0) pu.filing_type.push(id);
  1010. });
  1011. } else {
  1012. targetIds.forEach(id => {
  1013. if (pu.filing_type.indexOf(id) >= 0) pu.filing_type.splice(pu.filing_type.indexOf(id), 1);
  1014. })
  1015. }
  1016. }
  1017. }
  1018. delFiling(filingId, userId) {
  1019. const userIds = userId instanceof Array ? userId : [userId];
  1020. for (const id of userIds) {
  1021. const pu = this.permissionUser.find(x => { return x.id === id });
  1022. if (!pu) continue;
  1023. if (pu.filing_type.indexOf(filingId) >= 0) pu.filing_type.splice(pu.filing_type.indexOf(filingId), 1);
  1024. }
  1025. }
  1026. savePermission() {
  1027. const self = this;
  1028. const data = this.permissionUser.map(x => {
  1029. return { id: x.id, filing_type: x.filing_type.join(',') };
  1030. });
  1031. postData('permission/save', data, function(result) {
  1032. $(self.setting.modal).modal('hide');
  1033. });
  1034. }
  1035. }
  1036. const filingPermission = new FilingPermission({
  1037. modal: '#filing-permission',
  1038. list: '#filing-valid',
  1039. });
  1040. // 显示层次
  1041. (function (select) {
  1042. $(select).click(function () {
  1043. const tag = $(this).attr('tag');
  1044. setTimeout(() => {
  1045. showWaitingView();
  1046. switch (tag) {
  1047. case "1":
  1048. case "2":
  1049. case "3":
  1050. case "4":
  1051. filingObj.expandByLevel(parseInt(tag));
  1052. break;
  1053. case "last":
  1054. filingObj.expandByCustom(() => { return true; });
  1055. break;
  1056. }
  1057. closeWaitingView();
  1058. }, 100);
  1059. });
  1060. })('a[name=showLevel]');
  1061. });