base_controller.js 5.6 KB

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