login.js 1.21 KB
import qs from 'yoho-qs';
import cookie from 'yoho-cookie';
import jsonp from 'jsonp';
import 'whatwg-fetch';

const uid = qs.yh_uid;
let baseUrl;

if (process.env.NODE_ENV === 'production') {
    baseUrl = 'https://action.yoho.cn';
} else {
    baseUrl = 'http://localhost:6006';
}

const loginLog = userId => {
    if (!cookie.get('_loginLog')) {
        fetch(`${baseUrl}/user/loginLog`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                uid: userId
            })
        }).then(res => {
            if (res.ok) {
                cookie.set('_loginLog', true);
            }
        });
    }
};

if (!uid) {
    jsonp('http://m.yohobuy.com/passport/login/user', {}, (errMsg, userRes) => {
        if (userRes.code !== 200) {
            fetch(`${baseUrl}/user/getLoginUrl?url=${window.location.href}`).then(res => {
                if (res.ok) {
                    res.json().then(data => {
                        window.location.href = data.loginUrl;
                    });
                }
            });
        } else {
            loginLog(userRes.data);
        }
    });
} else {
    loginLog(uid);
}