file_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 order = data.order.split('|');
  120. if (order.length !== 2) throw '加载文件错误';
  121. if (order[0] !== 'filename' && order[0] !== 'create_time') throw '加载文件错误';
  122. if (order[1] !== 'asc' && order[1] !== 'desc') throw '加载文件错误';
  123. const result = await ctx.service.file.getFiles({
  124. where: { filing_id: data.filing_id, is_deleted: 0 },
  125. orders: [order],
  126. limit: data.count,
  127. offset: (data.page-1)*data.count,
  128. }, order);
  129. ctx.body = { err: 0, msg: '', data: result };
  130. } catch (err) {
  131. ctx.log(err);
  132. ctx.ajaxErrorBody(err, '加载文件失败');
  133. }
  134. }
  135. async checkCanUpload(ctx) {
  136. if (ctx.subProject.permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) < 0) {
  137. throw '您无权上传、导入、删除文件';
  138. }
  139. }
  140. async checkFiling(filing) {
  141. const child = await this.ctx.service.filing.getDataByCondition({ tree_pid: filing.id, is_deleted: 0 });
  142. if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
  143. }
  144. async uploadFile(ctx){
  145. let stream;
  146. try {
  147. await this.checkCanUpload(ctx);
  148. const parts = ctx.multipart({ autoFields: true });
  149. let index = 0;
  150. const create_time = Date.parse(new Date()) / 1000;
  151. let stream = await parts();
  152. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  153. const filing = await ctx.service.filing.getDataById(parts.field.filing_id);
  154. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  155. await this.checkFiling(filing);
  156. const uploadfiles = [];
  157. while (stream !== undefined) {
  158. if (!stream.filename) throw '未发现上传文件!';
  159. const fileInfo = path.parse(stream.filename);
  160. const filepath = `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  161. // 保存文件
  162. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  163. await sendToWormhole(stream);
  164. // 插入到stage_pay对应的附件列表中
  165. uploadfiles.push({
  166. filename: fileInfo.name,
  167. fileext: fileInfo.ext,
  168. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  169. filepath,
  170. });
  171. ++index;
  172. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  173. stream = await parts();
  174. } else {
  175. stream = undefined;
  176. }
  177. }
  178. const result = await ctx.service.file.addFiles(filing, uploadfiles, user);
  179. ctx.body = {err: 0, msg: '', data: result };
  180. } catch (error) {
  181. ctx.helper.log(error);
  182. // 失败需要消耗掉stream 以防卡死
  183. if (stream) await sendToWormhole(stream);
  184. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  185. }
  186. }
  187. async delFile(ctx) {
  188. try{
  189. const data = JSON.parse(ctx.request.body.data);
  190. if (!data.del) throw '缺少参数';
  191. const result = await ctx.service.file.delFiles(data.del);
  192. ctx.body = { err: 0, msg: '', data: result };
  193. } catch(error) {
  194. this.log(error);
  195. ctx.ajaxErrorBody(error, '删除附件失败');
  196. }
  197. }
  198. async saveFile(ctx) {
  199. try {
  200. const data = JSON.parse(ctx.request.body.data);
  201. if (!data.id) throw '缺少参数';
  202. const result = await ctx.service.file.saveFile(data.id, data.filename);
  203. ctx.body = { err: 0, msg: '', data: result };
  204. } catch (error) {
  205. this.log(error);
  206. ctx.ajaxErrorBody(error, '编辑附件失败');
  207. }
  208. }
  209. async uploadBigFile(ctx) {
  210. try {
  211. await this.checkCanUpload(ctx);
  212. const data = JSON.parse(ctx.request.body.data);
  213. if (!data.type || !data.filing_id || !data.fileInfo) throw '缺少参数';
  214. const filing = await ctx.service.filing.getDataById(data.filing_id);
  215. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  216. let result;
  217. const fileInfo = path.parse(data.fileInfo.filename);
  218. switch(data.type) {
  219. case 'begin':
  220. const create_time = Date.parse(new Date()) / 1000;
  221. result = {
  222. filename: `sp/file/${filing.spid}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + fileInfo.ext}`,
  223. };
  224. result.filepath = ctx.app.config.fujianOssFolder + result.filename;
  225. // todo 写入ossToken
  226. result.oss = await ctx.helper.getOssToken(ctx.app.fujianOss);
  227. break;
  228. case 'end':
  229. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  230. const uploadFiles = [{
  231. filepath: data.filepath,
  232. filename: fileInfo.name, fileext: fileInfo.ext, filesize: data.fileInfo.filesize,
  233. }];
  234. result = await ctx.service.file.addFiles(filing, uploadFiles, user);
  235. break;
  236. }
  237. ctx.body = {err: 0, msg: '', data: result };
  238. } catch (error) {
  239. ctx.log(error);
  240. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  241. }
  242. }
  243. async loadValidRelaTender(ctx) {
  244. try {
  245. const data = JSON.parse(ctx.request.body.data);
  246. if (data.type) throw '参数错误';
  247. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  248. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  249. ? JSON.parse(accountInfo.permission) : null;
  250. const tenderList = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin);
  251. const rela_tender = await ctx.subProject.rela_tender.split(',');
  252. const result = tenderList.filter(x => { return rela_tender.indexOf(x.id + '') >= 0});
  253. for (const r of result) {
  254. r.advance = await ctx.service.advance.getAllDataByCondition({ columns: ['id', 'order', 'type'], where: { tid: r.id }});
  255. r.advance.forEach(a => {
  256. const type = advanceConst.typeCol.find(x => { return x.type === a.type });
  257. if (type) a.type_str = type.name;
  258. });
  259. r.stage = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 'order'], where: { tid: r.id, status: auditConst.stage.status.checked } });
  260. }
  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. async _loadLedgerAtt(data) {
  268. if (!data.tender_id) throw '参数错误';
  269. return await this.ctx.service.ledgerAtt.getAllDataByCondition({ where: { tid: data.tender_id }, order: [['id', 'desc']]});
  270. }
  271. async _loadStageAtt(data) {
  272. if (!data.tender_id || !data.stage) throw '参数错误';
  273. switch (data.sub_type) {
  274. case 'att':
  275. const stage = await this.ctx.service.stage.getDataById(data.stage);
  276. return await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: data.tender_id, sid: stage.order }, order: [['id', 'desc']]});
  277. }
  278. }
  279. async _loadAdvanceAtt(data) {
  280. if (!data.stage) throw '参数错误';
  281. const self = this;
  282. const result = await this.ctx.service.advanceFile.getAllDataByCondition({ where: { vid: data.stage }, order: [['id', 'desc']]});
  283. result.forEach(x => {
  284. const info = path.parse(x.filename);
  285. x.filename = info.name;
  286. x.filesize = self.ctx.helper.sizeToBytes(x.filesize);
  287. });
  288. return result;
  289. }
  290. async loadRelaFiles(ctx) {
  291. try {
  292. const data = JSON.parse(ctx.request.body.data);
  293. if (!data.type) throw '参数错误';
  294. let files;
  295. switch(data.type) {
  296. case 'ledger':
  297. files = await this._loadLedgerAtt(data);
  298. break;
  299. case 'stage':
  300. files = await this._loadStageAtt(data);
  301. break;
  302. case 'advance':
  303. files = await this._loadAdvanceAtt(data);
  304. break;
  305. default: throw '未知文件类型';
  306. }
  307. ctx.body = {err: 0, msg: '', data: files };
  308. } catch (error) {
  309. ctx.helper.log(error);
  310. ctx.body = this.ajaxErrorBody(error, '加载附件失败,请重试');
  311. }
  312. }
  313. async relaFile(ctx) {
  314. try {
  315. const data = JSON.parse(ctx.request.body.data);
  316. if (!data.filing_id || !data.files) throw '缺少参数';
  317. const user = await ctx. service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  318. const filing = await ctx.service.filing.getDataById(data.filing_id);
  319. if (!filing || filing.is_deleted) throw '分类不存在,请刷新页面后重试';
  320. await this.checkFiling(filing);
  321. const result = await ctx.service.file.relaFiles(filing, data.files, user);
  322. ctx.body = {err: 0, msg: '', data: result };
  323. } catch (error) {
  324. ctx.helper.log(error);
  325. ctx.body = this.ajaxErrorBody(error, '导入附件失败,请重试');
  326. }
  327. }
  328. }
  329. return BudgetController;
  330. };