file_controller.js 16 KB

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