base_controller.js 7.1 KB

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