base_controller.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. this.jsValidator = this.app.jsValidator;
  24. // 当前菜单
  25. ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
  26. // 菜单列表
  27. ctx.menuList = menuList;
  28. ctx.showProject = false;
  29. ctx.showTender = false;
  30. ctx.showTitle = false;
  31. }
  32. /**
  33. * 渲染layout
  34. *
  35. * @param {String} view - 渲染的view
  36. * @param {Object} data - 渲染的数据
  37. * @return {void}
  38. */
  39. async layout(view, data = {}) {
  40. data.moment = moment;
  41. // 获取消息提示
  42. const message = this.ctx.session.message;
  43. // 取出后删除
  44. this.ctx.session.message = null;
  45. const viewString = await this.ctx.renderView(view, data);
  46. const renderData = {
  47. content: viewString,
  48. message: JSON.stringify(message),
  49. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  50. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  51. };
  52. await this.ctx.render('layout/layout.ejs', renderData);
  53. }
  54. /**
  55. * 设置提示
  56. *
  57. * @param {String} message - 提示信息
  58. * @param {String} type - 提示类型
  59. * @return {void}
  60. */
  61. setMessage(message, type) {
  62. let icon = '';
  63. switch (type) {
  64. case messageType.SUCCESS:
  65. icon = 'check';
  66. break;
  67. case messageType.ERROR:
  68. icon = 'exclamation-circle';
  69. break;
  70. case messageType.INFO:
  71. icon = 'info-circle';
  72. break;
  73. case messageType.WARNING:
  74. icon = 'warning';
  75. break;
  76. default:
  77. break;
  78. }
  79. this.ctx.session.message = {
  80. type,
  81. icon,
  82. message,
  83. };
  84. }
  85. }
  86. module.exports = BaseController;