file_controller.js 21 KB

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