file_controller.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2021/10/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const sendToWormhole = require('stream-wormhole');
  11. const path = require('path');
  12. const advanceConst = require('../const/advance');
  13. module.exports = app => {
  14. class FileController extends app.BaseController {
  15. checkUnlock(ctx) {
  16. if (ctx.subProject.lock_file) throw '管理员锁定中,暂无法编辑分类&文件,仅可查看';
  17. }
  18. checkLock(ctx) {
  19. if (!ctx.subProject.lock_file) throw '请先锁定,再管理分类数据';
  20. }
  21. /**
  22. * 概算投资
  23. *
  24. * @param ctx
  25. * @returns {Promise<void>}
  26. */
  27. async index(ctx) {
  28. try {
  29. if (!ctx.session.sessionProject.page_show.openFile) {
  30. throw '该功能已关闭或无法查看';
  31. }
  32. const renderData = {
  33. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.index),
  34. auditConst,
  35. };
  36. renderData.projectList = await ctx.service.subProject.getFileProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin);
  37. for (const p of renderData.projectList) {
  38. if (!p.is_folder) p.file_count = await this.service.filing.sumFileCount(p.id);
  39. }
  40. renderData.tenderList = await ctx.service.tender.getList4Select('stage');
  41. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  42. await this.layout('file/index.ejs', renderData, 'file/modal.ejs');
  43. } catch (err) {
  44. ctx.log(err);
  45. ctx.session.postError = err.toString();
  46. ctx.redirect(this.menu.menu.dashboard.url);
  47. }
  48. }
  49. async file(ctx) {
  50. try {
  51. const renderData = {
  52. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.file),
  53. };
  54. renderData.filing = await ctx.service.filing.getValidFiling(ctx.params.id, ctx.subProject.permission.filing_type);
  55. renderData.categoryData = await ctx.service.category.getAllCategory(ctx.session.sessionProject.id);
  56. renderData.canFiling = ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.filing.value) >= 0;
  57. renderData.canUpload = ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) >= 0;
  58. renderData.filingTypes = ctx.service.filing.analysisFilingType(renderData.filing);
  59. await this.layout('file/file.ejs', renderData, 'file/file_modal.ejs');
  60. } catch (err) {
  61. ctx.log(err);
  62. }
  63. }
  64. async getFilingTypePermission(ctx) {
  65. try {
  66. if (ctx.subProject.project_id !== this.ctx.session.sessionProject.id) throw '您无权操作该数据';
  67. const filingType = await ctx.service.subProjPermission.getFilingType(ctx.subProject.id);
  68. ctx.body = { err: 0, msg: '', data: filingType };
  69. } catch(err) {
  70. ctx.log(err);
  71. ctx.ajaxErrorBody(err, '获取授权用户数据错误');
  72. }
  73. }
  74. async saveFilingTypePermission(ctx) {
  75. try {
  76. const data = JSON.parse(ctx.request.body.data);
  77. await ctx.service.subProjPermission.saveFilingType(data);
  78. ctx.body = { err: 0, msg: '', data: '' };
  79. } catch(err) {
  80. ctx.log(err);
  81. ctx.ajaxErrorBody(err, '保存授权用户信息错误');
  82. }
  83. }
  84. async addFiling(ctx) {
  85. try {
  86. this.checkUnlock(ctx);
  87. const data = JSON.parse(ctx.request.body.data);
  88. const result = await ctx.service.filing.add(data);
  89. ctx.body = { err: 0, msg: '', data: result };
  90. } catch (err) {
  91. ctx.log(err);
  92. ctx.ajaxErrorBody(err, '新增分类失败');
  93. }
  94. }
  95. async delFiling(ctx) {
  96. try {
  97. this.checkUnlock(ctx);
  98. const data = JSON.parse(ctx.request.body.data);
  99. const result = await ctx.service.filing.del(data);
  100. ctx.body = { err: 0, msg: '', data: result };
  101. } catch (err) {
  102. ctx.log(err);
  103. ctx.ajaxErrorBody(err, '删除分类失败');
  104. }
  105. }
  106. async saveFiling(ctx) {
  107. try {
  108. this.checkUnlock(ctx);
  109. const data = JSON.parse(ctx.request.body.data);
  110. const result = await ctx.service.filing.save(data);
  111. ctx.body = { err: 0, msg: '', data: result };
  112. } catch (err) {
  113. ctx.log(err);
  114. ctx.ajaxErrorBody(err, '保存分类数据失败');
  115. }
  116. }
  117. async moveFiling(ctx) {
  118. try {
  119. this.checkUnlock(ctx);
  120. const data = JSON.parse(ctx.request.body.data);
  121. if (!data.id || !(data.tree_order >= 0)) throw '数据错误';
  122. const result = await ctx.service.filing.move(data);
  123. ctx.body = { err: 0, msg: '', data: result };
  124. } catch (err) {
  125. ctx.log(err);
  126. ctx.ajaxErrorBody(err, '移动分类失败');
  127. }
  128. }
  129. async loadFile(ctx) {
  130. try {
  131. const data = JSON.parse(ctx.request.body.data);
  132. const order = data.order.split('|');
  133. if (order.length !== 2) throw '加载文件错误';
  134. if (order[0] !== 'filename' && order[0] !== 'create_time') throw '加载文件错误';
  135. if (order[1] !== 'asc' && order[1] !== 'desc') throw '加载文件错误';
  136. const result = await ctx.service.file.getFiles({
  137. where: { filing_id: data.filing_id, is_deleted: 0 },
  138. orders: [order],
  139. limit: data.count,
  140. offset: (data.page-1)*data.count,
  141. }, order);
  142. ctx.body = { err: 0, msg: '', data: result };
  143. } catch (err) {
  144. ctx.log(err);
  145. ctx.ajaxErrorBody(err, '加载文件失败');
  146. }
  147. }
  148. async checkCanUpload(ctx) {
  149. if (ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) < 0) {
  150. throw '您无权上传、导入、删除文件';
  151. }
  152. }
  153. async checkFiling(filing) {
  154. const child = await this.ctx.service.filing.getDataByCondition({ tree_pid: filing.id, is_deleted: 0 });
  155. if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
  156. }
  157. async checkFiles(ctx) {
  158. try{
  159. const data = JSON.parse(ctx.request.body.data);
  160. if (!data.filing_id || !data.files) throw '缺少参数';
  161. const result = await ctx.service.file.checkFiles(data.filing_id, data.files);
  162. ctx.body = { err: 0, msg: '', data: result };
  163. } catch(error) {
  164. this.log(error);
  165. ctx.ajaxErrorBody(error, '检查附件错误');
  166. }
  167. }
  168. async uploadFile(ctx){
  169. let stream;
  170. try {
  171. await this.checkCanUpload(ctx);
  172. const parts = ctx.multipart({ autoFields: true });
  173. let index = 0;
  174. const create_time = Date.parse(new Date()) / 1000;
  175. let stream = await parts();
  176. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  177. const filing = await ctx.service.filing.getDataById(parts.field.filing_id);
  178. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  179. await this.checkFiling(filing);
  180. const uploadfiles = [];
  181. while (stream !== undefined) {
  182. if (!stream.filename) throw '未发现上传文件!';
  183. const fileInfo = path.parse(stream.filename);
  184. const filepath = `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  185. // 保存文件
  186. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  187. await sendToWormhole(stream);
  188. // 插入到stage_pay对应的附件列表中
  189. uploadfiles.push({
  190. filename: fileInfo.name,
  191. fileext: fileInfo.ext,
  192. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  193. filepath,
  194. });
  195. ++index;
  196. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  197. stream = await parts();
  198. } else {
  199. stream = undefined;
  200. }
  201. }
  202. const result = await ctx.service.file.addFiles(filing, uploadfiles, user);
  203. ctx.body = {err: 0, msg: '', data: result };
  204. } catch (error) {
  205. ctx.helper.log(error);
  206. // 失败需要消耗掉stream 以防卡死
  207. if (stream) await sendToWormhole(stream);
  208. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  209. }
  210. }
  211. async delFile(ctx) {
  212. try{
  213. const data = JSON.parse(ctx.request.body.data);
  214. if (!data.del) throw '缺少参数';
  215. const result = await ctx.service.file.delFiles(data.del);
  216. ctx.body = { err: 0, msg: '', data: result };
  217. } catch(error) {
  218. this.log(error);
  219. ctx.ajaxErrorBody(error, '删除附件失败');
  220. }
  221. }
  222. async saveFile(ctx) {
  223. try {
  224. const data = JSON.parse(ctx.request.body.data);
  225. if (!data.id) throw '缺少参数';
  226. const result = await ctx.service.file.saveFile(data.id, data.filename);
  227. ctx.body = { err: 0, msg: '', data: result };
  228. } catch (error) {
  229. this.log(error);
  230. ctx.ajaxErrorBody(error, '编辑附件失败');
  231. }
  232. }
  233. async moveFile(ctx) {
  234. try {
  235. const data = JSON.parse(ctx.request.body.data);
  236. if (!data.id || !data.filingId) throw '缺少参数';
  237. const result = await ctx.service.file.moveFile(data.id, data.filingId);
  238. ctx.body = { err: 0, msg: '', data: result };
  239. } catch (error) {
  240. this.log(error);
  241. ctx.ajaxErrorBody(error, '编辑附件失败');
  242. }
  243. }
  244. async uploadBigFile(ctx) {
  245. try {
  246. await this.checkCanUpload(ctx);
  247. const data = JSON.parse(ctx.request.body.data);
  248. if (!data.type || !data.filing_id || !data.fileInfo) throw '缺少参数';
  249. const filing = await ctx.service.filing.getDataById(data.filing_id);
  250. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  251. let result;
  252. const fileInfo = path.parse(data.fileInfo.filename);
  253. switch(data.type) {
  254. case 'begin':
  255. const create_time = Date.parse(new Date()) / 1000;
  256. result = {
  257. filename: `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + fileInfo.ext}`,
  258. };
  259. result.filepath = ctx.app.config.fujianOssFolder + result.filename;
  260. // todo 写入ossToken
  261. result.oss = await ctx.helper.getOssToken(ctx.app.fujianOss);
  262. break;
  263. case 'end':
  264. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  265. const uploadFiles = [{
  266. filepath: data.filepath,
  267. filename: fileInfo.name, fileext: fileInfo.ext, filesize: data.fileInfo.filesize,
  268. }];
  269. result = await ctx.service.file.addFiles(filing, uploadFiles, user);
  270. break;
  271. }
  272. ctx.body = {err: 0, msg: '', data: result };
  273. } catch (error) {
  274. ctx.log(error);
  275. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  276. }
  277. }
  278. async loadValidRelaTender(ctx) {
  279. try {
  280. const data = JSON.parse(ctx.request.body.data);
  281. if (data.type) throw '参数错误';
  282. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  283. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  284. ? JSON.parse(accountInfo.permission) : null;
  285. const tenderList = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin);
  286. const rela_tender = await ctx.subProject.rela_tender.split(',');
  287. const result = tenderList.filter(x => { return rela_tender.indexOf(x.id + '') >= 0});
  288. for (const r of result) {
  289. r.advance = await ctx.service.advance.getAllDataByCondition({ columns: ['id', 'order', 'type'], where: { tid: r.id }});
  290. r.advance.forEach(a => {
  291. const type = advanceConst.typeCol.find(x => { return x.type === a.type });
  292. if (type) a.type_str = type.name;
  293. });
  294. r.stage = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 'order'], where: { tid: r.id, status: auditConst.stage.status.checked } });
  295. }
  296. ctx.body = {err: 0, msg: '', data: result };
  297. } catch (error) {
  298. ctx.helper.log(error);
  299. ctx.body = this.ajaxErrorBody(error, '加载标段信息失败');
  300. }
  301. }
  302. async _loadLedgerAtt(data) {
  303. if (!data.tender_id) throw '参数错误';
  304. return await this.ctx.service.ledgerAtt.getAllDataByCondition({ where: { tid: data.tender_id }, order: [['id', 'desc']]});
  305. }
  306. async _loadStageAtt(data) {
  307. if (!data.tender_id || !data.stage) throw '参数错误';
  308. switch (data.sub_type) {
  309. case 'att':
  310. const stage = await this.ctx.service.stage.getDataById(data.stage);
  311. return await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: data.tender_id, sid: stage.order }, order: [['id', 'desc']]});
  312. }
  313. }
  314. async _loadAdvanceAtt(data) {
  315. if (!data.stage) throw '参数错误';
  316. const self = this;
  317. const result = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { vid: data.stage }, order: [['id', 'desc']]});
  318. result.forEach(x => {
  319. const info = path.parse(x.filename);
  320. x.filename = info.name;
  321. x.filesize = self.ctx.helper.sizeToBytes(x.filesize);
  322. });
  323. return result;
  324. }
  325. async loadRelaFiles(ctx) {
  326. try {
  327. const data = JSON.parse(ctx.request.body.data);
  328. if (!data.type) throw '参数错误';
  329. let files;
  330. switch(data.type) {
  331. case 'ledger':
  332. files = await this._loadLedgerAtt(data);
  333. break;
  334. case 'stage':
  335. files = await this._loadStageAtt(data);
  336. break;
  337. case 'advance':
  338. files = await this._loadAdvanceAtt(data);
  339. break;
  340. default: throw '未知文件类型';
  341. }
  342. ctx.body = {err: 0, msg: '', data: files };
  343. } catch (error) {
  344. ctx.helper.log(error);
  345. ctx.body = this.ajaxErrorBody(error, '加载附件失败,请重试');
  346. }
  347. }
  348. async relaFile(ctx) {
  349. try {
  350. const data = JSON.parse(ctx.request.body.data);
  351. if (!data.filing_id || !data.files) throw '缺少参数';
  352. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  353. const filing = await ctx.service.filing.getDataById(data.filing_id);
  354. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  355. await this.checkFiling(filing);
  356. const result = await ctx.service.file.relaFiles(filing, data.files, user);
  357. ctx.body = {err: 0, msg: '', data: result };
  358. } catch (error) {
  359. ctx.helper.log(error);
  360. ctx.body = this.ajaxErrorBody(error, '导入附件失败,请重试');
  361. }
  362. }
  363. async template(ctx) {
  364. const defaultTemplate = await ctx.service.filingTemplateList.getOriginTemplate();
  365. ctx.redirect('/file/template/' + defaultTemplate.id);
  366. }
  367. async templateDetail(ctx) {
  368. try {
  369. const renderData = {
  370. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.template),
  371. };
  372. renderData.templateList = await ctx.service.filingTemplateList.getAllTemplate(ctx.session.sessionProject.id);
  373. renderData.FtType = ctx.service.filingTemplateList.FtType;
  374. renderData.template = renderData.templateList.find(x => { return x.id === ctx.params.id });
  375. if (!renderData.template) throw '查看的资料模板不存在';
  376. renderData.templateData = await ctx.service.filingTemplate.getData(renderData.template.id);
  377. await this.layout('file/template.ejs', renderData, 'file/template_modal.ejs');
  378. } catch (err) {
  379. ctx.log(err);
  380. ctx.session.postError = err.toString();
  381. ctx.redirect(this.menu.menu.dashboard.url);
  382. }
  383. }
  384. async saveTemplate(ctx) {
  385. try {
  386. const id = ctx.query.id;
  387. const name = ctx.request.body.name;
  388. const [save, templateId] = await ctx.service.filingTemplateList.save(name, id);
  389. if (!save) throw '保存数据失败';
  390. ctx.redirect('/file/template/' + templateId);
  391. } catch(err) {
  392. ctx.log(err);
  393. ctx.session.postError = err.toString();
  394. ctx.redirect('/file/template');
  395. }
  396. }
  397. async resetTemplate(ctx) {
  398. try {
  399. const id = ctx.query.id;
  400. await ctx.service.filingTemplateList.reset(id);
  401. ctx.redirect('/file/template/' + id);
  402. } catch (err) {
  403. ctx.log(err);
  404. ctx.postError(err, '重置模板失败');
  405. ctx.redirect('/file/template');
  406. }
  407. }
  408. async delTemplate(ctx) {
  409. try {
  410. const id = ctx.query.id;
  411. await ctx.service.filingTemplateList.delete(id);
  412. console.log(ctx.request.headers.referer, id);
  413. if (ctx.request.headers.referer.indexOf(id) > 0) {
  414. ctx.redirect('/file/template');
  415. } else {
  416. ctx.redirect(ctx.request.headers.referer);
  417. }
  418. } catch (err) {
  419. ctx.log(err);
  420. ctx.postError(err, '删除模板失败');
  421. ctx.redirect('/file/template');
  422. }
  423. }
  424. async updateTemplate(ctx) {
  425. try {
  426. const data = JSON.parse(ctx.request.body.data);
  427. if (!data.updateType) throw '数据错误';
  428. let result;
  429. if (data.updateType === 'add') {
  430. result = await ctx.service.filingTemplate.add(ctx.params.id, data);
  431. } else if (data.updateType === 'del') {
  432. result = await ctx.service.filingTemplate.del(ctx.params.id, data);
  433. } else if (data.updateType === 'save') {
  434. result = await ctx.service.filingTemplate.save(data);
  435. } else if (data.updateType === 'move') {
  436. if (!data.id || !(data.tree_order >= 0)) throw '数据错误';
  437. result = await ctx.service.filingTemplate.move(ctx.params.id, data);
  438. } else if (data.updateType === 'import') {
  439. result = await ctx.service.filingTemplate.import(ctx.params.id, data.data);
  440. } else if (data.updateType === 'multi' ) {
  441. result = await ctx.service.filingTemplate.multiUpdate(ctx.params.id, data.data);
  442. }
  443. ctx.body = { err: 0, msg: '', data: result };
  444. } catch (err) {
  445. ctx.log(err);
  446. ctx.ajaxErrorBody(err, '修改失败');
  447. }
  448. }
  449. async search(ctx) {
  450. try {
  451. const limit = 1000;
  452. const data = JSON.parse(ctx.request.body.data);
  453. if (!data.filing_type || !data.keyword) throw '数据错误';
  454. const validFilingType = [];
  455. for (const f of data.filing_type) {
  456. if (ctx.subProject.permission.filing_type === 'all' || ctx.subProject.permission.filing_type.indexOf(f) >= 0) validFilingType.push(f);
  457. }
  458. const result = await ctx.service.file.search(validFilingType, data.keyword, limit);
  459. ctx.body = { err: 0, msg: '', data: { list: result, limit } };
  460. } catch(err) {
  461. ctx.log(err);
  462. ctx.ajaxErrorBody(err, '搜索文件失败');
  463. }
  464. }
  465. async manage(ctx) {
  466. try {
  467. const renderData = {
  468. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.manage),
  469. };
  470. renderData.filingData = await ctx.service.filing.getValidFiling(ctx.params.id, ctx.subProject.permission.filing_type);
  471. await this.layout('file/manage.ejs', renderData, 'file/manage_modal.ejs');
  472. } catch (err) {
  473. ctx.log(err);
  474. ctx.session.postError = err.toString();
  475. ctx.redirect(this.menu.menu.dashboard.url);
  476. }
  477. }
  478. async lockFiling(ctx) {
  479. try {
  480. await ctx.service.subProject.save({ id: ctx.subProject.id, lock_file: ctx.query.lock });
  481. ctx.redirect(`/sp/${ctx.subProject.id}/fm`);
  482. } catch(err) {
  483. ctx.log(err);
  484. ctx.postError(err, '资料归集分类锁定错误');
  485. ctx.redirect(`/sp/${ctx.subProject.id}/fm`);
  486. }
  487. }
  488. async manageUpdate(ctx) {
  489. try {
  490. const data = JSON.parse(ctx.request.body.data);
  491. if (!data.updateType) throw '数据错误';
  492. let result;
  493. const updateData = JSON.parse(JSON.stringify(data));
  494. delete updateData.updateType;
  495. if (data.updateType === 'add') {
  496. result = await ctx.service.filing.add(updateData);
  497. } else if (data.updateType === 'del') {
  498. result = await ctx.service.filing.del(updateData);
  499. } else if (data.updateType === 'save') {
  500. result = await ctx.service.filing.save(updateData);
  501. } else if (data.updateType === 'move') {
  502. if (!data.id || !(data.tree_order >= 0)) throw '数据错误';
  503. result = await ctx.service.filing.move(updateData);
  504. } else if (data.updateType === 'multi' ) {
  505. result = await ctx.service.filing.multiUpdate(ctx.subProject.id, data.data);
  506. }
  507. ctx.body = { err: 0, msg: '', data: result };
  508. } catch (err) {
  509. ctx.log(err);
  510. ctx.ajaxErrorBody(err, '修改失败');
  511. }
  512. }
  513. }
  514. return FileController;
  515. };