login.js
1.21 KB
1
2
3
4
5
6
7
8
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
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);
}