base_controller.js 11 KB

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