base_controller.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. /**
  3. * 控制器基类
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/11
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const Controller = require('egg').Controller;
  11. const menuList = require('../../config/menu');
  12. class BaseController extends Controller {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局context
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. ctx.menuList = menuList;
  22. ctx.menu = menuList[ctx.controllerName] ? menuList[ctx.controllerName] : {};
  23. ctx.title = ctx.menu.name ? ctx.menu.name : '';
  24. }
  25. /**
  26. * 渲染layout
  27. *
  28. * @param {String} view - 渲染的view
  29. * @param {Object} data - 渲染的数据
  30. * @param {String} modal - 渲染的modal
  31. * @return {void}
  32. */
  33. async layout(view, data = {}, modal = '') {
  34. data.moment = moment;
  35. const contentString = await this.ctx.renderView(view, data);
  36. const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
  37. const renderData = {
  38. content: contentString,
  39. modal: modalString,
  40. };
  41. await this.ctx.render('layout/layout.ejs', renderData);
  42. }
  43. }
  44. module.exports = BaseController;