123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 'use strict';
- /**
- * 控制器基类
- *
- * @author CaiAoLin
- * @date 2017/10/11
- * @version
- */
- const moment = require('moment');
- const Controller = require('egg').Controller;
- const menuList = require('../../config/menu');
- class BaseController extends Controller {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.menuList = menuList;
- ctx.menu = menuList[ctx.controllerName] ? menuList[ctx.controllerName] : {};
- ctx.title = ctx.menu.name ? ctx.menu.name : '';
- }
- /**
- * 渲染layout
- *
- * @param {String} view - 渲染的view
- * @param {Object} data - 渲染的数据
- * @param {String} modal - 渲染的modal
- * @return {void}
- */
- async layout(view, data = {}, modal = '') {
- // 获取消息提示
- let message = this.ctx.session.message;
- // 取出后删除
- this.ctx.session.message = null;
- data.moment = moment;
- try {
- data.min = this.app.config.min ? '.min' : '';
- const contentString = await this.ctx.renderView(view, data);
- const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
- const renderData = {
- min: this.app.config.min ? '.min' : '',
- content: contentString,
- modal: modalString,
- message,
- };
- await this.ctx.render('layout/layout.ejs', renderData);
- } catch (err) {
- console.log(err);
- }
- }
- /**
- * 设置提示
- *
- * @param {String} message - 提示信息
- * @param {String} type - 提示类型
- * @return {void}
- */
- setMessage(type, message) {
- this.ctx.session.message = {
- type,
- message,
- };
- }
- }
- module.exports = BaseController;
|