base_controller.js 7.4 KB

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