dashboard_controller.js 53 KB

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