base_controller.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. /**
  3. * 控制器基类
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/11
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const messageType = require('../const/message_type');
  11. const Controller = require('egg').Controller;
  12. const menuList = require('../../config/menu');
  13. class BaseController extends Controller {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局context
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.messageType = messageType;
  23. // 当前菜单
  24. ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
  25. // 菜单列表
  26. ctx.menuList = menuList;
  27. ctx.showProject = false;
  28. ctx.showTender = false;
  29. ctx.showTitle = false;
  30. }
  31. /**
  32. * 渲染layout
  33. *
  34. * @param {String} view - 渲染的view
  35. * @param {Object} data - 渲染的数据
  36. * @return {void}
  37. */
  38. async layout(view, data = {}) {
  39. data.moment = moment;
  40. // 获取消息提示
  41. const message = this.ctx.session.message;
  42. // 取出后删除
  43. this.ctx.session.message = null;
  44. const viewString = await this.ctx.renderView(view, data);
  45. const renderData = {
  46. content: viewString,
  47. message: JSON.stringify(message),
  48. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  49. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  50. };
  51. await this.ctx.render('layout/layout.ejs', renderData);
  52. }
  53. /**
  54. * 设置提示
  55. *
  56. * @param {String} message - 提示信息
  57. * @param {String} type - 提示类型
  58. * @return {void}
  59. */
  60. setMessage(message, type) {
  61. let icon = '';
  62. switch (type) {
  63. case messageType.SUCCESS:
  64. icon = 'check';
  65. break;
  66. case messageType.ERROR:
  67. icon = 'exclamation-circle';
  68. break;
  69. case messageType.INFO:
  70. icon = 'info-circle';
  71. break;
  72. case messageType.WARNING:
  73. icon = 'warning';
  74. break;
  75. default:
  76. break;
  77. }
  78. this.ctx.session.message = {
  79. type,
  80. icon,
  81. message,
  82. };
  83. }
  84. }
  85. module.exports = BaseController;