auth-helper.js 3.32 KB
'use strict';

const md5 = require('md5');
const _ = require('lodash');

const cache = global.yoho.cache;
const sign = global.yoho.sign;
const api = global.yoho.API;
const config = global.yoho.config;

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);
    },
    signinByOtherOpenID(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, sourceType, shoppingKey, unionId) {
        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);
    },
    signinByOpenID(nickname, openId, sourceType, shoppingKey, unionId) {
        let signinFunc = {
            other: this.signinByOtherOpenID,
            wechat: this.signinByWechat
        };

        // PC 的微信登录之前使用了 open_id, 所以需要特别的接口处理
        let type = sourceType !== 'wechat' ? 'other' : sourceType;

        return signinFunc[type](nickname, openId, sourceType, shoppingKey, unionId);
    },
    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 user = userInfo.data;

            if (!_.isEmpty(user)) {
                let uidCookie = `{data.profile_name}::${user.uid}::${user.username}::${token}`;

                req.session._TOKEN = token;
                req.session._LOGIN_UID = uid;
                req.session._USERNAME = user.username;

                res.cookie('_UID', uidCookie, {
                    domain: config.cookieDomain
                });

                res.cookie('_USERNAME', user.username, {
                    domain: config.cookieDomain
                });
            }

            req.session._TOKEN = token; // esline-disable-line
            req.session._LOGIN_UID = uid; // esline-disable-line
            res.cookie('_TOKEN', token, {
                domain: config.cookieDomain
            }); // 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;