payment_tender_check.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const paymentConst = require('../const/payment');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* paymentTenderCheck(next) {
  22. try {
  23. if (!this.session.sessionProject.showPayment) {
  24. throw '该功能已关闭或无法查看';
  25. }
  26. if (!this.params.pid) {
  27. throw '当前未打开标段';
  28. }
  29. const tender = yield this.service.paymentTender.getDataById(this.params.pid);
  30. if (tender.pid !== this.session.sessionProject.id || tender.spid !== this.subProject.id) throw '您无权查看该项目';
  31. // const projectInfo = yield this.service.project.getDataById(this.subProject.id);
  32. const modes = this.subProject.payment_setting ? JSON.parse(this.subProject.payment_setting) : _.cloneDeep(paymentConst.setting_modes);
  33. for (const m in paymentConst.setting_modes) {
  34. if (!modes[m]) modes[m] = _.cloneDeep(paymentConst.setting_modes[m]);
  35. }
  36. const auditPermission = yield this.service.subProjPermission.getPaymentPermission(this.subProject.permission.payment_permission);
  37. if (!auditPermission) {
  38. throw '权限不足';
  39. }
  40. if (!tender) {
  41. throw '标段不存在';
  42. }
  43. const payment = {
  44. auditPermission,
  45. project_setting: modes,
  46. }
  47. this.paymentTender = tender;
  48. this.payment = payment;
  49. yield next;
  50. } catch (err) {
  51. // 输出错误到日志
  52. if (err.stack) {
  53. this.logger.error(err);
  54. } else {
  55. this.session.message = {
  56. type: messageType.ERROR,
  57. icon: 'exclamation-circle',
  58. message: err,
  59. };
  60. this.getLogger('fail').info(JSON.stringify({
  61. error: err,
  62. project: this.session.sessionProject,
  63. user: this.session.sessionUser,
  64. body: this.session.body,
  65. }));
  66. }
  67. if (this.helper.isAjax(this.request)) {
  68. if (err.stack) {
  69. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  70. } else {
  71. this.body = {err: 3, msg: err.toString(), data: null};
  72. }
  73. } else {
  74. if (this.helper.isWap(this.request)) {
  75. this.redirect('/wap/list');
  76. } else {
  77. this.postError(err, '未知错误');
  78. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : (err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/sp/' + this.subProject.id + '/payment'));
  79. }
  80. }
  81. }
  82. };
  83. };