base_controller.js 8.1 KB

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