base_controller.js 5.9 KB

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