construction_check.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const _ = require('lodash');
  11. module.exports = options => {
  12. /**
  13. * 标段校验 中间件
  14. * 1. 读取标段数据(包括属性)
  15. * 2. 检验用户是否可见标段(不校验具体权限)
  16. *
  17. * @param {function} next - 中间件继续执行的方法
  18. * @return {void}
  19. */
  20. return function* constructionCheck(next) {
  21. try {
  22. const id = parseInt(this.params.tid);
  23. if (!id) throw '参数错误';
  24. const tender = yield this.service.tender.getTender(id, ['id', 'project_id', 'name']);
  25. if (!tender) {
  26. throw '标段不存在';
  27. }
  28. // 权限控制
  29. const result = yield this.service.constructionAudit.checkPermission(tender, this.session.sessionUser.accountId);
  30. if (!result) {
  31. throw '当前账号权限不足,请联系管理员添加权限';
  32. }
  33. this.constructionTender = tender;
  34. yield next;
  35. } catch (err) {
  36. // 输出错误到日志
  37. if (err.stack) {
  38. this.logger.error(err);
  39. } else {
  40. this.session.message = {
  41. type: messageType.ERROR,
  42. icon: 'exclamation-circle',
  43. message: err,
  44. };
  45. this.getLogger('fail').info(JSON.stringify({
  46. error: err,
  47. project: this.session.sessionProject,
  48. user: this.session.sessionUser,
  49. body: this.session.body,
  50. }));
  51. }
  52. if (this.helper.isAjax(this.request)) {
  53. if (err.stack) {
  54. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  55. } else {
  56. this.body = {err: 3, msg: err.toString(), data: null};
  57. }
  58. } else {
  59. if (this.helper.isWap(this.request)) {
  60. this.redirect('/wap/list');
  61. } else {
  62. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/construction');
  63. }
  64. }
  65. }
  66. };
  67. };