base_controller.js 9.3 KB

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