base_controller.js 6.1 KB

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