base_controller.js 7.4 KB

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