Blame view

apps/passport/models/auth-helper.js 2.66 KB
htoooth authored
1 2 3 4 5 6 7
'use strict';

const md5 = require('md5');

const cache = global.yoho.cache;
const sign = global.yoho.sign;
const api = global.yoho.API;
htoooth authored
8
const config = global.yoho.config;
htoooth authored
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

const Auth = {
    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);
    },
    signinByOpenID(nickname, openId, sourceType, shoppingKey) {
        let param = {
            nickname: nickname,
            openId: openId,
            source_type: sourceType,
            method: 'app.passport.signinByOpenID'
        };

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

        return api.get('', param);
    },
    signinByWechat(nickname, openId, unionId, sourceType, shoppingKey) {
        let param = {
            nickname: nickname,
            openId: openId,
            unionId: unionId,
            source_type: sourceType,
            method: 'app.passport.signinByWechat'
        };

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

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

        return api.get('', param);
    },
    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}`;

                req.session._TOKEN = token;
                req.session._LOGIN_UID = uid;

                res.cookie('_UID', uidCookie, {
htoooth authored
74
                    domain: config.cookieDomain
htoooth authored
75 76 77 78 79
                });
            }
            req.session._TOKEN = token; // esline-disable-line
            req.session._LOGIN_UID = uid; // esline-disable-line
            res.cookie('_TOKEN', token, {
htoooth authored
80
                domain: config.cookieDomain
htoooth authored
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            }); // esline-disable-line
        }).catch(console.log);
    },
    rememberAccount(accountInfo, req, res) {
        let aWeek = (new Date()).getTime() / 1000 + 504000; // 504000-一周
        let rememKey = md5(md5(accountInfo.account + accountInfo.password + accountInfo.area));

        res.cookie('isRemember', true, aWeek);
        res.cookie('remem', rememKey, aWeek);
        if (!cache.get(rememKey)) {
            cache.set(rememKey, accountInfo, aWeek);
        }
    }
};

module.exports = Auth;