user.js 3.87 KB
'use strict';

const _ = require('lodash');
const cookie = global.yoho.cookie;
const authcode = require('../../utils/authcode');
const config = global.yoho.config;

module.exports = () => {
    return (req, res, next) => {
        if (!req.yoho.isApp) {
            // 从 SESSION 中获取到当前登录用户的 UID
            if (req.session && _.isNumber(req.session.LOGIN_UID_)) {
                // 调用接口传参时切勿使用toString获得字符串
                req.user.uid = {
                    toString: () => {
                        return _.parseInt(req.session.LOGIN_UID_);
                    },
                    sessionKey: req.session.SESSION_KEY,
                    appSessionType: req.session.SESSION_TYPE
                };
                let userData = _.get(req.session, 'USER', {});

                _.merge(req.user, userData);
            }

            // session 没有读取到的时候,从 cookie 读取 UID
            if (!req.user.uid && req.cookies._UID) {
                let sessionKey = req.cookies._SESSION_KEY &&
                    authcode(req.cookies._SESSION_KEY, '_SESSION_KEY', 2592000000);

                // 调用接口传参时切勿使用toString获得字符串
                req.user.uid = {
                    toString: () => {
                        return _.parseInt(cookie.getUid(req));
                    },
                    sessionKey,
                    appSessionType: req.session.SESSION_TYPE
                };
            }
        } else {
            let params = req.method === 'POST' ? req.body : req.query;

            if (!req.user.uid &&
                (
                    (params.uid && params.uid !== '0') ||
                    (
                        req.cookies.app_uid &&
                        req.cookies.app_uid !== '0' &&
                        req.cookies.app_version &&
                        req.cookies.app_client_type
                    )
                )
            ) {
                let appUid = params.uid || req.cookies.app_uid;
                let appVersion = params.app_version || req.cookies.app_version || config.appVersion;
                let appSessionType = params.client_type || req.cookies.app_client_type;
                let sessionKey = params.session_key || req.cookies.app_session_key;

                req.user.uid = params.uid = {
                    toString: () => {
                        return _.parseInt(appUid);
                    },
                    appVersion: appVersion,
                    sessionKey: sessionKey,
                    appSessionType: appSessionType
                };

                // 此处 cookie 为了 pagecache 放到前端去写
                // res.cookie('app_uid', appUid.toString());
                // res.cookie('app_version', appVersion);
                // res.cookie('app_client_type', appSessionType);
            }

            if (!req.user.uid &&
                req.cookies.app_uid &&
                req.cookies.app_uid !== '0' &&
                req.cookies.app_session_key &&
                req.cookies.app_version &&
                req.cookies.app_client_type) {
                // 调用接口传参时切勿使用toString获得字符串
                req.user.uid = {
                    toString: () => {
                        return _.parseInt(req.cookies.app_uid);
                    },
                    sessionKey: req.cookies.app_session_key,
                    appVersion: req.cookies.app_version || params.app_version || config.appVersion,
                    appSessionType: req.cookies.app_client_type
                };
            }
        }

        res.locals.isLogin = Boolean(req.user.uid); // 用户是否登录
        if (_.get(req, 'user.uid.appSessionType') === 'miniapp') { // miniapp环境下调用接口使用web的version
            req.user.uid.appVersion = config.appVersion;
        }
        next();
    };
};