base_controller.js 7.6 KB

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