base_controller.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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').menu;
  13. const maintainConst = require('../const/maintain');
  14. class BaseController extends Controller {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局context
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.messageType = messageType;
  24. this.jsValidator = this.app.jsValidator;
  25. this.menu = this.app.menu;
  26. // 当前菜单
  27. ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
  28. ctx.title = ctx.menu === {} ? '' : ctx.menu.name;
  29. if (ctx.menu !== {} && ctx.menu.children !== null) {
  30. for (const index in ctx.menu.children) {
  31. if (index === ctx.actionName) {
  32. ctx.title = ctx.menu.children[index].name;
  33. }
  34. }
  35. }
  36. // 菜单列表
  37. ctx.menuList = menuList;
  38. ctx.showProject = false;
  39. ctx.showTender = false;
  40. ctx.showTitle = false;
  41. }
  42. /**
  43. * 渲染layout
  44. *
  45. * @param {String} view - 渲染的view
  46. * @param {Object} data - 渲染的数据
  47. * @param {String} modal - 渲染的modal
  48. * @return {void}
  49. */
  50. async layout(view, data = {}, modal = '') {
  51. data.moment = moment;
  52. // 获取消息提示
  53. let message = this.ctx.session.message;
  54. // 取出后删除
  55. this.ctx.session.message = null;
  56. // 获取报错信息
  57. const postError = this.ctx.session.postError;
  58. // 取出后删除
  59. this.ctx.session.postError = null;
  60. if (message === null && postError !== null && postError !== undefined) {
  61. message = {
  62. type: 'error',
  63. icon: 'exclamation-circle',
  64. message: postError,
  65. };
  66. }
  67. try {
  68. data.min = this.app.config.min;
  69. const viewString = await this.ctx.renderView(view, data);
  70. const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
  71. // 获取系统维护信息
  72. const maintainData = await this.ctx.service.maintain.getDataById(1);
  73. const renderData = {
  74. min: this.app.config.min,
  75. content: viewString,
  76. message: JSON.stringify(message),
  77. modal: modalString,
  78. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  79. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  80. tenderList: data.tenderList === undefined ? [] : data.tenderList,
  81. jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,
  82. maintainData,
  83. maintainConst,
  84. };
  85. await this.ctx.render('layout/layout.ejs', renderData);
  86. } catch(err) {
  87. this.log(err);
  88. }
  89. }
  90. /**
  91. * 设置提示
  92. *
  93. * @param {String} message - 提示信息
  94. * @param {String} type - 提示类型
  95. * @return {void}
  96. */
  97. setMessage(message, type) {
  98. let icon = '';
  99. switch (type) {
  100. case messageType.SUCCESS:
  101. icon = 'check';
  102. break;
  103. case messageType.ERROR:
  104. icon = 'exclamation-circle';
  105. break;
  106. case messageType.INFO:
  107. icon = 'info-circle';
  108. break;
  109. case messageType.WARNING:
  110. icon = 'warning';
  111. break;
  112. default:
  113. break;
  114. }
  115. this.ctx.session.message = {
  116. type,
  117. icon,
  118. message,
  119. };
  120. }
  121. log(error) {
  122. if (error.stack) {
  123. this.ctx.logger.error(error);
  124. } else {
  125. this.setMessage(error, messageType.ERROR);
  126. this.ctx.getLogger('fail').info(JSON.stringify({
  127. error: error,
  128. project: this.ctx.session.sessionProject,
  129. user: this.ctx.session.sessionUser,
  130. body: this.ctx.session.body,
  131. }));
  132. }
  133. }
  134. checkMeasureType (mt) {
  135. if (this.ctx.tender.data.measure_type !== mt) {
  136. throw '该模式下不可提交此数据';
  137. }
  138. }
  139. postError(error, msg) {
  140. if (error.stack) {
  141. this.ctx.session.postError = msg;
  142. } else {
  143. this.ctx.session.postError = error.toString();
  144. }
  145. }
  146. ajaxErrorBody(error, msg) {
  147. if (error.stack) {
  148. return {err: 4, msg: msg, data: null};
  149. } else {
  150. return {err: 3, msg: error.toString(), data: null};
  151. }
  152. }
  153. }
  154. module.exports = BaseController;