base_controller.js 5.0 KB

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