user.js 1.81 KB
'use strict';

const Fn = require('lodash/fp');
const cookie = global.yoho.cookie;

// const loginService = require('../../apps/passport/models/login-service');
// const _ = require('lodash');
// const cache = global.yoho.cache;

module.exports = () => {
    return (req, res, next) => {

        // 获得原始请求 url
        req.fullUrl = () => req.protocol + '://' + req.get('host') + req.originalUrl;

        // 从 cookie 读取 UID
        if (req.cookies._UID) {

            let uidCookie = req.cookies._UID.split('::');

            let getName = Fn.nth(0);
            let getVip = Fn.nth(2);
            let getToken = Fn.nth(3);

            req.user.name = getName(uidCookie);    // 0
            req.user.uid = cookie.getUid(req);     // 1
            req.user.vip = getVip(uidCookie);      // 2
            req.user.token = getToken(uidCookie);  // 3
            req.user.isStudent = req.cookies.isStudent || 0;
        }

        // 记住我
        // if (_.isEmpty(req.user) && req.cookies.isRemember === 'true' && req.cookies.remem) {
        //     return cache.get(req.cookies.remem).then((result) => {
        //         let data = JSON.parse(result || '{}');

        //         let area = data.area;
        //         let account = data.account;
        //         let password = data.password;

        //         return loginService.signin('password', area, account, password);
        //     }).then((result) => {
        //         if (result.code !== 200) {
        //             return Promise.reject();
        //         }

        //         return loginService.syncUserSession(result.data.uid, req, res);
        //     }).then(() => {
        //         return res.redirect(req.fullUrl());
        //     }).catch(next);
        // } else {
        //     return next();
        // }

        next();
    };
};