'use strict'; /** * 控制面板 * * @author CaiAoLin * @date 2017/11/23 * @version */ const auditConst = require('../const/audit'); const officeList = require('../const/cld_office').list; const maintainConst = require('../const/maintain'); const typeColMap = require('../const/advance').typeColMap; const moment = require('moment'); const fs = require('fs'); const path = require('path'); const sendToWormhole = require('stream-wormhole'); module.exports = app => { class DashboardController extends app.BaseController { /** * 控制面板页面 * * @param {Object} ctx - egg全局变量 * @return {void} */ async workspace(ctx) { const allAuditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId); const allAuditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId); const allAuditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId); const allAuditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId); const allAuditMaterial = await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId); const allAuditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId); const allAuditChangeProject = await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId); const allAuditChangeApply = await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId); const allAuditChangePlan = await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId); const allAuditPayments = await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId); const allAuditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId); const allAuditFinancials = await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId); const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId); const auditShenpiTenders = await ctx.service.ledgerAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiStages = await ctx.service.stageAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiChanges = await ctx.service.changeAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiRevise = await ctx.service.reviseAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiMaterial = await ctx.service.materialAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiAdvance = await ctx.service.advanceAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiChangeProject = await ctx.service.changeProjectAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiChangeApply = await ctx.service.changeApplyAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiChangePlan = await ctx.service.changePlanAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiPayment = await ctx.service.paymentDetailAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const auditShenpiFinancial = await ctx.service.financialPayAudit.getDonesByAudit(ctx.session.sessionUser.accountId); const dashboardStatus = { all: 0, dashboard: 0, worry: 0, early: 0, shenpi: { ledger: 0, stage: 0, change: 0, revise: 0, material: 0, advance: 0, changeProject: 0, changeApply: 0, changePlan: 0, payment: 0, financial: 0, }, }; let noticeList = []; // 还要考虑功能关闭不展示对应类型情况 const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true); for (const subProject of subProjects) { subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show); const spTenders = ctx.helper._.filter(auditShenpiTenders, { spid: subProject.id }); noticeList.push(...ctx.helper.addKeyValue4ObjArray(spTenders, 'shenpi_type', 'ledger')); const spStages = ctx.helper._.filter(auditShenpiStages, { spid: subProject.id }); noticeList.push(...ctx.helper.addKeyValue4ObjArray(spStages, 'shenpi_type', 'stage')); const spChanges = ctx.helper._.filter(auditShenpiChanges, { spid: subProject.id }); noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChanges, 'shenpi_type', 'change')); const spRevise = ctx.helper._.filter(auditShenpiRevise, { spid: subProject.id }); noticeList.push(...ctx.helper.addKeyValue4ObjArray(spRevise, 'shenpi_type', 'revise')); const spMaterial = subProject.page_show.openMaterial ? ctx.helper._.filter(auditShenpiMaterial, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spMaterial, 'shenpi_type', 'material')); const spAdvance = ctx.helper._.filter(auditShenpiAdvance, { spid: subProject.id }); noticeList.push(...ctx.helper.addKeyValue4ObjArray(spAdvance, 'shenpi_type', 'advance')); const spChangeProject = subProject.page_show.openChangeProject ? ctx.helper._.filter(auditShenpiChangeProject, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeProject, 'shenpi_type', 'changeProject')); const spChangeApply = subProject.page_show.openChangeApply ? ctx.helper._.filter(auditShenpiChangeApply, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeApply, 'shenpi_type', 'changeApply')); const spChangePlan = subProject.page_show.openChangePlan ? ctx.helper._.filter(auditShenpiChangePlan, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangePlan, 'shenpi_type', 'changePlan')); const spPayment = subProject.page_show.openPayment ? ctx.helper._.filter(auditShenpiPayment, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spPayment, 'shenpi_type', 'payment')); const spFinancial = subProject.page_show.openFinancial ? ctx.helper._.filter(auditShenpiFinancial, { spid: subProject.id }) : []; noticeList.push(...ctx.helper.addKeyValue4ObjArray(spFinancial, 'shenpi_type', 'financial')); } const noticeDayList = []; noticeList = ctx.helper._.orderBy(noticeList, ['shenpi_time'], ['desc']); for (const notice of noticeList) { const noticeDay = moment(new Date(notice.shenpi_time)).format('YYYY-MM-DD'); if (noticeDayList.indexOf(noticeDay) === -1) noticeDayList.push(noticeDay); } const dashboardShenpis = []; // 审批时间状态提示,0:无预警,1,预警(当前时间大于创建时间常高于stageAuditEarly天时,触发提示),2,紧急(当前时间大于审批发起时常高于stageAuditWorry天时,触发提示) const auditTenders = await this.auditSet(ctx, allAuditTenders, subProjects, dashboardStatus, 'ledger'); dashboardShenpis.push(...auditTenders); const auditStages = await this.auditSet(ctx, allAuditStages, subProjects, dashboardStatus, 'stage'); dashboardShenpis.push(...auditStages); const auditChanges = await this.auditSet(ctx, allAuditChanges, subProjects, dashboardStatus, 'change'); dashboardShenpis.push(...auditChanges); const auditRevise = await this.auditSet(ctx, allAuditRevise, subProjects, dashboardStatus, 'revise'); dashboardShenpis.push(...auditRevise); const auditMaterial = await this.auditSet(ctx, allAuditMaterial, subProjects, dashboardStatus, 'material'); dashboardShenpis.push(...auditMaterial); const auditAdvance = await this.auditSet(ctx, allAuditAdvance, subProjects, dashboardStatus, 'advance'); dashboardShenpis.push(...auditAdvance); const auditChangeProject = await this.auditSet(ctx, allAuditChangeProject, subProjects, dashboardStatus, 'changeProject'); dashboardShenpis.push(...auditChangeProject); const auditChangeApply = await this.auditSet(ctx, allAuditChangeApply, subProjects, dashboardStatus, 'changeApply'); dashboardShenpis.push(...auditChangeApply); const auditChangePlan = await this.auditSet(ctx, allAuditChangePlan, subProjects, dashboardStatus, 'changePlan'); dashboardShenpis.push(...auditChangePlan); const auditPayments = await this.auditSet(ctx, allAuditPayments, subProjects, dashboardStatus, 'payment'); dashboardShenpis.push(...auditPayments); const auditStageAss = await this.auditSet(ctx, allAuditStageAss, subProjects, dashboardStatus, 'stageAss'); dashboardShenpis.push(...auditStageAss); const auditFinancials = await this.auditSet(ctx, allAuditFinancials, subProjects, dashboardStatus, 'financial'); dashboardShenpis.push(...auditFinancials); // console.log(ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'desc'])); const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id); // 获取销售人员数据 const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id); const officeName = officeList[salesmanData.office]; // 获取版本信息 const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 }); // 获取项目通知 const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.helper._.map(subProjects, 'id'), 10); // 获取系统通知 const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2); // 获取系统维护信息 const maintainData = await ctx.service.maintain.getDataById(1); const renderData = { dashboardShenpis: ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'asc']), dashboardStatus, role: pa.role, authMobile: pa.auth_mobile, acLedger: auditConst.ledger, acStage: auditConst.stage, acChange: auditConst.change, acRevise: auditConst.revise, acMaterial: auditConst.material, acAdvance: auditConst.advance, acChangeProject: auditConst.changeProject, acChangeApply: auditConst.changeApply, acChangePlan: auditConst.changeApply, acFinancial: auditConst.financial, noticeList, noticeDayList, pushType: auditConst.pushType, projectData, salesmanData, officeName, versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), uid: ctx.session.sessionUser.accountId, maintainData, maintainConst, typeColMap, jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.dashboard.workspace), }; await this.layout('dashboard/workspace.ejs', renderData, 'dashboard/modal.ejs'); await ctx.service.projectAccount.defaultUpdate({ id: this.ctx.session.sessionUser.accountId, last_notice: new Date(), }); } async loadTenders(ctx) { try { const responseData = { err: 0, msg: '', data: {} }; const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true); const tenders = []; const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId); const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null; for (const subProject of subProjects) { subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show); // 获取每个项目下参与的标段 const subProjectTenders = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin, '', subProject); tenders.push(...subProjectTenders); } for (const t of tenders) { // 用标段管理的方法获取t数据 await this.ctx.service.tenderCache.loadTenderCache(t, ''); t.total_price = t.ledger_tp && t.ledger_tp.total_price ? t.ledger_tp.total_price : 0; // [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); } responseData.data.tenderList = tenders; ctx.body = responseData; } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } async auditSet(ctx, allAudits, subProjects, dashboardStatus, type) { const audits = []; for (const t of allAudits) { const sp = subProjects.find(sp => sp.id === t.spid); if (sp) { t.sp_name = sp.name; t.start_audit = 0; let calcTime; let closeType = false; switch (type) { case 'ledger': calcTime = t.ledger_status === auditConst[type].status.checking ? t.begin_time : t.end_time; break; case 'revise': calcTime = t.status === auditConst[type].status.checking ? t.begin_time : t.end_time; break; case 'stage': calcTime = t.sstatus === auditConst[type].status.checkNo ? t.end_time : t.begin_time; break; case 'stageAss': calcTime = t.begin_time; break; case 'change': 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')) : ''; break; case 'changeProject': if (!sp.page_show.openChangeProject) closeType = true; calcTime = t.status !== auditConst[type].status.back ? t.begin_time : t.end_time; break; case 'changeApply': if (!sp.page_show.openChangeApply) closeType = true; calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time; break; case 'changePlan': if (!sp.page_show.openChangePlan) closeType = true; calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time; break; case 'material': if (!sp.page_show.openMaterial) closeType = true; calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time; break; case 'advance': calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.create_time : t.end_time; break; case 'payment': if (!sp.page_show.openPayment) closeType = true; calcTime = t.sstatus !== auditConst.stage.status.checkNo ? t.begin_time : t.end_time; break; case 'financial': if (!sp.page_show.openFinancial) closeType = true; calcTime = t.fpcstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time; break; default: closeType = true; } if (closeType) continue; if (sp.page_show.openStageAudit) { const now = new Date(); const calcDay = ctx.helper.calculateDaysBetween(now, calcTime); if (sp.page_show.stageAuditWorry && calcDay >= sp.page_show.stageAuditWorry) { t.start_audit = 2; dashboardStatus.worry += 1; } else if (sp.page_show.stageAuditEarly && calcDay >= sp.page_show.stageAuditEarly) { t.start_audit = 1; dashboardStatus.early += 1; } } const calcType = type === 'stageAss' ? 'stage' : type; dashboardStatus.shenpi[calcType] += 1; t.shenpi_time = calcTime; t.shenpi_type = type; dashboardStatus.dashboard += 1; audits.push(t); } } return audits; } /** * 控制面板页面 * * @param {Object} ctx - egg全局变量 * @return {void} */ async index(ctx) { const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditMaterial = ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const auditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditChangeProject = ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const auditChangeApply = ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const auditChangePlan = ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const auditPayments = ctx.subProject.page_show.openPayment ? await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const auditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId, ctx.subProject.id); const auditFinancials = ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId, ctx.subProject.id) : []; const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId); const noticeList = await ctx.service.noticePush.getNotice(ctx.session.sessionProject.id, pa.id, ctx.subProject.id); const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id); // 获取销售人员数据 const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id); const officeName = officeList[salesmanData.office]; // 获取版本信息 const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 }); // 获取项目通知 const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id); const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null; const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1; // 获取系统通知 const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2); // 获取系统维护信息 const maintainData = await ctx.service.maintain.getDataById(1); // 获取各个审批的次数及最后的审批时间 const shenpi_count = [ { count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '预付款' }, { count: await ctx.service.ledgerAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账审批' }, { count: await ctx.service.reviseAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账修订' }, { count: await ctx.service.stageAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '计量审批' }, { count: await ctx.service.changeAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更审批' }, ]; if (ctx.subProject.page_show.openChangeProject) shenpi_count.push({ count: await ctx.service.changeProjectAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更立项' }); if (ctx.subProject.page_show.openChangeApply) shenpi_count.push({ count: await ctx.service.changeApplyAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更申请' }); if (ctx.subProject.page_show.openChangePlan) shenpi_count.push({ count: await ctx.service.changePlanAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更方案' }); if (ctx.subProject.page_show.openMaterial) shenpi_count.push({ count: await ctx.service.materialAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '材料调差' }); if (ctx.subProject.page_show.openFinancial) shenpi_count.push({ count: await ctx.service.financialPayAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '资金支付' }); // shenpi_count.push({ count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '预付款' }); const total_count = ctx.app._.sumBy(shenpi_count, 'count'); const shenpi_lastime = [ await ctx.service.advanceAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), await ctx.service.ledgerAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), await ctx.service.reviseAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), await ctx.service.stageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), await ctx.service.changeAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null, ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null, ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null, ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null, ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null, ]; const last_time = ctx.app._.max(shenpi_lastime); // console.log(ctx.app._.max(shenpi_lastime), ctx.helper.calcDayNum(last_time)); const renderData = { auditTenders, auditStages, auditChanges, auditRevise, auditMaterial, auditAdvance, auditChangeProject, auditChangeApply, auditChangePlan, auditPayments, auditStageAss, auditFinancials, shenpi_count, total_count, last_day: ctx.helper.calcDayNum(last_time), role: pa.role, authMobile: pa.auth_mobile, acLedger: auditConst.ledger, acStage: auditConst.stage, acChange: auditConst.change, acRevise: auditConst.revise, acMaterial: auditConst.material, acAdvance: auditConst.advance, acChangeProject: auditConst.changeProject, acChangeApply: auditConst.changeApply, acChangePlan: auditConst.changeApply, acFinancial: auditConst.financial, noticeList, pushType: auditConst.pushType, projectData, salesmanData, officeName, versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), userMsgPermission, uid: ctx.session.sessionUser.accountId, maintainData, maintainConst, typeColMap, }; await this.layout('dashboard/index.ejs', renderData, 'dashboard/modal.ejs'); await ctx.service.projectAccount.defaultUpdate({ id: this.ctx.session.sessionUser.accountId, last_notice: new Date(), }); } /** * 控制面板-通知页面 * * @param {Object} ctx - egg全局变量 * @return {void} */ async msg(ctx) { try { const page = ctx.page; const msgId = parseInt(ctx.params.mid) || 0; let msgInfo = msgId ? await ctx.service.message.getDataById(msgId) : null; const type = msgInfo ? msgInfo.type : ctx.request.query.type ? parseInt(ctx.request.query.type) : 1; if (msgInfo && msgInfo.type === 1 && msgInfo.project_id !== ctx.session.sessionProject.id) { throw '非该项目通知无权查看'; } if (msgInfo) { msgInfo.content = JSON.parse(JSON.stringify(msgInfo.content).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')); msgInfo.files = await this.ctx.service.messageAtt.getAtt(msgInfo.id); } const total = type === 1 ? await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, spid: [ctx.subProject.id, ''], type }) : await ctx.service.message.count({ status: 1, type }); const limit = 5; const offset = limit * (this.ctx.page - 1); const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id, limit, offset, type); const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId); const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null; const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1; if (!msgInfo) { msgInfo = msgList[0]; } // 分页相关 const pageInfo = { page, total: Math.ceil(total / limit), queryData: JSON.stringify(ctx.urlInfo.query), }; const renderData = { msgInfo, uid: ctx.session.sessionUser.accountId, type, pageInfo, userMsgPermission, msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '
').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, ' ')), }; await this.layout('dashboard/msg.ejs', renderData); } catch (error) { console.log(error); this.log(error); ctx.session.postError = error.toString(); ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard'); } } /** * 控制面板-通知添加和编辑页面 * * @param {Object} ctx - egg全局变量 * @return {void} */ async msgAdd(ctx) { let id = ctx.params.mid; id = parseInt(id); try { if (isNaN(id) || id < 0) { throw '参数错误'; } const rule = ctx.service.message.rule(); const jsValidator = await this.jsValidator.convert(rule).build(); const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id); const files = await ctx.service.messageAtt.getAtt(id); const renderData = { jsValidator, msgInfo, files, whiteList: ctx.app.config.multipart.whitelist, moment, }; await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs'); } catch (error) { console.log(error); // this.setMessage(error.toString(), this.messageType.ERROR); ctx.redirect(ctx.request.header.referer); } } /** * 控制面板-通知保存 * * @param {Object} ctx - egg全局变量 * @return {void} */ async msgSet(ctx) { try { let id = ctx.params.mid; id = parseInt(id); if (isNaN(id) || id < 0) { throw '参数错误'; } const rule = ctx.service.message.rule(); ctx.helper.validate(rule); const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id, ctx.subProject.id); if (result) { // 新增的项目通知会发送微信模版消息通知客户 if (id === 0) { // 获取该项目所有的openid,发送信息 const wechats = await ctx.service.projectAccount.getOpenIdListByPid(ctx.session.sessionProject.id); if (wechats.length > 0) { const msgInfo = await ctx.service.message.getDataById(result); const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id); // 绑定成功通知 const templateId = 'VKUo4us4lt2dQY0EaaJxcui2jkjmriN3A0K7i4kpZwY'; const url = ctx.protocol + '://' + ctx.host + '/wx/url2wap?project=' + ctx.session.sessionProject.code + '&url=' + ctx.protocol + '://' + ctx.host + '/wap/dashboard/msg/' + msgInfo.id; const msgData = { thing21: { value: ctx.helper.contentChange(projectData.name), }, thing2: { value: ctx.helper.contentChange(msgInfo.title), }, thing8: { value: msgInfo.creator, }, time3: { value: moment(msgInfo.release_time * 1000).format('YYYY-MM-DD'), }, }; for (const wx of wechats) { const result = await app.wechat.api.sendTemplate(wx.wx_openid, templateId, url, '', msgData); } } } ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg'); } } catch (error) { console.log(error); ctx.redirect(ctx.request.header.referer); } } /** * 控制面板-通知删除 * * @param {Object} ctx - egg全局变量 * @return {void} */ async msgDelete(ctx) { try { let id = ctx.params.mid; id = parseInt(id); if (isNaN(id) || id <= 0) { throw '参数错误'; } const msgInfo = await ctx.service.message.getDataById(id); if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) { throw '通知不存在或无权限操作'; } const result = await ctx.service.message.deleteMsg(msgInfo.id); if (result) { ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg'); } } catch (error) { console.log(error); ctx.redirect(ctx.request.header.referer); } } /** * 将推送记录设置为已读 * @param {Object} ctx 上下文 */ async pushSet(ctx) { try { const { id } = JSON.parse(ctx.request.body.data); const data = await ctx.service.noticePush.set(id); ctx.body = { err: 0, msg: '' }; } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } /** * 上传附件 * @param {*} ctx 上下文 */ async msgUploadFile(ctx) { let stream; try { const responseData = { err: 0, msg: '', data: {} }; const mid = ctx.params.mid || 0; if (!mid) throw '参数有误'; const parts = this.ctx.multipart({ autoFields: true, }); const files = []; const create_time = Date.parse(new Date()) / 1000; let idx = 0; while ((stream = await parts()) !== undefined) { if (!stream.filename) { // 如果没有传入直接返回 return; } const fileInfo = path.parse(stream.filename); const filepath = `app/public/upload/message/fujian_${create_time + idx.toString() + fileInfo.ext}`; await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream); files.push({ filepath, name: stream.filename, ext: fileInfo.ext }); ++idx; stream && (await sendToWormhole(stream)); } const in_time = new Date(); const payload = files.map(file => { let idx; if (Array.isArray(parts.field.name)) { idx = parts.field.name.findIndex(name => name === file.name); } else { idx = 'isString'; } const newFile = { project_id: ctx.session.sessionProject.id, mid, uid: ctx.session.sessionUser.accountId, filename: file.name, fileext: file.ext, filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]), filepath: file.filepath, upload_time: in_time, }; return newFile; }); // 执行文件信息写入数据库 await ctx.service.messageAtt.saveFileMsgToDb(payload); // 将最新的当前标段的所有文件信息返回 responseData.data = await ctx.service.messageAtt.getAtt(mid); ctx.body = responseData; } catch (err) { stream && (await sendToWormhole(stream)); this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } /** * 删除附件 * @param {Ojbect} ctx 上下文 */ async msgDeleteFile(ctx) { try { const mid = ctx.params.mid || 0; const responseData = { err: 0, msg: '', data: {} }; const data = JSON.parse(ctx.request.body.data); const fileInfo = await ctx.service.messageAtt.getDataById(data.id); if (fileInfo) { // 先删除文件 // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath)); await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath); // 再删除数据库 await ctx.service.messageAtt.delete(data.id); } else { throw '不存在该文件'; } responseData.data = await ctx.service.messageAtt.getAtt(mid); ctx.body = responseData; } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } } return DashboardController; };