'use strict'; /** * * * @author Ellisran * @date 2020/10/15 * @version */ const status = require('../const/audit').inspection.status; const shenpiConst = require('../const/shenpi'); const _ = require('lodash'); module.exports = options => { /** * 标段校验 中间件 * 1. 读取标段数据(包括属性) * 2. 检验用户是否可见标段(不校验具体权限) * * @param {function} next - 中间件继续执行的方法 * @return {void} */ return function* InspectionCheck(next) { try { // 获取revise if (!this.subProject.page_show.quality) { throw '该功能已关闭'; } const qiid = this.params.qiid || this.request.body.qiid; if (!qiid) { throw '您访问的质量巡检不存在'; } const inspection = yield this.service.qualityInspection.getDataById(qiid); if (!inspection) throw '质量巡检数据有误'; // 读取原报、审核人数据 yield this.service.qualityInspection.loadUser(inspection); // 权限相关 // todo 校验权限 (标段参与人、分享) const accountId = this.session.sessionUser.accountId, auditorIds = _.map(inspection.auditors, 'aid'), shareIds = []; const permission = this.session.sessionUser.permission; if (accountId === inspection.uid) { // 原报 inspection.filePermission = true; } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人 if (inspection.status === status.uncheck) { throw '您无权查看该数据'; } inspection.filePermission = true; } else if (inspection.status === status.checkNo && inspection.uid !== accountId) { const preAuditors = yield this.service.qualityInspectionAudit.getAuditors(inspection.id, inspection.times - 1); const preAuditorIds = _.map(preAuditors, 'aid'); if (preAuditorIds.indexOf(accountId) === -1) { throw '您无权查看该数据'; } inspection.filePermission = true; } else if (this.tender.isTourist || this.session.sessionUser.is_admin) { inspection.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1; } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人 if (inspection.status === status.uncheck) { throw '您无权查看该数据'; } inspection.filePermission = false; } else { // 其他不可见 throw '您无权查看该数据'; } // 调差的readOnly 指表格和页面只能看不能改,和审批无关 inspection.readOnly = !((inspection.status === status.uncheck || inspection.status === status.checkNo) && accountId === inspection.uid); inspection.rectificationPower = inspection.status === status.rectification && inspection.curAuditorIds.indexOf(accountId) !== -1; inspection.shenpiPower = (inspection.status === status.checking || inspection.status === status.checkNoPre) && inspection.curAuditorIds.indexOf(accountId) !== -1; this.inspection = inspection; // 根据状态判断是否需要更新审批人列表 if ((inspection.status === status.uncheck || inspection.status === status.checkNo) && this.tender.info.shenpi.inspection !== shenpiConst.sp_status.sqspr) { const shenpi_status = this.tender.info.shenpi.inspection; // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审 const auditList = yield this.service.qualityInspectionAudit.getAllDataByCondition({ where: { qiid: inspection.id, times: inspection.times, is_rectification: 0 }, orders: [['order', 'asc']] }); if (shenpi_status === shenpiConst.sp_status.gdspl) { const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: inspection.tid, sp_type: shenpiConst.sp_type.inspection, sp_status: shenpi_status } }); // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流 let sameAudit = auditList.length === shenpiList.length; if (sameAudit) { for (const audit of auditList) { const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; }); if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) { sameAudit = false; break; } } } if (!sameAudit) { yield this.service.qualityInspectionAudit.updateNewAuditList(inspection, shenpiList); yield this.service.qualityInspection.loadUser(inspection); } } else if (shenpi_status === shenpiConst.sp_status.gdzs) { const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: inspection.tid, sp_type: shenpiConst.sp_type.inspection, sp_status: shenpi_status }); // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审 const lastAuditors = auditList.filter(x => { x.order === auditList[auditList.length - 1].order; }); if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) { yield this.service.qualityInspectionAudit.updateLastAudit(inspection, auditList, shenpiInfo.audit_id); yield this.service.qualityInspection.loadUser(inspection); } else if (!shenpiInfo) { // 不存在终审人的状态下这里恢复为授权审批人 this.tender.info.shenpi.inspection = shenpiConst.sp_status.sqspr; } } } yield next; } catch (err) { console.log(err); // 输出错误到日志 if (err.stack) { this.logger.error(err); } else { this.getLogger('fail').info(JSON.stringify({ error: err, project: this.session.sessionProject, user: this.session.sessionUser, body: this.session.body, })); } // 重定向值标段管理 this.redirect(this.request.headers.referer); } }; };