file_detail.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 (!target) return false;
  413. if (nodes[0].level < 1) {
  414. toastr.error('顶层节点请勿移动');
  415. return false;
  416. }
  417. if (nodes[0].source_node.filing_type !== target.source_node.filing_type) {
  418. toastr.error('请勿跨越最顶层节点移动');
  419. return false;
  420. }
  421. if (target.source_node.file_count > 0 && moveType === 'inner') {
  422. toastr.error(`节点[${target.source_node.name}]下存在文件,不可添加子级`);
  423. return false;
  424. }
  425. const order = nodes[0].getIndex() + 1;
  426. const targetOrder = target.getIndex() + 1;
  427. const targetMax = target.getParentNode().children.length;
  428. if (moveType === 'prev') {
  429. if (target.tree_pid === nodes[0].tree_pid) {
  430. if (targetOrder > order) {
  431. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
  432. } else {
  433. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  434. }
  435. } else {
  436. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
  437. }
  438. } else if (moveType === 'next') {
  439. if (target.tree_pid === nodes[0].tree_pid) {
  440. if (targetOrder < order) {
  441. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
  442. } else {
  443. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
  444. }
  445. } else {
  446. filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
  447. }
  448. } else if (moveType === 'inner') {
  449. filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
  450. }
  451. }
  452. }
  453. };
  454. const filingObj = new FilingObj(levelTreeSetting);
  455. filingObj.analysisFiling(filing);
  456. $('#add-slibing').click(() => {
  457. if (!filingObj.curFiling) return;
  458. if (filingObj.curFiling.source_node.is_fixed) {
  459. toastr.error('固定分类不可添加同级');
  460. return;
  461. }
  462. filingObj.addSiblingFiling(filingObj.curFiling);
  463. });
  464. $('#add-child').click(() => {
  465. if (!filingObj.curFiling) return;
  466. if (filingObj.curFiling.source_node.file_count > 0) {
  467. toastr.error('该分类下已导入文件,不可添加子级');
  468. return;
  469. }
  470. filingObj.addChildFiling(filingObj.curFiling);
  471. });
  472. // $('#del-filing-btn').click(() => {
  473. // if (!filingObj.curFiling) return;
  474. // if (filingObj.curFiling.source_node.is_fixed) {
  475. // toastr.error('固定分类不可删除');
  476. // return;
  477. // }
  478. //
  479. // $('#del-filing').modal('show');
  480. // });
  481. $('#del-filing-ok').click(() => {
  482. filingObj.delFiling(filingObj.curFiling, function() {
  483. $('#del-filing').modal('hide');
  484. });
  485. });
  486. $('#add-file-ok').click(() => {
  487. const input = $('#upload-file');
  488. filingObj.uploadFiles(input[0].files, function() {
  489. $(input).val('');
  490. $('#add-file').modal('hide');
  491. });
  492. });
  493. $('body').on('mouseenter', ".table-file", function(){
  494. $(this).children(".btn-group-table").css("display","block");
  495. });
  496. $('body').on('mouseleave', ".table-file", function(){
  497. $(this).children(".btn-group-table").css("display","none");
  498. });
  499. $('body').on('click', "a[name=del-file]", function() {
  500. const del = [this.getAttribute('fid')];
  501. filingObj.delFiles(del);
  502. });
  503. $('body').on('click', "a[name=edit-file]", function() {
  504. const check = $('[name=filename] input[fid]');
  505. if (check.length > 0 && check[0].getAttribute('fid') === this.getAttribute('fid')) return;
  506. const id = this.getAttribute('fid');
  507. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === id });
  508. $(`td[fid=${id}]`).html(filingObj._getEditFileNameHtml(file));
  509. });
  510. $('body').on('click', "a[name=edit-file-ok]", function() {
  511. const td = $(this).parent().parent().parent();
  512. const fid = td.attr('fid');
  513. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  514. if (!file) return;
  515. filingObj.renameFile(fid, $('input', td).val());
  516. });
  517. $('body').on('click', "a[name=edit-file-cancel]", function() {
  518. const td = $(this).parent().parent().parent();
  519. const fid = td.attr('fid');
  520. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  521. if (!file) return;
  522. td.html(filingObj._getFileNameHtml(file));
  523. });
  524. // $('body').on('blur', "[name=filename] input[fid]", function() {
  525. // filingObj.renameFile(this.getAttribute('fid'), this.value);
  526. // });
  527. // $('body').on('keypress', "[name=filename] input[fid]", function(e) {
  528. // if (e.keyCode == 13) {
  529. // filingObj.renameFile(this.getAttribute('fid'), this.value);
  530. // }
  531. // });
  532. $('.page-select').click(function() {
  533. const content = this.getAttribute('content');
  534. switch(content) {
  535. case 'pre': filingObj.prePage(); break;
  536. case 'next': filingObj.nextPage(); break;
  537. default: return;
  538. }
  539. });
  540. $('#batch-download').click(function () {
  541. const self = this;
  542. const files = [];
  543. const checkes = $('[name=bd-check]:checked');
  544. checkes.each(function() {
  545. const fid = this.getAttribute('fid');
  546. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid; });
  547. file && files.push(file);
  548. });
  549. if (files.length === 0) return;
  550. $(self).attr('disabled', 'true');
  551. AliOss.zipFiles(files, filingObj.curFiling.source_node.name + '.zip', (fails) => {
  552. $(self).removeAttr('disabled');
  553. if (fails.length === 0) {
  554. toastr.success('下载成功');
  555. } else {
  556. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  557. }
  558. }, () => {
  559. $(self).removeAttr('disabled');
  560. toastr.error('批量下载失败');
  561. });
  562. });
  563. $('#batch-del-file-btn').click(() => {
  564. const checkes = $('[name=bd-check]:checked');
  565. if (checkes.length === 0) {
  566. return;
  567. } else {
  568. for (const c of checkes) {
  569. const fid = c.getAttribute('fid');
  570. const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fid });
  571. if (!file) continue;
  572. if (file.user_id !== userID) {
  573. toastr.error(`文件【${file.filename + file.fileext}】不是您上传的文件,请勿删除`);
  574. return;
  575. }
  576. }
  577. }
  578. $('#batch-del-file').modal('show');
  579. });
  580. $('#batch-del-file-ok').click(function() {
  581. const del = [];
  582. const checkes = $('[name=bd-check]:checked');
  583. checkes.each(function() {
  584. del.push(this.getAttribute('fid'));
  585. });
  586. filingObj.delFiles(del, function() {
  587. $('#batch-del-file').modal('hide');
  588. });
  589. });
  590. class RelaFileLoader {
  591. constructor() {
  592. const self = this;
  593. // 可导入的标段
  594. this.treeSetting = {
  595. view: {
  596. selectedMulti: false
  597. },
  598. data: {
  599. simpleData: {
  600. idKey: 'id',
  601. pIdKey: 'tree_pid',
  602. rootPId: '-1',
  603. enable: true,
  604. }
  605. },
  606. edit: {
  607. enable: false,
  608. },
  609. callback: {
  610. onClick: async function (e, key, node) {
  611. if (this.curTender && this.curTender.id === node.id) return;
  612. self.setCurTender(node);
  613. },
  614. }
  615. };
  616. $('body').on('click', '[name=rf-check]', function () {
  617. self.selectFile(this.getAttribute('rfid'), this.checked);
  618. });
  619. $('#tf-type').change(function() {
  620. self.selectTfType(this.value);
  621. });
  622. $('#tf-sub-type').change(function() {
  623. self.selectTfSubType(this.value);
  624. });
  625. $('#tf-stage').change(function() {
  626. self.selectTfStage(this.value);
  627. });
  628. $('#rela-file-ok').click(function() {
  629. const selectFiles = self.getSelectRelaFile();
  630. filingObj.relaFiles(selectFiles, function() {
  631. $('#rela-file').modal('hide');
  632. });
  633. });
  634. }
  635. clearFileSelect() {
  636. if (!this.tenderTree) return;
  637. const nodes = this.tenderTree.getNodes();
  638. nodes.forEach(node => {
  639. const x = node.source_node;
  640. x.selectFiles = [];
  641. if (x.att) x.att.forEach(la => { la.checked = false });
  642. if (x.advance) {
  643. x.advance.forEach(a => {
  644. if (a.files) a.files.forEach(aa => { aa.checked = false });
  645. });
  646. }
  647. if (x.stage) {
  648. x.stage.forEach(s => {
  649. if (s.att) s.att.forEach(sa => { sa.checked = false });
  650. })
  651. }
  652. });
  653. }
  654. refreshSelectHint(){
  655. if (this.curTender) {
  656. $('#cur-tender-hint').html(`当前标段,已选${this.curTender.source_node.selectFiles.length}文件`);
  657. } else {
  658. $('#cur-tender-hint').html('');
  659. }
  660. const nodes = this.tenderTree.getNodes();
  661. const selectTenders = nodes.filter(x => { return x.source_node.selectFiles.length > 0; });
  662. if (selectTenders.length > 0) {
  663. const count = selectTenders.reduce((rst, x) => { return rst + x.source_node.selectFiles.length; }, 0);
  664. $('#rela-file-hint').html(`已选择${selectTenders.length}个标段,共${count}个文件`);
  665. } else {
  666. $('#rela-file-hint').html('未选择标段、文件');
  667. }
  668. }
  669. selectFile(fileId, isSelect) {
  670. const file = this.curFiles.find(x => { return x.rf_id == fileId });
  671. if (file) {
  672. file.checked = isSelect;
  673. if (isSelect) {
  674. this.curTender.source_node.selectFiles.push(file);
  675. } else {
  676. const index = this.curTender.source_node.selectFiles.findIndex(x => { return x.rf_id === file.rf_id });
  677. this.curTender.source_node.selectFiles.splice(index, 1);
  678. }
  679. this.refreshSelectHint();
  680. }
  681. }
  682. async showRelaFile(){
  683. $('#rela-filing-hint').html(`当前目录:${filingObj.getCurFilingFullPath()}`);
  684. if (!this.tenderTree) {
  685. const tenders = await postDataAsync('file/rela/tender', {});
  686. const sortNodes = tenders.map(x => {
  687. return { id: x.id, tree_pid: -1, name: x.name, source_node: x };
  688. });
  689. this.tenderTree = this.filingTree = $.fn.zTree.init($('#rela-tender'), this.treeSetting, sortNodes);
  690. }
  691. this.clearFileSelect();
  692. this.refreshSelectHint();
  693. const firstNode = this.filingTree.getNodes()[0];
  694. if (firstNode) {
  695. this.filingTree.selectNode(firstNode);
  696. await this.setCurTender(firstNode);
  697. }
  698. }
  699. refreshTenderFileStage() {
  700. if (this.rfType.sub_type) {
  701. const type = this.tenderFileType.find(x => { return x.value === this.rfType.type});
  702. const subType = type.subType ? type.subType.find(x => { return x.value === this.rfType.sub_type; }) : null;
  703. if (subType) {
  704. this.rfType.stage = subType.stage[0].value;
  705. const html= [];
  706. for (const stage of subType.stage) {
  707. html.push(`<option value="${stage.value}">${stage.text}</option>`);
  708. }
  709. $('#tf-stage').html(html.join('')).show();
  710. } else {
  711. $('#tf-stage').html('').hide();
  712. }
  713. } else {
  714. $('#tf-stage').html('').hide();
  715. }
  716. }
  717. refreshTenderFileSubType() {
  718. const type = this.tenderFileType.find(x => { return x.value === this.rfType.type});
  719. if (type.subType && type.subType.length > 0) {
  720. this.rfType.sub_type = type.subType[0].value;
  721. const html= [];
  722. for (const tfst of type.subType) {
  723. html.push(`<option value="${tfst.value}">${tfst.text}</option>`);
  724. }
  725. $('#tf-sub-type').html(html.join('')).show();
  726. } else {
  727. $('#tf-sub-type').html('').hide();
  728. }
  729. }
  730. refreshTenderFileType() {
  731. const html= [];
  732. for (const tft of this.tenderFileType) {
  733. html.push(`<option value="${tft.value}">${tft.text}</option>`);
  734. }
  735. $('#tf-type').html(html.join(''));
  736. }
  737. refreshSelects(tender) {
  738. this.tenderFileType = [];
  739. this.tenderFileType.push({ value: 'ledger', text: '台账附件' });
  740. if (tender.stage && tender.stage.length > 0) {
  741. const stages = tender.stage.map(x => { return {value: x.id, text: `第${x.order}期`}; });
  742. this.tenderFileType.push({
  743. value: 'stage', text: '计量期',
  744. subType: [
  745. { value: 'att', text: '计量附件', stage: JSON.parse(JSON.stringify(stages)) },
  746. ],
  747. });
  748. }
  749. if (tender.advance && tender.advance.length > 0) {
  750. const advanceType = [];
  751. tender.advance.forEach(x => {
  752. let at = advanceType.find(y => { return y.value === x.type + '' });
  753. if (!at) {
  754. at = { value: x.type + '', text: x.type_str, stage: [] };
  755. advanceType.push(at);
  756. }
  757. at.stage.push({ value: x.id, text: `第${x.order}期`});
  758. });
  759. this.tenderFileType.push({
  760. value: 'advance', text: '预付款', subType: advanceType
  761. });
  762. }
  763. this.rfType = { type: this.tenderFileType[0].value };
  764. this.refreshTenderFileType();
  765. this.refreshTenderFileSubType();
  766. this.refreshTenderFileStage();
  767. }
  768. async selectTfStage(stage){
  769. this.rfType.stage = stage;
  770. await this.loadFiles();
  771. this.refreshFileTable();
  772. }
  773. async selectTfSubType(sub_type){
  774. this.rfType.sub_type = sub_type;
  775. this.refreshTenderFileStage();
  776. await this.loadFiles();
  777. this.refreshFileTable();
  778. }
  779. async selectTfType(type){
  780. this.rfType.type = type;
  781. this.refreshTenderFileSubType();
  782. this.refreshTenderFileStage();
  783. await this.loadFiles();
  784. this.refreshFileTable();
  785. }
  786. refreshFileTable() {
  787. const html = [];
  788. const typeStr = [];
  789. const selectOption = $('option:selected');
  790. selectOption.each((i, x) => {
  791. typeStr.push(x.innerText);
  792. });
  793. for (const f of this.curFiles) {
  794. html.push('<tr>');
  795. const checked = f.checked ? "checked" : '';
  796. html.push(`<td><input type="checkbox" name="rf-check" rfid="${f.rf_id}" ${checked}></td>`);
  797. html.push(`<td>${f.filename}${f.fileext}</td>`);
  798. html.push(`<td>${typeStr.join(' - ')}</td>`);
  799. html.push('</tr>');
  800. }
  801. $('#rf-files').html(html.join(''));
  802. };
  803. initFilesId(files){
  804. const tender_id = this.curTender.id;
  805. const rfType = this.rfType;
  806. files.forEach((f, i) => {
  807. f.rf_id = `${tender_id}-${rfType.type}-${rfType.sub_type}-${rfType.stage}-${i}`;
  808. f.rf_key = {
  809. tender_id, ...rfType, id: f.id,
  810. };
  811. });
  812. }
  813. async _loadRelaFiles(rfType) {
  814. return await postDataAsync('file/rela/files', { tender_id: this.curTender.id, ...rfType });
  815. }
  816. async _loadLedgerFile() {
  817. if (!this.curTender.source_node.att) this.curTender.source_node.att = await this._loadRelaFiles(this.rfType);
  818. this.curFiles = this.curTender.source_node.att;
  819. }
  820. async _loadStageFile() {
  821. const rfType = this.rfType;
  822. const stage = this.curTender.source_node.stage.find(x => {
  823. return x.id == rfType.stage;
  824. });
  825. if (!stage) {
  826. this.curFiles = [];
  827. return;
  828. }
  829. if (!stage[this.rfType.sub_type]) stage[this.rfType.sub_type] = await this._loadRelaFiles(rfType);
  830. this.curFiles = stage[this.rfType.sub_type];
  831. }
  832. async _loadAdvanceFile() {
  833. const rfType = this.rfType;
  834. const advance = this.curTender.source_node.advance.find(x => {
  835. return x.id == rfType.stage;
  836. });
  837. if (!advance) {
  838. this.curFiles = [];
  839. return;
  840. }
  841. if (!advance.files) advance.files = await this._loadRelaFiles(rfType);
  842. this.curFiles = advance.files;
  843. }
  844. async loadFiles() {
  845. switch (this.rfType.type) {
  846. case 'ledger': await this._loadLedgerFile(); break;
  847. case 'stage': await this._loadStageFile(); break;
  848. case 'advance': await this._loadAdvanceFile(); break;
  849. }
  850. this.initFilesId(this.curFiles);
  851. }
  852. async setCurTender(node) {
  853. this.curTender = node;
  854. this.refreshSelects(node.source_node);
  855. await this.loadFiles();
  856. this.refreshSelectHint();
  857. this.refreshFileTable();
  858. }
  859. getSelectRelaFile() {
  860. const data = [];
  861. const nodes = this.tenderTree.getNodes();
  862. nodes.forEach(node => {
  863. if (node.source_node.selectFiles.length === 0) return;
  864. node.source_node.selectFiles.forEach(x => {
  865. data.push({
  866. filename: x.filename, fileext: x.fileext, filepath: x.filepath, filesize: x.filesize,
  867. rela_info: x.rf_key,
  868. })
  869. });
  870. });
  871. return data;
  872. }
  873. }
  874. const relaFileLoader = new RelaFileLoader();
  875. $('#rela-file').on('show.bs.modal', function() {
  876. relaFileLoader.showRelaFile(this.getAttribute('content'));
  877. });
  878. // 授权相关
  879. class FilingPermission {
  880. constructor (setting) {
  881. this.setting = setting;
  882. const self = this;
  883. $(setting.modal).on('show.bs.modal', () => {
  884. self.loadPermission();
  885. });
  886. $(`${setting.modal}-ok`).click(() => {
  887. self.savePermission();
  888. });
  889. $('[name=ftName]').click(function () {
  890. const filingId = this.getAttribute('ftid');
  891. self.setCurFiling(filingId);
  892. });
  893. $('.book-list').on('click', 'dt', function () {
  894. const idx = $(this).find('.acc-btn').attr('data-groupid');
  895. const type = $(this).find('.acc-btn').attr('data-type');
  896. if (type === 'hide') {
  897. $(this).parent().find(`div[data-toggleid="${idx}"]`).show(() => {
  898. $(this).children().find('i').removeClass('fa-plus-square').addClass('fa-minus-square-o')
  899. $(this).find('.acc-btn').attr('data-type', 'show')
  900. })
  901. } else {
  902. $(this).parent().find(`div[data-toggleid="${idx}"]`).hide(() => {
  903. $(this).children().find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square')
  904. $(this).find('.acc-btn').attr('data-type', 'hide')
  905. })
  906. }
  907. return false;
  908. });
  909. $('dl').on('click', 'dd', function () {
  910. const type = $(this).data('type');
  911. if (type === 'all') {
  912. const cid = parseInt($(this).data('id'));
  913. const company = self.company.find(x => { return x.id === cid });
  914. for (const u of company.users) {
  915. if (u.filing_type.indexOf(self.curFiling) < 0) u.filing_type.push(self.curFiling);
  916. }
  917. } else {
  918. const uid = $(this).data('id');
  919. const pu = self.permissionUser.find(x => { return x.id === uid });
  920. if (pu.filing_type.indexOf(self.curFiling) < 0) pu.filing_type.push(self.curFiling);
  921. }
  922. self.loadCurFiling();
  923. });
  924. $('#sync-filing').click(function() {
  925. const selectFiling = $('[name=cbft]:checked');
  926. if (selectFiling.length === 0) {
  927. toastr.warning('请先选择文档类别');
  928. return;
  929. }
  930. const selectFilingId = [];
  931. selectFiling.each((i, x) => { selectFilingId.push(x.value); });
  932. self.syncFiling(self.curFiling, selectFilingId);
  933. toastr.success('同步成功');
  934. $('[name=cbft]').each((i, x) => { x.checked = false; });
  935. });
  936. $('#batch-del-filing').click(() => {
  937. const selectUser = $('[name=ftu-check]:checked');
  938. if (selectUser.length === 0) {
  939. toastr.warning('请先选择用户');
  940. return;
  941. }
  942. const userId = [];
  943. selectUser.each((i, x) => { userId.push(x.getAttribute('uid')); });
  944. self.delFiling(self.curFiling, userId);
  945. self.loadCurFiling();
  946. });
  947. $('body').on('click', '[name=del-filing]', function() {
  948. const id = this.getAttribute('uid');
  949. self.delFiling(self.curFiling, id);
  950. self.loadCurFiling();
  951. })
  952. }
  953. analysisFiling(data) {
  954. this.permissionUser = data;
  955. this.permissionUser.forEach(x => { x.filing_type = x.filing_type ? x.filing_type.split(',') : []; });
  956. this.company = [];
  957. for (const pu of this.permissionUser) {
  958. let c = this.company.find(x => { return x.company === pu.company });
  959. if (!c) {
  960. c = { id: this.company.length + 1, company: pu.company, users: [] };
  961. this.company.push(c);
  962. }
  963. c.users.push(pu);
  964. }
  965. }
  966. loadCurFiling() {
  967. const html = [];
  968. for (const f of this.permissionUser) {
  969. if (f.filing_type.indexOf(this.curFiling) < 0) continue;
  970. html.push('<tr>');
  971. html.push(`<td><input uid="${f.id}" type="checkbox" name="ftu-check"></td>`);
  972. html.push(`<td>${f.name}</td>`);
  973. html.push(`<td>${moment(f.create_time).format('YYYY-MM-DD HH:mm:ss')}</td>`);
  974. html.push(`<td>${f.file_permission}</td>`);
  975. html.push(`<td><button class="btn btn-sm btn-outline-danger" uid="${f.id}" name="del-filing">移除</button></td>`);
  976. html.push('</tr>');
  977. }
  978. $(this.setting.list).html(html.join(''));
  979. }
  980. setCurFiling(filingType) {
  981. this.curFiling = filingType;
  982. $('[name=ftName]').removeClass('bg-warning-50');
  983. $(`[ftid=${filingType}]`).addClass('bg-warning-50');
  984. this.loadCurFiling();
  985. }
  986. loadPermissionUser() {
  987. const html = [];
  988. for (const c of this.company) {
  989. 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>`);
  990. html.push(`<div class="dd-content" data-toggleid="${c.id}">`);
  991. 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>`);
  992. for (const u of c.users) {
  993. html.push(`<dd class="border-bottom p-2 mb-0 " data-id="${u.id}" >`);
  994. html.push(`<p class="mb-0 d-flex"><span class="text-primary">${u.name}</span><span class="ml-auto">${u.mobile}</span></p>`);
  995. html.push(`<span class="text-muted">${u.role}</span>`);
  996. html.push(`</dd>`);
  997. }
  998. html.push('</div>');
  999. }
  1000. $('#puList').html(html.join(''));
  1001. }
  1002. loadPermission() {
  1003. const self = this;
  1004. postData('permission', {}, function(result) {
  1005. self.analysisFiling(result);
  1006. if (!self.curFiling) {
  1007. self.setCurFiling($('[name=ftName]').attr('ftid'));
  1008. } else {
  1009. self.loadCurFiling();
  1010. }
  1011. self.loadPermissionUser();
  1012. });
  1013. }
  1014. syncFiling(sourceId, targetIds) {
  1015. for (const pu of this.permissionUser) {
  1016. if (pu.filing_type.indexOf(sourceId) >= 0) {
  1017. targetIds.forEach(id => {
  1018. if (pu.filing_type.indexOf(id) < 0) pu.filing_type.push(id);
  1019. });
  1020. } else {
  1021. targetIds.forEach(id => {
  1022. if (pu.filing_type.indexOf(id) >= 0) pu.filing_type.splice(pu.filing_type.indexOf(id), 1);
  1023. })
  1024. }
  1025. }
  1026. }
  1027. delFiling(filingId, userId) {
  1028. const userIds = userId instanceof Array ? userId : [userId];
  1029. for (const id of userIds) {
  1030. const pu = this.permissionUser.find(x => { return x.id === id });
  1031. if (!pu) continue;
  1032. if (pu.filing_type.indexOf(filingId) >= 0) pu.filing_type.splice(pu.filing_type.indexOf(filingId), 1);
  1033. }
  1034. }
  1035. savePermission() {
  1036. const self = this;
  1037. const data = this.permissionUser.map(x => {
  1038. return { id: x.id, filing_type: x.filing_type.join(',') };
  1039. });
  1040. postData('permission/save', data, function(result) {
  1041. $(self.setting.modal).modal('hide');
  1042. });
  1043. }
  1044. }
  1045. const filingPermission = new FilingPermission({
  1046. modal: '#filing-permission',
  1047. list: '#filing-valid',
  1048. });
  1049. // 显示层次
  1050. (function (select) {
  1051. $(select).click(function () {
  1052. const tag = $(this).attr('tag');
  1053. setTimeout(() => {
  1054. showWaitingView();
  1055. switch (tag) {
  1056. case "1":
  1057. case "2":
  1058. case "3":
  1059. case "4":
  1060. filingObj.expandByLevel(parseInt(tag));
  1061. break;
  1062. case "last":
  1063. filingObj.expandByCustom(() => { return true; });
  1064. break;
  1065. }
  1066. closeWaitingView();
  1067. }, 100);
  1068. });
  1069. })('a[name=showLevel]');
  1070. });