file_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. const renderData = {
  24. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.index),
  25. auditConst,
  26. };
  27. renderData.projectList = await ctx.service.subProject.getFileProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin);
  28. renderData.tenderList = await ctx.service.tender.getList4Select('stage');
  29. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  30. await this.layout('file/index.ejs', renderData, 'file/modal.ejs');
  31. } catch (err) {
  32. ctx.log(err);
  33. }
  34. }
  35. async file(ctx) {
  36. try {
  37. const renderData = {
  38. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.file.file),
  39. };
  40. renderData.filing = await ctx.service.filing.getValidFiling(ctx.params.id, ctx.subProject.permission.filing_type);
  41. renderData.categoryData = await ctx.service.category.getAllCategory(ctx.session.sessionProject.id);
  42. renderData.canFiling = ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.filing.value) >= 0;
  43. renderData.canUpload = ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) >= 0;
  44. renderData.filingTypes = ctx.service.filing.filingType;
  45. await this.layout('file/file.ejs', renderData, 'file/file_modal.ejs');
  46. } catch (err) {
  47. ctx.log(err);
  48. }
  49. }
  50. async getFilingTypePermission(ctx) {
  51. try {
  52. if (ctx.subProject.project_id !== this.ctx.session.sessionProject.id) throw '您无权操作该数据';
  53. const filingType = await ctx.service.subProjPermission.getFilingType(ctx.subProject.id);
  54. ctx.body = { err: 0, msg: '', data: filingType };
  55. } catch(err) {
  56. ctx.log(err);
  57. ctx.ajaxErrorBody(err, '获取授权用户数据错误');
  58. }
  59. }
  60. async saveFilingTypePermission(ctx) {
  61. try {
  62. const data = JSON.parse(ctx.request.body.data);
  63. await ctx.service.subProjPermission.saveFilingType(data);
  64. ctx.body = { err: 0, msg: '', data: '' };
  65. } catch(err) {
  66. ctx.log(err);
  67. ctx.ajaxErrorBody(err, '保存授权用户信息错误');
  68. }
  69. }
  70. async addFiling(ctx) {
  71. try {
  72. const data = JSON.parse(ctx.request.body.data);
  73. const result = await ctx.service.filing.add(data);
  74. ctx.body = { err: 0, msg: '', data: result };
  75. } catch (err) {
  76. ctx.log(err);
  77. ctx.ajaxErrorBody(err, '新增分类失败');
  78. }
  79. }
  80. async delFiling(ctx) {
  81. try {
  82. const data = JSON.parse(ctx.request.body.data);
  83. const result = await ctx.service.filing.del(data);
  84. ctx.body = { err: 0, msg: '', data: result };
  85. } catch (err) {
  86. ctx.log(err);
  87. ctx.ajaxErrorBody(err, '删除分类失败');
  88. }
  89. }
  90. async saveFiling(ctx) {
  91. try {
  92. const data = JSON.parse(ctx.request.body.data);
  93. const result = await ctx.service.filing.save(data);
  94. ctx.body = { err: 0, msg: '', data: result };
  95. } catch (err) {
  96. ctx.log(err);
  97. ctx.ajaxErrorBody(err, '保存分类数据失败');
  98. }
  99. }
  100. async loadFile(ctx) {
  101. try {
  102. const data = JSON.parse(ctx.request.body.data);
  103. const result = await ctx.service.file.getFiles({
  104. where: { filing_id: data.filing_id, is_deleted: 0 },
  105. limit: data.count,
  106. offset: (data.page-1)*data.count
  107. });
  108. ctx.body = { err: 0, msg: '', data: result };
  109. } catch (err) {
  110. ctx.log(err);
  111. ctx.ajaxErrorBody(err, '加载文件失败');
  112. }
  113. }
  114. async checkCanUpload(ctx) {
  115. if (ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) < 0) {
  116. throw '您无权上传、导入、删除文件';
  117. }
  118. }
  119. async checkFiling(filing) {
  120. const child = await this.ctx.service.filing.getDataByCondition({ tree_pid: filing.id, is_deleted: 0 });
  121. if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
  122. }
  123. async uploadFile(ctx){
  124. let stream;
  125. try {
  126. await this.checkCanUpload(ctx);
  127. const parts = ctx.multipart({ autoFields: true });
  128. let index = 0;
  129. const create_time = Date.parse(new Date()) / 1000;
  130. let stream = await parts();
  131. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  132. const filing = await ctx.service.filing.getDataById(parts.field.filing_id);
  133. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  134. await this.checkFiling(filing);
  135. const uploadfiles = [];
  136. while (stream !== undefined) {
  137. if (!stream.filename) throw '未发现上传文件!';
  138. const fileInfo = path.parse(stream.filename);
  139. const filepath = `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  140. // 保存文件
  141. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  142. await sendToWormhole(stream);
  143. // 插入到stage_pay对应的附件列表中
  144. uploadfiles.push({
  145. filename: fileInfo.name,
  146. fileext: fileInfo.ext,
  147. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  148. filepath,
  149. });
  150. ++index;
  151. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  152. stream = await parts();
  153. } else {
  154. stream = undefined;
  155. }
  156. }
  157. const result = await ctx.service.file.addFiles(filing, uploadfiles, user);
  158. ctx.body = {err: 0, msg: '', data: result };
  159. } catch (error) {
  160. ctx.helper.log(error);
  161. // 失败需要消耗掉stream 以防卡死
  162. if (stream) await sendToWormhole(stream);
  163. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  164. }
  165. }
  166. async delFile(ctx) {
  167. try{
  168. const data = JSON.parse(ctx.request.body.data);
  169. if (!data.del) throw '缺少参数';
  170. const result = await ctx.service.file.delFiles(data.del);
  171. ctx.body = { err: 0, msg: '', data: result };
  172. } catch(error) {
  173. this.log(error);
  174. ctx.ajaxErrorBody(error, '删除附件失败');
  175. }
  176. }
  177. async loadValidRelaTender(ctx) {
  178. try {
  179. const data = JSON.parse(ctx.request.body.data);
  180. if (data.type) throw '参数错误';
  181. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  182. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  183. ? JSON.parse(accountInfo.permission) : null;
  184. const tenderList = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin);
  185. const rela_tender = await ctx.subProject.rela_tender.split(',');
  186. const result = tenderList.filter(x => { return rela_tender.indexOf(x.id + '') >= 0});
  187. for (const r of result) {
  188. r.advance = await ctx.service.advance.getAllDataByCondition({ columns: ['id', 'order', 'type'], where: { tid: r.id }});
  189. r.advance.forEach(a => {
  190. const type = advanceConst.typeCol.find(x => { return x.type === a.type });
  191. if (type) a.type_str = type.name;
  192. });
  193. r.stage = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 'order'], where: { tid: r.id, status: auditConst.stage.status.checked } });
  194. }
  195. ctx.body = {err: 0, msg: '', data: result };
  196. } catch (error) {
  197. ctx.helper.log(error);
  198. ctx.body = this.ajaxErrorBody(error, '加载标段信息失败');
  199. }
  200. }
  201. async _loadLedgerAtt(data) {
  202. if (!data.tender_id) throw '参数错误';
  203. return await this.ctx.service.ledgerAtt.getAllDataByCondition({ where: { tid: data.tender_id }, order: [['id', 'desc']]});
  204. }
  205. async _loadStageAtt(data) {
  206. if (!data.tender_id || !data.stage) throw '参数错误';
  207. switch (data.sub_type) {
  208. case 'att':
  209. const stage = await this.ctx.service.stage.getDataById(data.stage);
  210. return await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: data.tender_id, sid: stage.order }, order: [['id', 'desc']]});
  211. }
  212. }
  213. async _loadAdvanceAtt(data) {
  214. if (!data.stage) throw '参数错误';
  215. return await this.ctx.service.advanceFile.getAllDataByCondition({ where: { vid: data.stage }, order: [['id', 'desc']]});
  216. }
  217. async loadRelaFiles(ctx) {
  218. try {
  219. const data = JSON.parse(ctx.request.body.data);
  220. console.log(data);
  221. if (!data.type) throw '参数错误';
  222. let files;
  223. switch(data.type) {
  224. case 'ledger':
  225. files = await this._loadLedgerAtt(data);
  226. break;
  227. case 'stage':
  228. files = await this._loadStageAtt(data);
  229. break;
  230. case 'advance':
  231. files = await this._loadAdvanceAtt(data);
  232. break;
  233. default: throw '未知文件类型';
  234. }
  235. ctx.body = {err: 0, msg: '', data: files };
  236. } catch (error) {
  237. ctx.helper.log(error);
  238. ctx.body = this.ajaxErrorBody(error, '加载附件失败,请重试');
  239. }
  240. }
  241. async relaFile(ctx) {
  242. try {
  243. const data = JSON.parse(ctx.request.body.data);
  244. if (!data.filing_id || !data.files) throw '缺少参数';
  245. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  246. const filing = await ctx.service.filing.getDataById(data.filing_id);
  247. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  248. await this.checkFiling(filing);
  249. const result = await ctx.service.file.relaFiles(filing, data.files, user);
  250. ctx.body = {err: 0, msg: '', data: result };
  251. } catch (error) {
  252. ctx.helper.log(error);
  253. ctx.body = this.ajaxErrorBody(error, '导入附件失败,请重试');
  254. }
  255. }
  256. }
  257. return BudgetController;
  258. };