123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit').ledger;
- const messageType = require('../const/message_type');
- const scPermission = require('../const/schedule').permission;
- const buildStatus = require('../const/tender').buildStatus;
- 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.readOnly = tender.data.build_status === buildStatus.status.finish;
- 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 '您无权查看该项目';
- }
- tender.his = tender.data.ledger_status === auditConst.status.checkNo
- ? yield this.service.ledgerHistory.getDataById(tender.data.his_id)
- : null;
- const subProject = yield this.service.subProject.getDataById(tender.data.spid);
- 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');
- let auditAssists = yield this.service.auditAss.getData(tender.id);
- auditAssists = auditAssists.filter(x => {
- return x.user_id === tender.data.user_id || stageAuditorsId.indexOf(x.user_id) >= 0;
- });
- const auditAssistsId = this.helper._.map(auditAssists, 'ass_user_id');
- tender.userAssistsId = auditAssists.filter(x => {return x.user_id === tender.data.user_id;})
- .map(x => { return x.ass_user_id; });
- const settleAuditors = yield this.service.settleAudit.getAllAuditors(tender.id);
- const settleAuditorsId = this.helper._.map(settleAuditors, 'audit_id');
- const phasePayAuditors = yield this.service.phasePayAudit.getAllAuditors(tender.id);
- const phasePayAuditorsId = this.helper._.map(phasePayAuditors, 'audit_id');
- 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 changeProjectAuditors = subProject.page_show.openChangeProject ? yield this.service.changeProjectAudit.getAllAuditors(tender.id) : [];
- const changeProjectAuditorsId = this.helper._.map(changeProjectAuditors, 'aid');
- const changeProjectXsAuditors = subProject.page_show.openChangeProject ? yield this.service.changeProjectXsAudit.getAllAuditors(tender.id) : [];
- const changeProjectXsAuditorsId = this.helper._.map(changeProjectXsAuditors, 'aid');
- const changeApplyAuditors = subProject.page_show.openChangeApply ? yield this.service.changeApplyAudit.getAllAuditors(tender.id) : [];
- const changeApplyAuditorsId = this.helper._.map(changeApplyAuditors, 'aid');
- const changePlanAuditors = subProject.page_show.openChangePlan ? yield this.service.changePlanAudit.getAllAuditors(tender.id) : [];
- const changePlanAuditorsId = this.helper._.map(changePlanAuditors, 'aid');
- const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;
- const isTenderTourist = yield this.service.tenderTourist.getDataByCondition({ tid: tender.id, user_id: accountId });
- const scheduleUser = subProject.page_show.xxjd ? yield this.service.scheduleAudit.getDataByCondition({ tid: tender.id, audit_id: this.session.sessionUser.accountId }) : [];
- // 判断访问人是否具有游客身份
- tender.isTourist = isTenderTourist !== null;
- // 游客权限
- tender.touristPermission = yield this.service.tenderTourist.getTouristPermission(isTenderTourist);
- if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&
- (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&
- stageAuditorsId.indexOf(accountId) === -1 && auditAssistsId.indexOf(accountId) === -1 &&
- settleAuditorsId.indexOf(accountId) === -1 && phasePayAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&
- reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1 &&
- changeProjectAuditorsId.indexOf(accountId) === -1 && changeProjectXsAuditorsId.indexOf(accountId) === -1 &&
- changeApplyAuditorsId.indexOf(accountId) === -1 && changePlanAuditorsId.indexOf(accountId) === -1 &&
- advanceAuditorsId.indexOf(accountId) === -1 && !this.session.sessionUser.is_admin && !isTenderTourist &&
- (!scheduleUser || scheduleUser.permission === scPermission.no)) {
- 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;
- tender.ledgerUsers = tender.ledger_status === auditConst.status.uncheck ? [tender.data.user_id] : [tender.data.user_id, ...auditorsId];
- this.tender = tender;
- // this.session.sessionProject.page_show = yield this.service.project.getPageshow(this.session.sessionProject.id);
- // 投资进度权限获取
- let schedule_permission = scPermission.no;
- if (this.session.sessionUser.accountId === tender.data.user_id) {
- schedule_permission = scPermission.edit;
- } else {
- if (scheduleUser) {
- if (tender.isTourist && scheduleUser.permission === scPermission.no) {
- schedule_permission = scPermission.show;
- } else {
- schedule_permission = scheduleUser.permission;
- }
- } else if (tender.isTourist) {
- schedule_permission = scPermission.show;
- }
- }
- tender.schedule_permission = schedule_permission;
- yield next;
- } catch (err) {
- console.log(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 {
- err === '您无权查看该内容'
- ? this.redirect(this.request.headers.referer)
- : (this.tender && this.tender.spid ? this.redirect(`/sp/${this.tender.spid}/list`) : this.redirect('/subproj'));
- }
- }
- }
- };
- };
|