base_controller.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. this.ctx.menuList.budget.display = this.ctx.session.sessionProject.showBudget;
  69. try {
  70. data.min = this.app.config.min;
  71. const viewString = await this.ctx.renderView(view, data);
  72. const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
  73. // 获取系统维护信息
  74. const maintainData = await this.ctx.service.maintain.getDataById(1);
  75. const renderData = {
  76. min: this.app.config.min,
  77. content: viewString,
  78. message: JSON.stringify(message),
  79. modal: modalString,
  80. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  81. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  82. tenderList: data.tenderList === undefined ? [] : data.tenderList,
  83. jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,
  84. maintainData,
  85. maintainConst,
  86. };
  87. await this.ctx.render('layout/layout.ejs', renderData);
  88. } catch(err) {
  89. this.log(err);
  90. }
  91. }
  92. /**
  93. * 设置提示
  94. *
  95. * @param {String} message - 提示信息
  96. * @param {String} type - 提示类型
  97. * @return {void}
  98. */
  99. setMessage(message, type) {
  100. let icon = '';
  101. switch (type) {
  102. case messageType.SUCCESS:
  103. icon = 'check';
  104. break;
  105. case messageType.ERROR:
  106. icon = 'exclamation-circle';
  107. break;
  108. case messageType.INFO:
  109. icon = 'info-circle';
  110. break;
  111. case messageType.WARNING:
  112. icon = 'warning';
  113. break;
  114. default:
  115. break;
  116. }
  117. this.ctx.session.message = {
  118. type,
  119. icon,
  120. message,
  121. };
  122. }
  123. log(error) {
  124. if (error.stack) {
  125. this.ctx.logger.error(error);
  126. } else {
  127. this.setMessage(error, messageType.ERROR);
  128. this.ctx.getLogger('fail').info(JSON.stringify({
  129. error: error,
  130. project: this.ctx.session.sessionProject,
  131. user: this.ctx.session.sessionUser,
  132. body: this.ctx.session.body,
  133. }));
  134. }
  135. }
  136. checkMeasureType (mt) {
  137. if (this.ctx.tender.data.measure_type !== mt) {
  138. throw '该模式下不可提交此数据';
  139. }
  140. }
  141. postError(error, msg) {
  142. if (error.stack) {
  143. this.ctx.session.postError = msg;
  144. } else {
  145. this.ctx.session.postError = error.toString();
  146. }
  147. }
  148. ajaxErrorBody(error, msg) {
  149. if (error.stack) {
  150. return {err: 4, msg: msg, data: null};
  151. } else {
  152. return {err: 3, msg: error.toString(), data: null};
  153. }
  154. }
  155. }
  156. module.exports = BaseController;