dashboard_controller.js 57 KB

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