file_detail.js 47 KB

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