login-service.js 3.23 KB
'use strict';

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

const aes = require('./aes-pwd');
const cache = global.yoho.cache;
const sign = global.yoho.sign;
const cookie = global.yoho.cookie;
const config = global.yoho.config;
const crypto = global.yoho.crypto;

const Promise = require('bluebird');

const cartService = require('./cart-service');
const userService = require('./user-service');
const api = require('./login-api');

const signin = (type, area, profile, password, shoppingKey) => {
    let loginBy = {
        password: api.signinByPasswordWithAes,
        sms: api.signinBySMS,
        qrcode: api.signinByQrCode
    };

    return loginBy[type](area, profile, password, shoppingKey);
};

const syncUserSession = (uid, req, res, sessionKey) => {
    if (sessionKey) {
        cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
            global.yoho.logger.error('write session key fail');
        });
    }

    return Promise.all([userService.profile(uid), cartService.goodsCount(uid)]).spread((userInfo, count) => {
        let token = sign.makeToken(uid);
        let data = userInfo.data;
        let encryptionUid = aes.encryptionUid(data.uid);

        if (data) {
            let uidCookie = `${data.profile_name}::${encryptionUid}::${data.vip_info.title}::${token}`;
            let isStudent = data.vip_info.is_student || 0;

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

            // 购物车中商品的数量
            res.cookie('_g', JSON.stringify({
                _k: cookie.getShoppingKey(req),
                _nac: count,
                _ac: 0,
                _c: 1
            }), {
                domain: config.cookieDomain
            });
        }

        req.session.TOKEN_ = token;
        req.session.LOGIN_UID_ = uid;

        res.cookie('_TOKEN', token, {
            domain: config.cookieDomain,

        });

    }).catch(console.log);
};

const 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, {
        maxAge: aWeek,
        domain: config.cookieDomain
    });
    res.cookie('remem', rememKey, {
        maxAge: aWeek,
        domain: config.cookieDomain
    });

    // if (!cache.get(rememKey)) {
    //     cache.set(rememKey, accountInfo, aWeek);
    // }
};

const fetchByQrCode = () => {
    let code = {
        qrcode: uuid.v4(),
        time: new Date().getTime()
    };

    const encodeStr = _.flow(JSON.stringify, _.partial(crypto.encryption, null), encodeURIComponent);

    return Promise.resolve({
        data: {
            code: encodeStr(code)
        },
        code: 200
    });
};

module.exports = {
    signin,
    syncUserSession,
    rememberAccount,
    fetchByQrCode,
    checkByQrCode: api.checkByQrCode,
    sendPasswordBySMS: api.sendPasswordBySMS,
    checkUserExitBySMS: api.checkUserExitBySMS,
    verifyPasswordBySMS: api.verifyPasswordBySMS,
    signinByOpenID: api.signinByOpenID
};