| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | '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;module.exports = app => {    class DashboardController extends app.BaseController {        /**         * 控制面板页面         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        async index(ctx) {            const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);            const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);            const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);            const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId);            const auditMaterial = await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId);            const auditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId);            const auditChangeProject = ctx.session.sessionProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId) : [];            const auditChangeApply = ctx.session.sessionProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId) : [];            const auditChangePlan = ctx.session.sessionProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId) : [];            const auditPayments = await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId);            const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);            const noticeList = await ctx.service.noticePush.getNotice(ctx.session.sessionProject.id, pa.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);            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), name: '预付款' },                { count: await ctx.service.ledgerAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '台账审批' },                { count: await ctx.service.reviseAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '台账修订' },                { count: await ctx.service.stageAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '计量审批' },                { count: await ctx.service.changeAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '变更审批' },            ];            if (ctx.session.sessionProject.page_show.openChangeProject) shenpi_count.push({ count: await ctx.service.changeProjectAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '变更立项' });            if (ctx.session.sessionProject.page_show.openChangeApply) shenpi_count.push({ count: await ctx.service.changeApplyAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '变更申请' });            if (ctx.session.sessionProject.page_show.openChangePlan) shenpi_count.push({ count: await ctx.service.changePlanAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '变更方案' });            shenpi_count.push({ count: await ctx.service.materialAudit.getCountByChecked(ctx.session.sessionUser.accountId), 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),                await ctx.service.ledgerAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId),                await ctx.service.reviseAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId),                await ctx.service.stageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId),                await ctx.service.changeAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId),                ctx.session.sessionProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId) : null,                ctx.session.sessionProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId) : null,                ctx.session.sessionProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId) : null,                await ctx.service.materialAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId),            ];            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,                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.flow,                acRevise: auditConst.revise,                acMaterial: auditConst.material,                acAdvance: auditConst.advance,                acChangeProject: auditConst.changeProject,                acChangeApply: auditConst.changeApply,                acChangePlan: auditConst.changeApply,                noticeList,                pushType: auditConst.pushType,                projectData,                salesmanData,                officeName,                versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),                msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),                sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').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 msgList(ctx) {            const page = ctx.page;            const type = ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;            const total = type === 1 ?                await ctx.service.message.count({ project_id: ctx.session.sessionProject.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, 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;            // 分页相关            const pageInfo = {                page,                total: Math.ceil(total / limit),                queryData: JSON.stringify(ctx.urlInfo.query),            };            const renderData = {                uid: ctx.session.sessionUser.accountId,                type,                pageInfo,                userMsgPermission,                msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),            };            await this.layout('dashboard/msg.ejs', renderData);        }        /**         * 控制面板-通知添加和编辑页面         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        async msgAdd(ctx) {            let id = ctx.params.id;            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 renderData = {                    jsValidator,                    msgInfo,                };                await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');            } catch (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.id;                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);                if (result) {                    ctx.redirect('/dashboard/msg/list');                }            } catch (error) {                ctx.redirect(ctx.request.header.referer);            }        }        /**         * 控制面板-通知删除         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        async msgDelete(ctx) {            try {                let id = ctx.params.id;                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.deleteById(msgInfo.id);                if (result) {                    ctx.redirect('/dashboard/msg/list');                }            } catch (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 };            }        }    }    return DashboardController;};
 |