base_controller.js 5.6 KB

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