file_detail.js 47 KB

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