base_controller.js 8.8 KB

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