file_detail.js 44 KB

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