user.js 905 Bytes
'use strict';

const _ = require('lodash');
const cookie = global.yoho.cookie;

module.exports = () => {
    return (req, res, next) => {
        // 从 SESSION 中获取到当前登录用户的 UID
        if (req.session && _.isNumber(req.session.LOGIN_UID)) {
            req.user.uid = req.session.LOGIN_UID;

            let userData = _.get(req.session, 'USER', {});

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

        // session 没有读取到的时候,从 cookie 读取 UID
        if (!req.user.uid && req.cookies._UID) {
            req.user.uid = cookie.getUid(req);
        }

        // app 特殊读法
        if (!req.user.uid && req.yoho.isApp) {
            let userAgent = _.get(req.headers, 'user-agent', '');

            if (userAgent.indexOf('YohoBuy') >= 0) {
                req.user.uid = req.cookies._YOHOUID || 0;
            }
        }

        next();
    };
};