login-api.js 2.75 KB
/**
 * Created by TaoHuang on 2016/7/25.
 */

'use strict';
const aes = require('./aes-pwd');
const api = global.yoho.API;

/**
 * 手机或邮箱登录 app.passport.signin => app.passport.signinAES 加密
 */
const signinAsync = (area, profile, password, shoppingKey, req) => {
    let param = {
        method: 'app.passport.signinAES',
        area: area,
        profile: profile,
        password: aes.aesPwd(password)
    };

    let ip = req.header('x-forwarded-for').split(',');

    let uaIp = {
        'user-agent': req.headers['user-agent'],
        'X-YOHO-IP': ip.length ? ip[ip.length - 1] : ''
    };

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

    return api.post('', param, { headers: uaIp });
};

/**
 * 手机短信登录
 * @param area
 * @param mobile
 * @param token
 * @param shoppingKey
 * @returns {*|Promise}
 */
const signinBySMS = (area, mobile, token, shoppingKey) => {
    let param = {
        method: 'app.passport.autoSignin',
        area: area,
        profile: mobile,
        code: token
    };

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

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

/**
 * 除微信第三方登录
 */
const signinByOtherOpenIDAsync = (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);
};

/**
 * 微信第三方登录
 */
const signinByWechatAsync = (nickname, openId, sourceType, shoppingKey, unionId) => {
    let param = {
        nickname: nickname,
        openId: openId,
        unionId: unionId,
        source_type: sourceType,
        method: 'app.passport.signinByOpenID'
    };

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

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

const sendPasswordBySMS = (area, mobile) => {
    let param = {
        method: 'app.message.sendSms',
        area: area,
        mobile: mobile,
        type: 1 // 手机快捷登录短信验证码
    };

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

const verifyPasswordBySMS = (area, mobile, code) => {
    return api.get('', {
        method: 'app.message.verifySmsCode',
        area: area,
        mobile: mobile,
        code: code,
        type: 1 // 手机快捷登录短信验证码
    });
};

const checkUserExitBySMS = (area, mobile) => {
    return api.get('', {
        method: 'app.passport.checkUserExist',
        area: area,
        mobile: mobile
    });
};

module.exports = {
    signinAsync,
    signinBySMS,
    signinByOtherOpenIDAsync,
    signinByWechatAsync,
    sendPasswordBySMS,
    verifyPasswordBySMS,
    checkUserExitBySMS
};