'use strict'; /** * * * @author Mai * @date * @version */ const messageType = require('../const/message_type'); const _ = require('lodash'); const contractConst = require('../const/contract'); module.exports = options => { /** * 标段校验 中间件 * 1. 读取标段数据(包括属性) * 2. 检验用户是否可见标段(不校验具体权限) * * @param {function} next - 中间件继续执行的方法 * @return {void} */ return function* contractCheck(next) { try { if (!this.subProject.page_show.openContract) { throw '该功能已关闭或无法查看'; } const tid = this.params.tid || null; const type = this.params.type ? contractConst.type[this.params.type] : contractConst.type.expenses; const info = tid ? yield this.service.tender.getDataById(tid) : this.subProject; if (!info) throw '项目或标段不存在'; const options = tid ? { tid } : { spid: this.subProject.id }; if (this.request.originalUrl && this.request.originalUrl.indexOf('detail') > -1) yield this.service.contractTree.insertTree(options, info); // 权限控制 const cloneOptions = _.cloneDeep(options); cloneOptions.uid = this.session.sessionUser.accountId; let result = false; if (tid) { result = yield this.service.contractAudit.getDataByCondition(cloneOptions); } else { const contractPermission = this.subProject.permission.contract_permission; if (contractPermission.length > 0 && _.intersection([3, 4, 5], contractPermission).length > 0) { result = yield this.service.subProjPermission.getContractPermission(contractPermission); } } // const result = yield this.service.contractAudit.checkPermission(options, this.session.sessionUser.accountId); if (!result && !this.session.sessionUser.is_admin) { throw '当前账号权限不足,请联系管理员添加权限'; } this.contract = info; this.contractOptions = options; this.contract_audit_permission = result; this.contract_type = type; this.contract_tender = !!tid; 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') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect(`/sp/${this.subProject.id}/dashboard`); } } } }; };