base_controller.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 maintainConst = require('../const/maintain');
  13. const otherProjectController = ['setting', 'file', 'profile'];
  14. class BaseController extends Controller {
  15. loadMenu(ctx) {
  16. if (ctx.isProjectController !== false) ctx.isProjectController = otherProjectController.indexOf(ctx.controllerName) >= 0 || !!this.app.menu.projectMenu[ctx.controllerName];
  17. const menuList = ctx.isProjectController
  18. ? JSON.parse(JSON.stringify(this.app.menu.projectMenu))
  19. : JSON.parse(JSON.stringify(this.app.menu.menu));
  20. ctx.menu = menuList[ctx.controllerName] || Object.values(menuList).find(menu => menu.controllers && menu.controllers.indexOf(ctx.controllerName) >= 0) || {};
  21. ctx.title = ctx.menu.name || '';
  22. if (ctx.menu && ctx.menu.children) {
  23. if (ctx.menu.children[ctx.actionName]) ctx.title = ctx.menu.children[ctx.actionName].name;
  24. }
  25. if (!ctx.isProjectController && ctx.subProject) {
  26. if (ctx.controllerName === 'sp') {
  27. if (ctx.url.indexOf('file') > 0 || ctx.url.indexOf('fm')) {
  28. ctx.menu = menuList.file;
  29. } else {
  30. ctx.menu = menuList.budget;
  31. }
  32. }
  33. menuList.datacollect.display = ctx.subProject.showDataCollect || false;
  34. menuList.file.display = ctx.subProject.page_show.openFile || false;
  35. menuList.contract.display = ctx.subProject.page_show.openContract || false;
  36. menuList.financial.display = ctx.subProject.page_show.openFinancial || false;
  37. menuList.budget.display = ctx.subProject.page_show.openBudget || false;
  38. menuList.payment.display = ctx.subProject.page_show.openPayment || false;
  39. for (const index in menuList) {
  40. const im = menuList[index];
  41. if (!im.url) {
  42. if (index === 'tender') {
  43. im.url = `/sp/${ctx.subProject.id}${ctx.curListUrl}`;
  44. } else if (index === 'contract') {
  45. im.url = `/sp/${ctx.subProject.id}/${im.controller}/detail`;
  46. } else if (index === 'financial') {
  47. im.url = `/sp/${ctx.subProject.id}/${im.controller}/${ctx.subProject.financialToUrl}`;
  48. } else {
  49. im.url = `/sp/${ctx.subProject.id}/${im.controller}`;
  50. }
  51. }
  52. }
  53. } else {
  54. menuList.management.display = ctx.session && ctx.session.sessionProject ? ctx.session.sessionProject.page_show.openManagement : false;
  55. }
  56. ctx.menuList = menuList;
  57. }
  58. /**
  59. * 构造函数
  60. *
  61. * @param {Object} ctx - egg全局context
  62. * @return {void}
  63. */
  64. constructor(ctx) {
  65. super(ctx);
  66. this.messageType = messageType;
  67. this.jsValidator = this.app.jsValidator;
  68. this.menu = this.app.menu;
  69. // 菜单列表
  70. ctx.showProject = false;
  71. ctx.showTender = false;
  72. ctx.showTitle = false;
  73. }
  74. /**
  75. * 渲染layout
  76. *
  77. * @param {String} view - 渲染的view
  78. * @param {Object} data - 渲染的数据
  79. * @param {String} modal - 渲染的modal
  80. * @return {void}
  81. */
  82. async layout(view, data = {}, modal = '') {
  83. data.moment = moment;
  84. // 获取消息提示
  85. let message = this.ctx.session.message;
  86. // 取出后删除
  87. this.ctx.session.message = null;
  88. // 获取报错信息
  89. const postError = this.ctx.session.postError;
  90. // 取出后删除
  91. this.ctx.session.postError = null;
  92. if (message === null && postError !== null && postError !== undefined) {
  93. message = {
  94. type: 'error',
  95. icon: 'exclamation-circle',
  96. message: postError,
  97. };
  98. }
  99. try {
  100. this.loadMenu(this.ctx);
  101. data.min = this.app.config.min;
  102. const viewString = await this.ctx.renderView(view, data);
  103. const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
  104. // 获取系统维护信息
  105. const maintainData = await this.ctx.service.maintain.getDataById(1);
  106. const renderData = {
  107. min: this.app.config.min,
  108. content: viewString,
  109. message: message ? message : '',
  110. modal: modalString,
  111. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  112. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  113. tenderList: data.tenderList === undefined ? [] : data.tenderList,
  114. jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,
  115. maintainData,
  116. maintainConst,
  117. };
  118. await this.ctx.render('layout/layout.ejs', renderData);
  119. } catch (err) {
  120. this.log(err);
  121. }
  122. }
  123. /**
  124. * 设置提示
  125. *
  126. * @param {String} message - 提示信息
  127. * @param {String} type - 提示类型
  128. * @return {void}
  129. */
  130. setMessage(message, type) {
  131. let icon = '';
  132. switch (type) {
  133. case messageType.SUCCESS:
  134. icon = 'check';
  135. break;
  136. case messageType.ERROR:
  137. icon = 'exclamation-circle';
  138. break;
  139. case messageType.INFO:
  140. icon = 'info-circle';
  141. break;
  142. case messageType.WARNING:
  143. icon = 'warning';
  144. break;
  145. default:
  146. break;
  147. }
  148. this.ctx.session.message = {
  149. type,
  150. icon,
  151. message,
  152. };
  153. }
  154. log(error) {
  155. if (error.stack) {
  156. this.ctx.logger.error(error);
  157. } else {
  158. this.setMessage(error, messageType.ERROR);
  159. this.ctx.getLogger('fail').info(JSON.stringify({
  160. error: error,
  161. project: this.ctx.session.sessionProject,
  162. user: this.ctx.session.sessionUser,
  163. body: this.ctx.session.body,
  164. }));
  165. }
  166. }
  167. checkMeasureType (mt) {
  168. if (this.ctx.tender.data.measure_type !== mt) {
  169. throw '该模式下不可提交此数据';
  170. }
  171. }
  172. postError(error, msg) {
  173. if (error.stack) {
  174. this.ctx.session.postError = msg;
  175. } else {
  176. this.ctx.session.postError = error.toString();
  177. }
  178. }
  179. ajaxErrorBody(error, msg) {
  180. if (error.stack) {
  181. return {err: 4, msg: msg, data: null};
  182. } else {
  183. return {err: 3, msg: error.toString(), data: null};
  184. }
  185. }
  186. }
  187. module.exports = BaseController;