file_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 moveFiling(ctx) {
  101. try {
  102. const data = JSON.parse(ctx.request.body.data);
  103. if (!data.id || !(data.tree_order >= 0)) throw '数据错误';
  104. const result = await ctx.service.filing.move(data);
  105. ctx.body = { err: 0, msg: '', data: result };
  106. } catch (err) {
  107. ctx.log(err);
  108. ctx.ajaxErrorBody(err, '移动分类失败');
  109. }
  110. }
  111. async loadFile(ctx) {
  112. try {
  113. const data = JSON.parse(ctx.request.body.data);
  114. const result = await ctx.service.file.getFiles({
  115. where: { filing_id: data.filing_id, is_deleted: 0 },
  116. limit: data.count,
  117. offset: (data.page-1)*data.count
  118. });
  119. ctx.body = { err: 0, msg: '', data: result };
  120. } catch (err) {
  121. ctx.log(err);
  122. ctx.ajaxErrorBody(err, '加载文件失败');
  123. }
  124. }
  125. async checkCanUpload(ctx) {
  126. if (ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) < 0) {
  127. throw '您无权上传、导入、删除文件';
  128. }
  129. }
  130. async checkFiling(filing) {
  131. const child = await this.ctx.service.filing.getDataByCondition({ tree_pid: filing.id, is_deleted: 0 });
  132. if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
  133. }
  134. async uploadFile(ctx){
  135. let stream;
  136. try {
  137. await this.checkCanUpload(ctx);
  138. const parts = ctx.multipart({ autoFields: true });
  139. let index = 0;
  140. const create_time = Date.parse(new Date()) / 1000;
  141. let stream = await parts();
  142. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  143. const filing = await ctx.service.filing.getDataById(parts.field.filing_id);
  144. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  145. await this.checkFiling(filing);
  146. const uploadfiles = [];
  147. while (stream !== undefined) {
  148. if (!stream.filename) throw '未发现上传文件!';
  149. const fileInfo = path.parse(stream.filename);
  150. const filepath = `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  151. // 保存文件
  152. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  153. await sendToWormhole(stream);
  154. // 插入到stage_pay对应的附件列表中
  155. uploadfiles.push({
  156. filename: fileInfo.name,
  157. fileext: fileInfo.ext,
  158. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  159. filepath,
  160. });
  161. ++index;
  162. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  163. stream = await parts();
  164. } else {
  165. stream = undefined;
  166. }
  167. }
  168. const result = await ctx.service.file.addFiles(filing, uploadfiles, user);
  169. ctx.body = {err: 0, msg: '', data: result };
  170. } catch (error) {
  171. ctx.helper.log(error);
  172. // 失败需要消耗掉stream 以防卡死
  173. if (stream) await sendToWormhole(stream);
  174. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  175. }
  176. }
  177. async delFile(ctx) {
  178. try{
  179. const data = JSON.parse(ctx.request.body.data);
  180. if (!data.del) throw '缺少参数';
  181. const result = await ctx.service.file.delFiles(data.del);
  182. ctx.body = { err: 0, msg: '', data: result };
  183. } catch(error) {
  184. this.log(error);
  185. ctx.ajaxErrorBody(error, '删除附件失败');
  186. }
  187. }
  188. async loadValidRelaTender(ctx) {
  189. try {
  190. const data = JSON.parse(ctx.request.body.data);
  191. if (data.type) throw '参数错误';
  192. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  193. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  194. ? JSON.parse(accountInfo.permission) : null;
  195. const tenderList = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin);
  196. const rela_tender = await ctx.subProject.rela_tender.split(',');
  197. const result = tenderList.filter(x => { return rela_tender.indexOf(x.id + '') >= 0});
  198. for (const r of result) {
  199. r.advance = await ctx.service.advance.getAllDataByCondition({ columns: ['id', 'order', 'type'], where: { tid: r.id }});
  200. r.advance.forEach(a => {
  201. const type = advanceConst.typeCol.find(x => { return x.type === a.type });
  202. if (type) a.type_str = type.name;
  203. });
  204. r.stage = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 'order'], where: { tid: r.id, status: auditConst.stage.status.checked } });
  205. }
  206. ctx.body = {err: 0, msg: '', data: result };
  207. } catch (error) {
  208. ctx.helper.log(error);
  209. ctx.body = this.ajaxErrorBody(error, '加载标段信息失败');
  210. }
  211. }
  212. async _loadLedgerAtt(data) {
  213. if (!data.tender_id) throw '参数错误';
  214. return await this.ctx.service.ledgerAtt.getAllDataByCondition({ where: { tid: data.tender_id }, order: [['id', 'desc']]});
  215. }
  216. async _loadStageAtt(data) {
  217. if (!data.tender_id || !data.stage) throw '参数错误';
  218. switch (data.sub_type) {
  219. case 'att':
  220. const stage = await this.ctx.service.stage.getDataById(data.stage);
  221. return await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: data.tender_id, sid: stage.order }, order: [['id', 'desc']]});
  222. }
  223. }
  224. async _loadAdvanceAtt(data) {
  225. if (!data.stage) throw '参数错误';
  226. return await this.ctx.service.advanceFile.getAllDataByCondition({ where: { vid: data.stage }, order: [['id', 'desc']]});
  227. }
  228. async loadRelaFiles(ctx) {
  229. try {
  230. const data = JSON.parse(ctx.request.body.data);
  231. console.log(data);
  232. if (!data.type) throw '参数错误';
  233. let files;
  234. switch(data.type) {
  235. case 'ledger':
  236. files = await this._loadLedgerAtt(data);
  237. break;
  238. case 'stage':
  239. files = await this._loadStageAtt(data);
  240. break;
  241. case 'advance':
  242. files = await this._loadAdvanceAtt(data);
  243. break;
  244. default: throw '未知文件类型';
  245. }
  246. ctx.body = {err: 0, msg: '', data: files };
  247. } catch (error) {
  248. ctx.helper.log(error);
  249. ctx.body = this.ajaxErrorBody(error, '加载附件失败,请重试');
  250. }
  251. }
  252. async relaFile(ctx) {
  253. try {
  254. const data = JSON.parse(ctx.request.body.data);
  255. if (!data.filing_id || !data.files) throw '缺少参数';
  256. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  257. const filing = await ctx.service.filing.getDataById(data.filing_id);
  258. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  259. await this.checkFiling(filing);
  260. const result = await ctx.service.file.relaFiles(filing, data.files, user);
  261. ctx.body = {err: 0, msg: '', data: result };
  262. } catch (error) {
  263. ctx.helper.log(error);
  264. ctx.body = this.ajaxErrorBody(error, '导入附件失败,请重试');
  265. }
  266. }
  267. }
  268. return BudgetController;
  269. };