base_controller.js 11 KB

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