| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 | 'use strict';// 加密类const crypto = require('crypto');const messageType = require('../const/message_type');module.exports = options => {    /**     * session判断中间件     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* sessionAuth(next) {        try {            // 判断session            const sessionUser = this.session.sessionUser;            if (sessionUser === undefined) {                throw '不存在session';            }            // 校验session            if (sessionUser.account === undefined || sessionUser.loginTime === undefined) {                throw '用户数据不完整';            }            // 校验session            const sessionToken = crypto.createHmac('sha1', sessionUser.loginTime + '')                .update(sessionUser.account).digest('hex').toString('base64');            if (sessionToken !== sessionUser.sessionToken) {                throw 'session数据错误';            }            // 获取用户新建标段权利            const accountInfo = yield this.service.projectAccount.getDataById(this.session.sessionUser.accountId);            this.session.sessionUser.permission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;            const projectData = yield this.service.project.getDataById(this.session.sessionProject.id);            this.session.sessionProject.page_show = yield this.service.projectAccount.getPageShow(projectData.page_show);            this.session.sessionProject.custom = projectData.custom;            this.session.sessionProject.dataCollect = projectData.data_collect;            this.session.sessionProject.customType = projectData.customType;            this.session.sessionProject.funSet = projectData.fun_set ? JSON.parse(projectData.fun_set) : null;            // 判断是否有权限查看决策大屏            let showDataCollect = 0;            if (projectData.data_collect) {                if (sessionUser.is_admin) {                    showDataCollect = 1;                } else {                    const auditInfo = yield this.service.datacollectAudit.getDataByCondition({ pid: projectData.id, uid: accountInfo.id });                    if (auditInfo) {                        showDataCollect = 1;                    } else {                        let companyInfo = null;                        if (accountInfo.company_id) {                            companyInfo = yield this.service.datacollectAudit.getDataByCondition({                                pid: projectData.id,                                company_id: accountInfo.company_id,                            });                        } else {                            const cuInfo = yield this.service.constructionUnit.getDataByCondition({                                pid: projectData.id,                                name: accountInfo.company,                            });                            if (cuInfo) {                                companyInfo = yield this.service.datacollectAudit.getDataByCondition({                                    pid: projectData.id,                                    company_id: cuInfo.id,                                });                                yield this.service.projectAccount.update({ company_id: cuInfo.id }, { id: accountInfo.id });                            }                        }                        if (companyInfo) {                            showDataCollect = 1;                        } else {                            const grounpInfo = yield this.service.datacollectAudit.getGroupInfo(projectData.id, accountInfo.account_group);                            if (grounpInfo) {                                showDataCollect = 1;                            }                        }                    }                }            }            this.session.sessionProject.showDataCollect = showDataCollect;            // 判断是否有权限查看支付审批            let showPayment = 0;            if (sessionUser.is_admin) {                this.session.sessionProject.showSubProj = true;                this.session.sessionProject.showBudget = true;                showPayment = 1;            } else {                this.session.sessionProject.showSubProj = false;                this.session.sessionProject.showBudget = yield this.service.subProjPermission.showBudget(sessionUser.accountId);                // const grounpInfo = yield this.service.paymentPermissionAudit.getGroupInfo(projectData.id, accountInfo.account_group);                // if (grounpInfo) {                //     showPayment = 1;                // } else {                const auditInfo = yield this.service.paymentPermissionAudit.getDataByCondition({ pid: projectData.id, uid: accountInfo.id });                if (auditInfo) {                    showPayment = 1;                }                // }            }            this.session.sessionProject.showPayment = showPayment;            // 同步消息            yield this.service.notify.syncNotifyData();            // 同步系统维护信息            yield this.service.maintain.syncMaintainData();            if (this.session === null) {                throw '系统维护中~';            }            // 对sub_menu项目默认打开页进行配置            const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);            path && (this.curListUrl = path);            // 针对非wap重定向,去掉wap            if (this.method === 'GET' && this.url.match(/\/wap\//) && !this.helper.isMobile(this.request.header['user-agent'])) {                const returnUrl = this.url.replace(/\/wap/g, '');                this.redirect(returnUrl);            }        } catch (error) {            console.log(error);            this.log(error);            if (this.helper.isAjax(this.request)) {                return this.body = {                    err: 2,                    msg: '登录信息异常,请重新登录',                    data: '',                };            } else if (this.session === null) {                if (this.helper.isWap(this.request)) {                    this.session.wapTenderID = this.params.id ? this.params.id : null;                    return this.redirect('/wap/login?referer=' + this.url);                }                return this.redirect('/login?referer=' + this.url);            }            if (this.helper.isWap(this.request)) {                this.session.wapTenderID = this.params.id ? this.params.id : null;                return this.redirect('/wap/login?referer=' + this.url);            }            this.session.message = {                type: messageType.ERROR,                icon: 'exclamation-circle',                message: '登录信息异常,请重新登录',            };            return this.redirect('/login?referer=' + this.url);        }        yield next;    };};
 |