| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 | 'use strict';/** * 控制器基类 * * @author CaiAoLin * @date 2017/10/11 * @version */const moment = require('moment');const messageType = require('../const/message_type');const Controller = require('egg').Controller;const maintainConst = require('../const/maintain');const otherProjectController = ['setting', 'file', 'profile'];class BaseController extends Controller {    loadMenu(ctx) {        if (ctx.isProjectController !== false) ctx.isProjectController = otherProjectController.indexOf(ctx.controllerName) >= 0 || !!this.app.menu.projectMenu[ctx.controllerName];        const menuList = ctx.isProjectController            ? JSON.parse(JSON.stringify(this.app.menu.projectMenu))            : JSON.parse(JSON.stringify(this.app.menu.menu));        ctx.menu = menuList[ctx.controllerName] || Object.values(menuList).find(menu => menu.controllers && menu.controllers.indexOf(ctx.controllerName) >= 0) || {};        ctx.title = ctx.menu.name || '';        if (ctx.menu && ctx.menu.children) {            if (ctx.menu.children[ctx.actionName]) ctx.title = ctx.menu.children[ctx.actionName].name;        }        if (!ctx.isProjectController && ctx.subProject) {            if (ctx.controllerName === 'sp') {                if (ctx.url.indexOf('file') > 0 || ctx.url.indexOf('fm')) {                    ctx.menu = menuList.file;                } else {                    ctx.menu = menuList.budget;                }            }            menuList.datacollect.display = ctx.subProject.showDataCollect || false;            menuList.file.display = ctx.subProject.page_show.openFile || false;            menuList.contract.display = ctx.subProject.page_show.openContract || false;            menuList.financial.display = ctx.subProject.page_show.openFinancial || false;            menuList.budget.display = ctx.subProject.page_show.openBudget || false;            menuList.payment.display = ctx.subProject.page_show.openPayment || false;            for (const index in menuList) {                const im = menuList[index];                if (!im.url) {                    if (index === 'tender') {                        im.url = `/sp/${ctx.subProject.id}${ctx.curListUrl}`;                    } else if (index === 'contract') {                        im.url = `/sp/${ctx.subProject.id}/${im.controller}/detail`;                    } else if (index === 'financial') {                        im.url = `/sp/${ctx.subProject.id}/${im.controller}/${ctx.subProject.financialToUrl}`;                    } else {                        im.url = `/sp/${ctx.subProject.id}/${im.controller}`;                    }                }            }        } else {            menuList.management.display = ctx.session && ctx.session.sessionProject ? ctx.session.sessionProject.page_show.openManagement : false;        }        ctx.menuList = menuList;    }    /**     * 构造函数     *     * @param {Object} ctx - egg全局context     * @return {void}     */    constructor(ctx) {        super(ctx);        this.messageType = messageType;        this.jsValidator = this.app.jsValidator;        this.menu = this.app.menu;        // 菜单列表        ctx.showProject = false;        ctx.showTender = false;        ctx.showTitle = false;    }    /**     * 渲染layout     *     * @param {String} view - 渲染的view     * @param {Object} data - 渲染的数据     * @param {String} modal - 渲染的modal     * @return {void}     */    async layout(view, data = {}, modal = '') {        data.moment = moment;        // 获取消息提示        let message = this.ctx.session.message;        // 取出后删除        this.ctx.session.message = null;        // 获取报错信息        const postError = this.ctx.session.postError;        // 取出后删除        this.ctx.session.postError = null;        if (message === null && postError !== null && postError !== undefined) {            message = {                type: 'error',                icon: 'exclamation-circle',                message: postError,            };        }        try {            this.loadMenu(this.ctx);            data.min = this.app.config.min;            const viewString = await this.ctx.renderView(view, data);            const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);            // 获取系统维护信息            const maintainData = await this.ctx.service.maintain.getDataById(1);            const renderData = {                min: this.app.config.min,                content: viewString,                message: message ? message : '',                modal: modalString,                dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,                breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,                tenderList: data.tenderList === undefined ? [] : data.tenderList,                jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,                maintainData,                maintainConst,            };            await this.ctx.render('layout/layout.ejs', renderData);        } catch (err) {            this.log(err);        }    }    /**     * 设置提示     *     * @param {String} message - 提示信息     * @param {String} type - 提示类型     * @return {void}     */    setMessage(message, type) {        let icon = '';        switch (type) {            case messageType.SUCCESS:                icon = 'check';                break;            case messageType.ERROR:                icon = 'exclamation-circle';                break;            case messageType.INFO:                icon = 'info-circle';                break;            case messageType.WARNING:                icon = 'warning';                break;            default:                break;        }        this.ctx.session.message = {            type,            icon,            message,        };    }    log(error) {        if (error.stack) {            this.ctx.logger.error(error);        } else {            this.setMessage(error, messageType.ERROR);            this.ctx.getLogger('fail').info(JSON.stringify({                error: error,                project: this.ctx.session.sessionProject,                user: this.ctx.session.sessionUser,                body: this.ctx.session.body,            }));        }    }    checkMeasureType (mt) {        if (this.ctx.tender.data.measure_type !== mt) {            throw '该模式下不可提交此数据';        }    }    postError(error, msg) {        if (error.stack) {            this.ctx.session.postError = msg;        } else {            this.ctx.session.postError = error.toString();        }    }    ajaxErrorBody(error, msg) {        if (error.stack) {            return {err: 4, msg: msg, data: null};        } else {            return {err: 3, msg: error.toString(), data: null};        }    }}module.exports = BaseController;
 |