base_controller.js 5.3 KB

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