base_controller.js 11 KB

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