base_controller.js 6.2 KB

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