auth-helper.js 2.21 KB
'use strict';
const aes = require('./aes-pwd');
const sign = global.yoho.sign;
const api = global.yoho.API;

class Auth {

    static signin(area, profile, password, shoppingKey) {
        let param = {
            method: 'app.passport.signin',
            area: area,
            profile: profile,
            password: password
        };

        if (shoppingKey) {
            param.shopping_key = shoppingKey;
        }

        return api.post('', param);
    }

    static signinAes(area, profile, password, shoppingKey) {
        let param = {
            method: 'app.passport.signinAES',
            area: area,
            profile: profile,
            password: aes.aesPwd(password)
        };

        if (shoppingKey) {
            param.shopping_key = shoppingKey;
        }

        return api.post('', param);
    }

    static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
        let param = {
            nickname: nickname,
            openId: openId,
            source_type: sourceType, // esline-disable-line
            method: 'app.passport.signinByOpenID',
            shoppingKey: shoppingKey
        };

        if (shoppingKey) {
            param.shopping_key = shoppingKey;
        }

        return api.get('', param);
    }

    static profile(uid) {
        let param = {
            uid: uid,
            method: 'app.passport.profile'
        };

        return api.get('', param);
    }

    static syncUserSession(uid, req, res) {
        return Auth.profile(uid).then((userInfo) => {
            let token = sign.makeToken(uid);
            let data = userInfo.data;

            if (data) {
                let uidCookie = `${data.profile_name}::${data.uid}::${data.vip_info.title}::${token}`;

                res.cookie('_UID', uidCookie, {
                    domain: 'yohobuy.com',
                    expires: new Date(Date.now() + 2592000000) // 有效期一年
                });
            }
            req.session._TOKEN = token;
            req.session._LOGIN_UID = uid;
            res.cookie('_TOKEN', token, {
                domain: 'yohobuy.com',
                expires: new Date(Date.now() + 2592000000) // 有效期一年
            });
        });
    }
}

module.exports = Auth;