base.js
4.89 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import jsonp from "../utils/jsonp";
import cookies from '../utils/cookie';
import {getQueryObj} from '../utils/common'
import Promise from 'promise-polyfill'
import {invokeMethod} from '../utils/common'
const parseUrl = (url) => {
let query = {},
hashs,
hash,
i;
url = (url || '').split('?');
hashs = (url[1] || '').split('&');
if (hashs && hashs.length) {
for (i = 0; i < hashs.length; i++) {
hash = hashs[i].split('=');
query[hash[0]] = hash[1];
}
}
return {
path: url[0],
query: query
}
};
let env = '';
const _appGetUid = function () {
return new Promise(function (resolve) {
invokeMethod({
method: 'get.uid',
arguments: {},
success: function (uid) {
resolve(parseInt(uid));
},
fail: function () {
let uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
resolve(uid);
}
});
})
}
const _appGetSessionKey = function () {
return new Promise(function (resolve) {
invokeMethod({
method: 'get.sessionId',
arguments: {},
success: function (sessionKey) {
resolve(sessionKey);
},
fail: function () {
let sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';
resolve(sessionKey);
}
});
})
}
export default {
//evn
env: function () {
let envFlag = window.__wxjs_environment;
if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
env = 'app';
document.addEventListener('deviceready', function () {
});
} else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag === 'miniprogram')) {
env = 'miniprogram';
} else if (location.origin === 'https://www.yohobuy.com') {
env = 'pc'
} else {
env = 'h5'
}
return env
},
//url
loginUrl: function () {
let url, refer = location.href;
if (env === 'app') {
url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
refer = parseUrl(refer);
if (/Android/i.test(navigator.userAgent || '')) {
if (url.indexOf('?') < 0) {
url += '?appLogin=1';
}
if (location.href.indexOf('&openby') >= 0) {
url = url.substring(0, url.indexOf('&openby'));
}
url += '&';
} else {
if (location.href.indexOf('#openby') >= 0) {
url = url.substring(0, url.indexOf('#openby'));
}
url += '#';
}
url += 'openby:yohobuy=' + JSON.stringify({
action: 'go.weblogin',
params: {
priority: 'N',
jumpurl: {
url: refer.path,
param: refer.query
},
needlogout: 'Y'
}
});
} else if (env === 'pc') {
url = 'https://www.yoho/signin.html?refer=' + encodeURIComponent(location.href);
} else if (env === 'miniprogram') {
url = '/pages/userCenter/userCenter'
} else {
if (refer.indexOf('?') < 0) {
refer += "?skcall=1"
} else {
refer += "&skcall=1"
}
url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
}
return url;
},
//uid
getUser: function () {
return new Promise(function (resolve) {
let obj = {
sessionType: cookies.cookie('app_client_type') || '',
appVersion: cookies.cookie('app_version') || ''
}
if (env === 'h5' || env === 'pc') {
jsonp('//m.yohobuy.com/passport/login/user').then(r => {
obj.uid = 0;
if (r.code === 200) {
obj.uid = Number(r.data)
}
obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';
resolve(obj);
});
} else if (env === 'app') {
return Promise.all([_appGetUid(), _appGetSessionKey()]).then(function (r) {
obj.uid = r[0]
obj.sessionKey = r[1]
resolve(obj);
})
} else {
obj.uid = Number(cookies.cookie('app_uid')) || Number(getQueryObj().uid) || 0;
obj.sessionKey = cookies.cookie('app_session_key') || getQueryObj().session_key || '';
resolve(obj);
}
})
}
}