base_controller.js 5.4 KB

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