'use strict'; /** * * * @author Mai * @date * @version */ const messageType = require('../const/message_type'); const paymentConst = require('../const/payment'); const _ = require('lodash'); module.exports = options => { /** * 标段校验 中间件 * 1. 读取标段数据(包括属性) * 2. 检验用户是否可见标段(不校验具体权限) * * @param {function} next - 中间件继续执行的方法 * @return {void} */ return function* paymentTenderCheck(next) { try { if (!this.subProject.showPayment) { throw '该功能已关闭或无法查看'; } if (!this.params.pid) { throw '当前未打开标段'; } const tender = yield this.service.paymentTender.getDataById(this.params.pid); if (tender.pid !== this.session.sessionProject.id || tender.spid !== this.subProject.id) throw '您无权查看该项目'; // const projectInfo = yield this.service.project.getDataById(this.subProject.id); const modes = this.subProject.payment_setting ? JSON.parse(this.subProject.payment_setting) : _.cloneDeep(paymentConst.setting_modes); for (const m in paymentConst.setting_modes) { if (!modes[m]) modes[m] = _.cloneDeep(paymentConst.setting_modes[m]); } const auditPermission = yield this.service.subProjPermission.getPaymentPermission(this.subProject.permission.payment_permission); if (!auditPermission) { throw '权限不足'; } if (!tender) { throw '标段不存在'; } const payment = { auditPermission, project_setting: modes, } this.paymentTender = tender; this.payment = payment; yield next; } catch (err) { // 输出错误到日志 if (err.stack) { this.logger.error(err); } else { this.session.message = { type: messageType.ERROR, icon: 'exclamation-circle', message: err, }; this.getLogger('fail').info(JSON.stringify({ error: err, project: this.session.sessionProject, user: this.session.sessionUser, body: this.session.body, })); } if (this.helper.isAjax(this.request)) { if (err.stack) { this.body = {err: 4, msg: '标段数据未知错误', data: null}; } else { this.body = {err: 3, msg: err.toString(), data: null}; } } else { if (this.helper.isWap(this.request)) { this.redirect('/wap/subproj'); } else { this.postError(err, '未知错误'); err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : (err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/sp/' + this.subProject.id + '/payment')); } } } }; };