base_controller.js 9.5 KB

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