base_controller.js 7.2 KB

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