'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.session.sessionProject.page_show.openContract) { throw '该功能已关闭或无法查看'; } const stid = this.params.stid; const type = this.params.type ? contractConst.type[this.params.type] : contractConst.type.expenses; if (!stid) throw '参数错误'; let spid = null; let tid = null; // 判断stid字符串是不是只有数字 if (!/^\d+$/.test(stid)) { spid = stid; } else { tid = stid; } if (!spid && !tid) { throw '参数数据错误'; } const info = spid ? yield this.service.subProject.getDataById(spid) : yield this.service.tender.getDataById(tid); if (!info) throw '项目或标段不存在'; const options = spid ? { spid } : { tid }; 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; const result = yield this.service.contractAudit.getDataByCondition(cloneOptions); // 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; 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/list'); } else { this.postError(err, '未知错误'); err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect('/contract'); } } } }; };