file_detail.js 48 KB

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