construction_check.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if (!this.subProject.page_show.openConstruction) {
  23. throw '该功能已关闭或无法查看';
  24. }
  25. const id = parseInt(this.params.tid);
  26. if (!id) throw '参数错误';
  27. const tender = yield this.service.tender.getTender(id, ['id', 'project_id', 'name']);
  28. if (!tender) {
  29. throw '标段不存在';
  30. }
  31. // 权限控制
  32. const result = yield this.service.constructionAudit.checkPermission(tender, this.session.sessionUser.accountId);
  33. if (!result) {
  34. throw '当前账号权限不足,请联系管理员添加权限';
  35. }
  36. this.constructionTender = tender;
  37. yield next;
  38. } catch (err) {
  39. // 输出错误到日志
  40. if (err.stack) {
  41. this.logger.error(err);
  42. } else {
  43. this.session.message = {
  44. type: messageType.ERROR,
  45. icon: 'exclamation-circle',
  46. message: err,
  47. };
  48. this.getLogger('fail').info(JSON.stringify({
  49. error: err,
  50. project: this.session.sessionProject,
  51. user: this.session.sessionUser,
  52. body: this.session.body,
  53. }));
  54. }
  55. if (this.helper.isAjax(this.request)) {
  56. if (err.stack) {
  57. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  58. } else {
  59. this.body = {err: 3, msg: err.toString(), data: null};
  60. }
  61. } else {
  62. if (this.helper.isWap(this.request)) {
  63. this.redirect('/wap/subproj');
  64. } else {
  65. this.postError(err, '未知错误');
  66. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : (err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect(`/sp/${this.subProject.id}/construction`));
  67. }
  68. }
  69. }
  70. };
  71. };