dashboard_controller.js 57 KB

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