dashboard_controller.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. 'use strict';
  2. /**
  3. * 控制面板
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/23
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const officeList = require('../const/cld_office').list;
  11. const maintainConst = require('../const/maintain');
  12. const typeColMap = require('../const/advance').typeColMap;
  13. const moment = require('moment');
  14. const fs = require('fs');
  15. const path = require('path');
  16. const sendToWormhole = require('stream-wormhole');
  17. module.exports = app => {
  18. class DashboardController extends app.BaseController {
  19. /**
  20. * 控制面板页面
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. async workspace(ctx) {
  26. const allAuditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);
  27. const allAuditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);
  28. const allAuditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);
  29. const allAuditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId);
  30. const allAuditMaterial = await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId);
  31. const allAuditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId);
  32. const allAuditChangeProject = await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId);
  33. const allAuditChangeApply = await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId);
  34. const allAuditChangePlan = await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId);
  35. const allAuditPayments = await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId);
  36. const allAuditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId);
  37. const allAuditFinancials = await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId);
  38. const allAuditInspections = await ctx.service.qualityInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId);
  39. const allAuditSafeInspections = await ctx.service.safeInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId);
  40. const allAuditSafeStage = await ctx.service.safeStageAudit.getAuditStage(ctx.session.sessionUser.accountId);
  41. const allAuditPhasePay = await ctx.service.phasePayAudit.getAuditStage(ctx.session.sessionUser.accountId);
  42. const allAuditCostStageLedger = await ctx.service.costStageAudit.getAuditStage('ledger', ctx.session.sessionUser.accountId);
  43. const allAuditCostStageBook = await ctx.service.costStageAudit.getAuditStage('book', ctx.session.sessionUser.accountId);
  44. const allAuditCostStageAnalysis = await ctx.service.costStageAudit.getAuditStage('analysis', ctx.session.sessionUser.accountId);
  45. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  46. const auditShenpiTenders = await ctx.service.ledgerAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  47. const auditShenpiStages = await ctx.service.stageAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  48. const auditShenpiChanges = await ctx.service.changeAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  49. const auditShenpiRevise = await ctx.service.reviseAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  50. const auditShenpiMaterial = await ctx.service.materialAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  51. const auditShenpiAdvance = await ctx.service.advanceAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  52. const auditShenpiChangeProject = await ctx.service.changeProjectAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  53. const auditShenpiChangeApply = await ctx.service.changeApplyAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  54. const auditShenpiChangePlan = await ctx.service.changePlanAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  55. const auditShenpiPayment = await ctx.service.paymentDetailAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  56. const auditShenpiFinancial = await ctx.service.financialPayAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  57. const auditShenpiInspection = await ctx.service.qualityInspectionAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  58. const auditShenpiSafeInspection = await ctx.service.safeInspectionAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  59. const auditShenpiSafeStage = await ctx.service.safeStageAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  60. const auditShenpiPhasePay = await ctx.service.phasePayAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  61. const auditShenpiCostStageLedger = await ctx.service.costStageAudit.getDonesByAudit('ledger', ctx.session.sessionUser.accountId);
  62. const auditShenpiCostStageBook = await ctx.service.costStageAudit.getDonesByAudit('book', ctx.session.sessionUser.accountId);
  63. const auditShenpiCostStageAnalysis = await ctx.service.costStageAudit.getDonesByAudit('analysis', ctx.session.sessionUser.accountId);
  64. const dashboardStatus = {
  65. all: 0,
  66. dashboard: 0,
  67. worry: 0,
  68. early: 0,
  69. shenpi: {
  70. ledger: 0,
  71. stage: 0,
  72. change: 0,
  73. revise: 0,
  74. material: 0,
  75. advance: 0,
  76. changeProject: 0,
  77. changeApply: 0,
  78. changePlan: 0,
  79. payment: 0,
  80. financial: 0,
  81. inspection: 0,
  82. safeInspection: 0,
  83. safeStage: 0,
  84. phasePay: 0,
  85. costStageLedger: 0,
  86. costStageBook: 0,
  87. costStageAnalysis: 0,
  88. },
  89. };
  90. let noticeList = [];
  91. // 还要考虑功能关闭不展示对应类型情况
  92. const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true);
  93. for (const subProject of subProjects) {
  94. subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show);
  95. const spTenders = ctx.helper._.filter(auditShenpiTenders, { spid: subProject.id });
  96. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spTenders, 'shenpi_type', 'ledger'));
  97. const spStages = ctx.helper._.filter(auditShenpiStages, { spid: subProject.id });
  98. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spStages, 'shenpi_type', 'stage'));
  99. const spChanges = ctx.helper._.filter(auditShenpiChanges, { spid: subProject.id });
  100. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChanges, 'shenpi_type', 'change'));
  101. const spRevise = ctx.helper._.filter(auditShenpiRevise, { spid: subProject.id });
  102. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spRevise, 'shenpi_type', 'revise'));
  103. const spMaterial = subProject.page_show.openMaterial ? ctx.helper._.filter(auditShenpiMaterial, { spid: subProject.id }) : [];
  104. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spMaterial, 'shenpi_type', 'material'));
  105. const spAdvance = ctx.helper._.filter(auditShenpiAdvance, { spid: subProject.id });
  106. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spAdvance, 'shenpi_type', 'advance'));
  107. const spChangeProject = subProject.page_show.openChangeProject ? ctx.helper._.filter(auditShenpiChangeProject, { spid: subProject.id }) : [];
  108. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeProject, 'shenpi_type', 'changeProject'));
  109. const spChangeApply = subProject.page_show.openChangeApply ? ctx.helper._.filter(auditShenpiChangeApply, { spid: subProject.id }) : [];
  110. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeApply, 'shenpi_type', 'changeApply'));
  111. const spChangePlan = subProject.page_show.openChangePlan ? ctx.helper._.filter(auditShenpiChangePlan, { spid: subProject.id }) : [];
  112. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangePlan, 'shenpi_type', 'changePlan'));
  113. const spPayment = subProject.page_show.openPayment ? ctx.helper._.filter(auditShenpiPayment, { spid: subProject.id }) : [];
  114. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spPayment, 'shenpi_type', 'payment'));
  115. const spFinancial = subProject.page_show.openFinancial ? ctx.helper._.filter(auditShenpiFinancial, { spid: subProject.id }) : [];
  116. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spFinancial, 'shenpi_type', 'financial'));
  117. const spInspection = subProject.page_show.qualityInspection ? ctx.helper._.filter(auditShenpiInspection, { spid: subProject.id }) : [];
  118. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spInspection, 'shenpi_type', 'inspection'));
  119. const spSafeInspection = subProject.page_show.safeInspection ? ctx.helper._.filter(auditShenpiSafeInspection, { spid: subProject.id }) : [];
  120. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spSafeInspection, 'shenpi_type', 'safeInspection'));
  121. const spSafeStage = subProject.page_show.safeInspection ? ctx.helper._.filter(auditShenpiSafeStage, { spid: subProject.id }) : [];
  122. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spSafeStage, 'shenpi_type', 'safeStage'));
  123. const spPhasePay = subProject.page_show.phasePay ? ctx.helper._.filter(auditShenpiPhasePay, { spid: subProject.id }) : [];
  124. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spPhasePay, 'shenpi_type', 'phasePay'));
  125. const spCostStageLedger = subProject.page_show.cost ? ctx.helper._.filter(auditShenpiCostStageLedger, { spid: subProject.id }) : [];
  126. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spCostStageLedger, 'shenpi_type', 'costStageLedger'));
  127. const spCostStageBook = subProject.page_show.cost ? ctx.helper._.filter(auditShenpiCostStageBook, { spid: subProject.id }) : [];
  128. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spCostStageBook, 'shenpi_type', 'costStageBook'));
  129. const spCostStageAnalysis = subProject.page_show.cost ? ctx.helper._.filter(auditShenpiCostStageAnalysis, { spid: subProject.id }) : [];
  130. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spCostStageAnalysis, 'shenpi_type', 'costStageAnalysis'));
  131. }
  132. const noticeDayList = [];
  133. noticeList = ctx.helper._.orderBy(noticeList, ['shenpi_time'], ['desc']);
  134. for (const notice of noticeList) {
  135. const noticeDay = moment(new Date(notice.shenpi_time)).format('YYYY-MM-DD');
  136. if (noticeDayList.indexOf(noticeDay) === -1) noticeDayList.push(noticeDay);
  137. }
  138. const dashboardShenpis = [];
  139. // 审批时间状态提示,0:无预警,1,预警(当前时间大于创建时间常高于stageAuditEarly天时,触发提示),2,紧急(当前时间大于审批发起时常高于stageAuditWorry天时,触发提示)
  140. const auditTenders = await this.auditSet(ctx, allAuditTenders, subProjects, dashboardStatus, 'ledger');
  141. dashboardShenpis.push(...auditTenders);
  142. const auditStages = await this.auditSet(ctx, allAuditStages, subProjects, dashboardStatus, 'stage');
  143. dashboardShenpis.push(...auditStages);
  144. const auditChanges = await this.auditSet(ctx, allAuditChanges, subProjects, dashboardStatus, 'change');
  145. dashboardShenpis.push(...auditChanges);
  146. const auditRevise = await this.auditSet(ctx, allAuditRevise, subProjects, dashboardStatus, 'revise');
  147. dashboardShenpis.push(...auditRevise);
  148. const auditMaterial = await this.auditSet(ctx, allAuditMaterial, subProjects, dashboardStatus, 'material');
  149. dashboardShenpis.push(...auditMaterial);
  150. const auditAdvance = await this.auditSet(ctx, allAuditAdvance, subProjects, dashboardStatus, 'advance');
  151. dashboardShenpis.push(...auditAdvance);
  152. const auditChangeProject = await this.auditSet(ctx, allAuditChangeProject, subProjects, dashboardStatus, 'changeProject');
  153. dashboardShenpis.push(...auditChangeProject);
  154. const auditChangeApply = await this.auditSet(ctx, allAuditChangeApply, subProjects, dashboardStatus, 'changeApply');
  155. dashboardShenpis.push(...auditChangeApply);
  156. const auditChangePlan = await this.auditSet(ctx, allAuditChangePlan, subProjects, dashboardStatus, 'changePlan');
  157. dashboardShenpis.push(...auditChangePlan);
  158. const auditPayments = await this.auditSet(ctx, allAuditPayments, subProjects, dashboardStatus, 'payment');
  159. dashboardShenpis.push(...auditPayments);
  160. const auditStageAss = await this.auditSet(ctx, allAuditStageAss, subProjects, dashboardStatus, 'stageAss');
  161. dashboardShenpis.push(...auditStageAss);
  162. const auditFinancials = await this.auditSet(ctx, allAuditFinancials, subProjects, dashboardStatus, 'financial');
  163. dashboardShenpis.push(...auditFinancials);
  164. const auditInspections = await this.auditSet(ctx, allAuditInspections, subProjects, dashboardStatus, 'inspection');
  165. dashboardShenpis.push(...auditInspections);
  166. const auditSafeInspections = await this.auditSet(ctx, allAuditSafeInspections, subProjects, dashboardStatus, 'safeInspection');
  167. dashboardShenpis.push(...auditSafeInspections);
  168. const auditSafeStage = await this.auditSet(ctx, allAuditSafeStage, subProjects, dashboardStatus, 'safeStage');
  169. dashboardShenpis.push(...auditSafeStage);
  170. const auditPhasePay = await this.auditSet(ctx, allAuditPhasePay, subProjects, dashboardStatus, 'phasePay');
  171. dashboardShenpis.push(...auditPhasePay);
  172. const auditCostStageLedger = await this.auditSet(ctx, allAuditCostStageLedger, subProjects, dashboardStatus, 'costStageLedger');
  173. dashboardShenpis.push(...auditCostStageLedger);
  174. const auditCostStageBook = await this.auditSet(ctx, allAuditCostStageBook, subProjects, dashboardStatus, 'costStageBook');
  175. dashboardShenpis.push(...auditCostStageBook);
  176. const auditCostStageAnalysis = await this.auditSet(ctx, allAuditCostStageAnalysis, subProjects, dashboardStatus, 'costStageAnalysis');
  177. dashboardShenpis.push(...auditCostStageAnalysis);
  178. // console.log(ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'desc']));
  179. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  180. // 获取销售人员数据
  181. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  182. const officeName = officeList[salesmanData.office];
  183. // 获取版本信息
  184. const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
  185. // 获取项目通知
  186. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.helper._.map(subProjects, 'id'), 10);
  187. // 获取系统通知
  188. const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2);
  189. // 获取系统维护信息
  190. const maintainData = await ctx.service.maintain.getDataById(1);
  191. // 最近使用项目列表
  192. const recentProjects = await ctx.service.subProjPermission.getRecentProjects(ctx.session.sessionUser.accountId);
  193. const renderData = {
  194. dashboardShenpis: ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'asc']),
  195. dashboardStatus,
  196. role: pa.role,
  197. authMobile: pa.auth_mobile,
  198. acLedger: auditConst.ledger,
  199. acStage: auditConst.stage,
  200. acChange: auditConst.change,
  201. acRevise: auditConst.revise,
  202. acMaterial: auditConst.material,
  203. acAdvance: auditConst.advance,
  204. acChangeProject: auditConst.changeProject,
  205. acChangeApply: auditConst.changeApply,
  206. acChangePlan: auditConst.changeApply,
  207. acFinancial: auditConst.financial,
  208. acInspection: auditConst.inspection,
  209. acSafeStage: auditConst.safeStage,
  210. acPhasePay: auditConst.phasePay,
  211. acCostStage: auditConst.costStage,
  212. noticeList,
  213. noticeDayList,
  214. pushType: auditConst.pushType,
  215. projectData,
  216. salesmanData,
  217. officeName,
  218. versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  219. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  220. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  221. uid: ctx.session.sessionUser.accountId,
  222. maintainData,
  223. maintainConst,
  224. typeColMap,
  225. recentProjects,
  226. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.dashboard.workspace),
  227. };
  228. await this.layout('dashboard/workspace.ejs', renderData, 'dashboard/modal.ejs');
  229. await ctx.service.projectAccount.defaultUpdate({
  230. id: this.ctx.session.sessionUser.accountId,
  231. last_notice: new Date(),
  232. });
  233. }
  234. async loadTenders(ctx) {
  235. try {
  236. const responseData = { err: 0, msg: '', data: {} };
  237. const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true);
  238. const tenders = [];
  239. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  240. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  241. for (const subProject of subProjects) {
  242. subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show);
  243. // 获取每个项目下参与的标段
  244. const subProjectTenders = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin, '', subProject);
  245. tenders.push(...subProjectTenders);
  246. }
  247. for (const t of tenders) {
  248. // 用标段管理的方法获取t数据
  249. await this.ctx.service.tenderCache.loadTenderCache(t, '');
  250. t.total_price = t.ledger_tp && t.ledger_tp.total_price ? t.ledger_tp.total_price : 0;
  251. // [t.change_tp, t.change_p_tp, t.change_n_tp, t.change_valuation_tp, t.change_unvaluation_tp] = await ctx.service.change.getChangeTp(t.id);
  252. }
  253. responseData.data.tenderList = tenders;
  254. ctx.body = responseData;
  255. } catch (err) {
  256. this.log(err);
  257. ctx.body = { err: 1, msg: err.toString(), data: null };
  258. }
  259. }
  260. async auditSet(ctx, allAudits, subProjects, dashboardStatus, type) {
  261. const audits = [];
  262. for (const t of allAudits) {
  263. const sp = subProjects.find(sp => sp.id === t.spid);
  264. if (sp) {
  265. t.sp_name = sp.name;
  266. t.start_audit = 0;
  267. let calcTime;
  268. let closeType = false;
  269. switch (type) {
  270. case 'ledger':
  271. calcTime = t.ledger_status === auditConst[type].status.checking ? t.begin_time : t.end_time;
  272. break;
  273. case 'revise':
  274. calcTime = t.status === auditConst[type].status.checking ? t.begin_time : t.end_time;
  275. break;
  276. case 'stage':
  277. calcTime = t.sstatus === auditConst[type].status.checkNo ? t.end_time : t.begin_time;
  278. break;
  279. case 'stageAss':
  280. calcTime = t.begin_time;
  281. break;
  282. case 'change':
  283. calcTime = t.begin_time ? t.begin_time : t.cin_time ? new Date(ctx.moment.unix(t.cin_time).format('YYYY-MM-DD HH:mm:ss')) : '';
  284. break;
  285. case 'changeProject':
  286. if (!sp.page_show.openChangeProject) closeType = true;
  287. calcTime = t.status !== auditConst[type].status.back ? t.begin_time : t.end_time;
  288. break;
  289. case 'changeApply':
  290. if (!sp.page_show.openChangeApply) closeType = true;
  291. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  292. break;
  293. case 'changePlan':
  294. if (!sp.page_show.openChangePlan) closeType = true;
  295. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  296. break;
  297. case 'material':
  298. if (!sp.page_show.openMaterial) closeType = true;
  299. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  300. break;
  301. case 'advance':
  302. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.create_time : t.end_time;
  303. break;
  304. case 'payment':
  305. if (!sp.page_show.openPayment) closeType = true;
  306. calcTime = t.sstatus !== auditConst.stage.status.checkNo ? t.begin_time : t.end_time;
  307. break;
  308. case 'financial':
  309. if (!sp.page_show.openFinancial) closeType = true;
  310. calcTime = t.fpcstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  311. break;
  312. case 'inspection':
  313. if (!sp.page_show.qualityInspection) closeType = true;
  314. calcTime = t.status !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  315. break;
  316. case 'safeInspection':
  317. if (!sp.page_show.safeInspection) closeType = true;
  318. calcTime = t.status !== auditConst.inspection.status.checkNo ? t.begin_time : t.end_time;
  319. break;
  320. case 'safeStage':
  321. if (!sp.page_show.safePayment) closeType = true;
  322. calcTime = t.create_time;
  323. break;
  324. case 'phasePay':
  325. if (!sp.page_show.phasePay) closeType = true;
  326. calcTime = t.create_time;
  327. break;
  328. case 'costStageLedger':
  329. case 'costStageBook':
  330. case 'costStageAnalysis':
  331. if (!sp.page_show.cost) closeType = true;
  332. calcTime = t.create_time;
  333. break;
  334. default:
  335. closeType = true;
  336. }
  337. if (closeType) continue;
  338. if (sp.page_show.openStageAudit) {
  339. const now = new Date();
  340. const calcDay = ctx.helper.calculateDaysBetween(now, calcTime);
  341. if (sp.page_show.stageAuditWorry && calcDay >= sp.page_show.stageAuditWorry) {
  342. t.start_audit = 2;
  343. dashboardStatus.worry += 1;
  344. } else if (sp.page_show.stageAuditEarly && calcDay >= sp.page_show.stageAuditEarly) {
  345. t.start_audit = 1;
  346. dashboardStatus.early += 1;
  347. }
  348. }
  349. const calcType = type === 'stageAss' ? 'stage' : type;
  350. dashboardStatus.shenpi[calcType] += 1;
  351. t.shenpi_time = calcTime;
  352. t.shenpi_type = type;
  353. dashboardStatus.dashboard += 1;
  354. audits.push(t);
  355. }
  356. }
  357. return audits;
  358. }
  359. /**
  360. * 控制面板页面
  361. *
  362. * @param {Object} ctx - egg全局变量
  363. * @return {void}
  364. */
  365. async index(ctx) {
  366. const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId, ctx.subProject.id);
  367. const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id);
  368. const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId, ctx.subProject.id);
  369. const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId, ctx.subProject.id);
  370. const auditMaterial = ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  371. const auditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId, ctx.subProject.id);
  372. const auditChangeProject = ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  373. const auditChangeApply = ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  374. const auditChangePlan = ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  375. const auditPayments = ctx.subProject.page_show.openPayment ? await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  376. const auditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId, ctx.subProject.id);
  377. const auditFinancials = ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  378. const auditInspections = ctx.subProject.page_show.qualityInspection ? await ctx.service.qualityInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  379. const auditSafeInspections = ctx.subProject.page_show.safeInspection ? await ctx.service.safeInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  380. const auditSafeStage = ctx.subProject.page_show.safePayment ? await ctx.service.safeStageAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  381. const auditPhasePay = ctx.subProject.page_show.phasePay ? await ctx.service.phasePayAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  382. const auditCostStageLedger = ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getAuditStage('ledger', ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  383. const auditCostStageBook = ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getAuditStage('book', ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  384. const auditCostStageAnalysis = ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getAuditStage('analysis', ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  385. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  386. const noticeList = await ctx.service.noticePush.getNotice(ctx.session.sessionProject.id, pa.id, ctx.subProject.id);
  387. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  388. // 获取销售人员数据
  389. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  390. const officeName = officeList[salesmanData.office];
  391. // 获取版本信息
  392. const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
  393. // 获取项目通知
  394. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id);
  395. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  396. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  397. // 获取系统通知
  398. const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2);
  399. // 获取系统维护信息
  400. const maintainData = await ctx.service.maintain.getDataById(1);
  401. // 获取各个审批的次数及最后的审批时间
  402. const shenpi_count = [
  403. { count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '预付款' },
  404. { count: await ctx.service.ledgerAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账审批' },
  405. { count: await ctx.service.reviseAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账修订' },
  406. { count: await ctx.service.stageAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '计量审批' },
  407. { count: await ctx.service.changeAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更审批' },
  408. ];
  409. if (ctx.subProject.page_show.openChangeProject) shenpi_count.push({ count: await ctx.service.changeProjectAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更立项' });
  410. if (ctx.subProject.page_show.openChangeApply) shenpi_count.push({ count: await ctx.service.changeApplyAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更申请' });
  411. if (ctx.subProject.page_show.openChangePlan) shenpi_count.push({ count: await ctx.service.changePlanAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更方案' });
  412. if (ctx.subProject.page_show.openMaterial) shenpi_count.push({ count: await ctx.service.materialAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '材料调差' });
  413. if (ctx.subProject.page_show.openFinancial) shenpi_count.push({ count: await ctx.service.financialPayAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '资金支付' });
  414. if (ctx.subProject.page_show.qualityInspection) shenpi_count.push({ count: await ctx.service.qualityInspectionAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '质量巡检' });
  415. if (ctx.subProject.page_show.safeInspection) shenpi_count.push({ count: await ctx.service.safeInspectionAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '安全巡检' });
  416. if (ctx.subProject.page_show.safePayment) shenpi_count.push({ count: await ctx.service.safeStageAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '安全计量' });
  417. if (ctx.subProject.page_show.phasePay) shenpi_count.push({ count: await ctx.service.phasePayAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '合同支付' });
  418. if (ctx.subProject.page_show.cost) {
  419. shenpi_count.push({ count: await ctx.service.costStageAudit.getCountByChecked('ledger', ctx.session.sessionUser.accountId, ctx.subProject.id), name: '成本报审' });
  420. shenpi_count.push({ count: await ctx.service.costStageAudit.getCountByChecked('book', ctx.session.sessionUser.accountId, ctx.subProject.id), name: '财务账面' });
  421. shenpi_count.push({ count: await ctx.service.costStageAudit.getCountByChecked('analysis', ctx.session.sessionUser.accountId, ctx.subProject.id), name: '成本分析' });
  422. }
  423. // shenpi_count.push({ count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '预付款' });
  424. const total_count = ctx.app._.sumBy(shenpi_count, 'count');
  425. const shenpi_lastime = [
  426. await ctx.service.advanceAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  427. await ctx.service.ledgerAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  428. await ctx.service.reviseAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  429. await ctx.service.stageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  430. await ctx.service.changeAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  431. ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  432. ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  433. ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  434. ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  435. ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  436. ctx.subProject.page_show.qualityInspection ? await ctx.service.qualityInspectionAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  437. ctx.subProject.page_show.safeInspection ? await ctx.service.safeInspectionAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  438. ctx.subProject.page_show.safeInspection ? await ctx.service.safeStageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  439. ctx.subProject.page_show.phasePay ? await ctx.service.phasePayAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  440. ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getLastEndTimeByChecked('ledger', ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  441. ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getLastEndTimeByChecked('book', ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  442. ctx.subProject.page_show.cost ? await ctx.service.costStageAudit.getLastEndTimeByChecked('analysis', ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  443. ];
  444. const last_time = ctx.app._.max(shenpi_lastime);
  445. // console.log(ctx.app._.max(shenpi_lastime), ctx.helper.calcDayNum(last_time));
  446. const renderData = {
  447. auditTenders,
  448. auditStages,
  449. auditChanges,
  450. auditRevise,
  451. auditMaterial,
  452. auditAdvance,
  453. auditChangeProject,
  454. auditChangeApply,
  455. auditChangePlan,
  456. auditPayments,
  457. auditStageAss,
  458. auditFinancials,
  459. auditInspections,
  460. auditSafeInspections,
  461. auditSafeStage,
  462. auditPhasePay,
  463. auditCostStageLedger,
  464. auditCostStageBook,
  465. auditCostStageAnalysis,
  466. shenpi_count,
  467. total_count,
  468. last_day: ctx.helper.calcDayNum(last_time),
  469. role: pa.role,
  470. authMobile: pa.auth_mobile,
  471. acLedger: auditConst.ledger,
  472. acStage: auditConst.stage,
  473. acChange: auditConst.change,
  474. acRevise: auditConst.revise,
  475. acMaterial: auditConst.material,
  476. acAdvance: auditConst.advance,
  477. acChangeProject: auditConst.changeProject,
  478. acChangeApply: auditConst.changeApply,
  479. acChangePlan: auditConst.changeApply,
  480. acFinancial: auditConst.financial,
  481. acInspection: auditConst.inspection,
  482. acSafeStage: auditConst.safeStage,
  483. acPhasePay: auditConst.phasePay,
  484. acCostStage: auditConst.costStage,
  485. noticeList,
  486. pushType: auditConst.pushType,
  487. projectData,
  488. salesmanData,
  489. officeName,
  490. versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  491. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  492. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  493. userMsgPermission,
  494. uid: ctx.session.sessionUser.accountId,
  495. maintainData,
  496. maintainConst,
  497. typeColMap,
  498. };
  499. await this.layout('dashboard/index.ejs', renderData, 'dashboard/modal.ejs');
  500. await ctx.service.projectAccount.defaultUpdate({
  501. id: this.ctx.session.sessionUser.accountId,
  502. last_notice: new Date(),
  503. });
  504. }
  505. /**
  506. * 控制面板-通知页面
  507. *
  508. * @param {Object} ctx - egg全局变量
  509. * @return {void}
  510. */
  511. async msg(ctx) {
  512. try {
  513. const page = ctx.page;
  514. const msgId = parseInt(ctx.params.mid) || 0;
  515. let msgInfo = msgId ? await ctx.service.message.getDataById(msgId) : null;
  516. const type = msgInfo ? msgInfo.type : ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
  517. if (msgInfo && msgInfo.type === 1 && msgInfo.project_id !== ctx.session.sessionProject.id) {
  518. throw '非该项目通知无权查看';
  519. }
  520. if (msgInfo) {
  521. msgInfo.content = JSON.parse(JSON.stringify(msgInfo.content).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;'));
  522. msgInfo.files = await this.ctx.service.messageAtt.getAtt(msgInfo.id);
  523. }
  524. const total = type === 1 ?
  525. await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, spid: [ctx.subProject.id, ''], type }) :
  526. await ctx.service.message.count({ status: 1, type });
  527. const limit = 5;
  528. const offset = limit * (this.ctx.page - 1);
  529. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id, limit, offset, type);
  530. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  531. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  532. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  533. if (!msgInfo) {
  534. msgInfo = msgList[0];
  535. }
  536. // 分页相关
  537. const pageInfo = {
  538. page,
  539. total: Math.ceil(total / limit),
  540. queryData: JSON.stringify(ctx.urlInfo.query),
  541. };
  542. const renderData = {
  543. msgInfo,
  544. uid: ctx.session.sessionUser.accountId,
  545. type,
  546. pageInfo,
  547. userMsgPermission,
  548. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  549. };
  550. await this.layout('dashboard/msg.ejs', renderData);
  551. } catch (error) {
  552. console.log(error);
  553. this.log(error);
  554. ctx.session.postError = error.toString();
  555. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard');
  556. }
  557. }
  558. /**
  559. * 控制面板-通知添加和编辑页面
  560. *
  561. * @param {Object} ctx - egg全局变量
  562. * @return {void}
  563. */
  564. async msgAdd(ctx) {
  565. let id = ctx.params.mid;
  566. id = parseInt(id);
  567. try {
  568. if (isNaN(id) || id < 0) {
  569. throw '参数错误';
  570. }
  571. const rule = ctx.service.message.rule();
  572. const jsValidator = await this.jsValidator.convert(rule).build();
  573. const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
  574. const files = await ctx.service.messageAtt.getAtt(id);
  575. const renderData = {
  576. jsValidator,
  577. msgInfo,
  578. files,
  579. whiteList: ctx.app.config.multipart.whitelist,
  580. moment,
  581. };
  582. await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
  583. } catch (error) {
  584. console.log(error);
  585. // this.setMessage(error.toString(), this.messageType.ERROR);
  586. ctx.redirect(ctx.request.header.referer);
  587. }
  588. }
  589. /**
  590. * 控制面板-通知保存
  591. *
  592. * @param {Object} ctx - egg全局变量
  593. * @return {void}
  594. */
  595. async msgSet(ctx) {
  596. try {
  597. let id = ctx.params.mid;
  598. id = parseInt(id);
  599. if (isNaN(id) || id < 0) {
  600. throw '参数错误';
  601. }
  602. const rule = ctx.service.message.rule();
  603. ctx.helper.validate(rule);
  604. const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id, ctx.subProject.id);
  605. if (result) {
  606. // 新增的项目通知会发送微信模版消息通知客户
  607. if (id === 0) {
  608. // 获取该项目所有的openid,发送信息
  609. const wechats = await ctx.service.projectAccount.getOpenIdListByPid(ctx.session.sessionProject.id);
  610. if (wechats.length > 0) {
  611. const msgInfo = await ctx.service.message.getDataById(result);
  612. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  613. // 绑定成功通知
  614. const templateId = 'VKUo4us4lt2dQY0EaaJxcui2jkjmriN3A0K7i4kpZwY';
  615. const url = ctx.protocol + '://' + ctx.host + '/wx/url2wap?project=' + ctx.session.sessionProject.code + '&url=' + ctx.protocol + '://' + ctx.host + '/wap/dashboard/msg/' + msgInfo.id;
  616. const msgData = {
  617. thing21: {
  618. value: ctx.helper.contentChange(projectData.name),
  619. },
  620. thing2: {
  621. value: ctx.helper.contentChange(msgInfo.title),
  622. },
  623. thing8: {
  624. value: msgInfo.creator,
  625. },
  626. time3: {
  627. value: moment(msgInfo.release_time * 1000).format('YYYY-MM-DD'),
  628. },
  629. };
  630. for (const wx of wechats) {
  631. const result = await app.wechat.api.sendTemplate(wx.wx_openid, templateId, url, '', msgData);
  632. }
  633. }
  634. }
  635. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg');
  636. }
  637. } catch (error) {
  638. console.log(error);
  639. ctx.redirect(ctx.request.header.referer);
  640. }
  641. }
  642. /**
  643. * 控制面板-通知删除
  644. *
  645. * @param {Object} ctx - egg全局变量
  646. * @return {void}
  647. */
  648. async msgDelete(ctx) {
  649. try {
  650. let id = ctx.params.mid;
  651. id = parseInt(id);
  652. if (isNaN(id) || id <= 0) {
  653. throw '参数错误';
  654. }
  655. const msgInfo = await ctx.service.message.getDataById(id);
  656. if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
  657. throw '通知不存在或无权限操作';
  658. }
  659. const result = await ctx.service.message.deleteMsg(msgInfo.id);
  660. if (result) {
  661. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg');
  662. }
  663. } catch (error) {
  664. console.log(error);
  665. ctx.redirect(ctx.request.header.referer);
  666. }
  667. }
  668. /**
  669. * 将推送记录设置为已读
  670. * @param {Object} ctx 上下文
  671. */
  672. async pushSet(ctx) {
  673. try {
  674. const { id } = JSON.parse(ctx.request.body.data);
  675. const data = await ctx.service.noticePush.set(id);
  676. ctx.body = { err: 0, msg: '' };
  677. } catch (err) {
  678. this.log(err);
  679. ctx.body = { err: 1, msg: err.toString(), data: null };
  680. }
  681. }
  682. /**
  683. * 上传附件
  684. * @param {*} ctx 上下文
  685. */
  686. async msgUploadFile(ctx) {
  687. let stream;
  688. try {
  689. const responseData = { err: 0, msg: '', data: {} };
  690. const mid = ctx.params.mid || 0;
  691. if (!mid) throw '参数有误';
  692. const parts = this.ctx.multipart({
  693. autoFields: true,
  694. });
  695. const files = [];
  696. const create_time = Date.parse(new Date()) / 1000;
  697. let idx = 0;
  698. while ((stream = await parts()) !== undefined) {
  699. if (!stream.filename) {
  700. // 如果没有传入直接返回
  701. return;
  702. }
  703. const fileInfo = path.parse(stream.filename);
  704. const filepath = `app/public/upload/message/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  705. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  706. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  707. ++idx;
  708. stream && (await sendToWormhole(stream));
  709. }
  710. const in_time = new Date();
  711. const payload = files.map(file => {
  712. let idx;
  713. if (Array.isArray(parts.field.name)) {
  714. idx = parts.field.name.findIndex(name => name === file.name);
  715. } else {
  716. idx = 'isString';
  717. }
  718. const newFile = {
  719. project_id: ctx.session.sessionProject.id,
  720. mid,
  721. uid: ctx.session.sessionUser.accountId,
  722. filename: file.name,
  723. fileext: file.ext,
  724. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  725. filepath: file.filepath,
  726. upload_time: in_time,
  727. };
  728. return newFile;
  729. });
  730. // 执行文件信息写入数据库
  731. await ctx.service.messageAtt.saveFileMsgToDb(payload);
  732. // 将最新的当前标段的所有文件信息返回
  733. responseData.data = await ctx.service.messageAtt.getAtt(mid);
  734. ctx.body = responseData;
  735. } catch (err) {
  736. stream && (await sendToWormhole(stream));
  737. this.log(err);
  738. ctx.body = { err: 1, msg: err.toString(), data: null };
  739. }
  740. }
  741. /**
  742. * 删除附件
  743. * @param {Ojbect} ctx 上下文
  744. */
  745. async msgDeleteFile(ctx) {
  746. try {
  747. const mid = ctx.params.mid || 0;
  748. const responseData = { err: 0, msg: '', data: {} };
  749. const data = JSON.parse(ctx.request.body.data);
  750. const fileInfo = await ctx.service.messageAtt.getDataById(data.id);
  751. if (fileInfo) {
  752. // 先删除文件
  753. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  754. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  755. // 再删除数据库
  756. await ctx.service.messageAtt.delete(data.id);
  757. } else {
  758. throw '不存在该文件';
  759. }
  760. responseData.data = await ctx.service.messageAtt.getAtt(mid);
  761. ctx.body = responseData;
  762. } catch (err) {
  763. this.log(err);
  764. ctx.body = { err: 1, msg: err.toString(), data: null };
  765. }
  766. }
  767. }
  768. return DashboardController;
  769. };