base_controller.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 menuList = require('../../config/menu').menu;
  13. class BaseController extends Controller {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局context
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.messageType = messageType;
  23. this.jsValidator = this.app.jsValidator;
  24. this.menu = this.app.menu;
  25. // 当前菜单
  26. ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
  27. ctx.title = ctx.menu === {} ? '' : ctx.menu.name;
  28. if (ctx.menu !== {} && ctx.menu.children !== null) {
  29. for (const index in ctx.menu.children) {
  30. if (index === ctx.actionName) {
  31. ctx.title = ctx.menu.children[index].name;
  32. }
  33. }
  34. }
  35. // 菜单列表
  36. ctx.menuList = menuList;
  37. ctx.showProject = false;
  38. ctx.showTender = false;
  39. ctx.showTitle = false;
  40. }
  41. /**
  42. * 渲染layout
  43. *
  44. * @param {String} view - 渲染的view
  45. * @param {Object} data - 渲染的数据
  46. * @param {String} modal - 渲染的modal
  47. * @return {void}
  48. */
  49. async layout(view, data = {}, modal = '') {
  50. data.moment = moment;
  51. // 获取消息提示
  52. let message = this.ctx.session.message;
  53. // 取出后删除
  54. this.ctx.session.message = null;
  55. // 获取报错信息
  56. const postError = this.ctx.session.postError;
  57. // 取出后删除
  58. this.ctx.session.postError = null;
  59. if (message === null && postError !== null && postError !== undefined) {
  60. message = {
  61. type: 'error',
  62. icon: 'exclamation-circle',
  63. message: postError,
  64. };
  65. }
  66. try {
  67. data.min = this.app.config.min;
  68. const viewString = await this.ctx.renderView(view, data);
  69. const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
  70. const renderData = {
  71. min: this.app.config.min,
  72. content: viewString,
  73. message: JSON.stringify(message),
  74. modal: modalString,
  75. dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
  76. breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
  77. tenderList: data.tenderList === undefined ? [] : data.tenderList,
  78. jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,
  79. };
  80. await this.ctx.render('layout/layout.ejs', renderData);
  81. } catch(err) {
  82. this.log(err);
  83. }
  84. }
  85. /**
  86. * 设置提示
  87. *
  88. * @param {String} message - 提示信息
  89. * @param {String} type - 提示类型
  90. * @return {void}
  91. */
  92. setMessage(message, type) {
  93. let icon = '';
  94. switch (type) {
  95. case messageType.SUCCESS:
  96. icon = 'check';
  97. break;
  98. case messageType.ERROR:
  99. icon = 'exclamation-circle';
  100. break;
  101. case messageType.INFO:
  102. icon = 'info-circle';
  103. break;
  104. case messageType.WARNING:
  105. icon = 'warning';
  106. break;
  107. default:
  108. break;
  109. }
  110. this.ctx.session.message = {
  111. type,
  112. icon,
  113. message,
  114. };
  115. }
  116. log(error) {
  117. if (error.stack) {
  118. this.ctx.logger.error(error);
  119. } else {
  120. this.setMessage(error, messageType.ERROR);
  121. this.ctx.getLogger('fail').info(JSON.stringify({
  122. error: error,
  123. project: this.ctx.session.sessionProject,
  124. user: this.ctx.session.sessionUser,
  125. body: this.ctx.session.body,
  126. }));
  127. }
  128. }
  129. checkMeasureType (mt) {
  130. if (this.ctx.tender.data.measure_type !== mt) {
  131. throw '该模式下不可提交此数据';
  132. }
  133. }
  134. postError(error, msg) {
  135. if (error.stack) {
  136. this.ctx.session.postError = msg;
  137. } else {
  138. this.ctx.session.postError = error.toString();
  139. }
  140. }
  141. ajaxErrorBody(error, msg) {
  142. if (error.stack) {
  143. return {err: 2, msg: msg, data: null};
  144. } else {
  145. return {err: 1, msg: error.toString(), data: null};
  146. }
  147. }
  148. }
  149. module.exports = BaseController;