base_controller.js 11 KB

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