file_detail.js 42 KB

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