file_detail.js 43 KB

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