file_detail.js 48 KB

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