| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | 'use strict';/** * * * @author Mai * @date * @version */const auditConst = require('../const/audit').ledger;const messageType = require('../const/message_type');module.exports = options => {    /**     * 标段校验 中间件     * 1. 读取标段数据(包括属性)     * 2. 检验用户是否可见标段(不校验具体权限)     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* tenderCheck(next) {        try {            // 读取标段数据            const tender = { id: parseInt(this.params.id) };            if (!tender.id) {                throw '当前未打开标段';            }            tender.data = yield this.service.tender.getTender(tender.id);            if (!tender.data) {                throw '标段数据错误';            }            if (!tender.data.measure_type) {                throw '请先选择计量模式';            }            tender.info = yield this.service.tenderInfo.getTenderInfo(tender.id);            if (!tender.data.ledger_status) {                tender.data.ledger_status = auditConst.status.uncheck;            }            tender.auditLedgerConst = auditConst;            if (!tender.data.ledger_times) {                tender.data.ledger_times = 1;            }            if (tender.data.project_id !== this.session.sessionProject.id) {                throw '您无权查看该项目';            }            const accountId = this.session.sessionUser.accountId;            const advanceAuditors = yield this.service.advanceAudit.getAllAuditors(tender.id);            const advanceAuditorsId = this.helper._.map(advanceAuditors, 'audit_id');            const times = tender.data.ledger_status === auditConst.status.checkNo ? tender.data.ledger_times - 1 : tender.data.ledger_times;            const auditors = yield this.service.ledgerAudit.getAuditors(tender.id, times);            const auditorsId = this.helper._.map(auditors, 'audit_id');            const stageAuditors = yield this.service.stageAudit.getAllAuditors(tender.id);            const stageAuditorsId = this.helper._.map(stageAuditors, 'aid');            const changeAuditors = yield this.service.changeAudit.getAllAuditors(tender.id);            const changeAuditorsId = this.helper._.map(changeAuditors, 'uid');            const reviseAuditors = yield this.service.reviseAudit.getAllAuditors(tender.id);            const reviseAuditorsId = this.helper._.map(reviseAuditors, 'audit_id');            const materialAuditors = yield this.service.materialAudit.getAllAuditors(tender.id);            const materialAuditorsId = this.helper._.map(materialAuditors, 'aid');            const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;            if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&                (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&                stageAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&                reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1 &&                advanceAuditorsId.indexOf(accountId) === -1) {                throw '您无权查看该项目';            }            tender.ledgerReadOnly = this.session.sessionUser.accountId !== tender.data.user_id ||                tender.data.ledger_status === auditConst.status.checking || tender.data.ledger_status === auditConst.status.checked;            tender.advanceAuditorsId = advanceAuditorsId;            this.tender = tender;            this.session.sessionProject.page_show = yield this.service.project.getPageshow(this.session.sessionProject.id);            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: 2, msg: '标段数据未知错误', data: null};                } else {                    this.body = {err: 1, msg: err.toString(), data: null};                }            } else {                if (this.helper.isWap(this.request)) {                    this.redirect('/wap/list');                } else {                    err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/list');                }            }        }    };};
 |