yoho-app.js
7.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* YOHO-SDK
*
* 与原生 APP 交互的代码
* 所有函数要做降级处理
* 假如不是 YOHO App,在浏览器实现对应的功能
* 浏览器不支持的功能,给出提示,控制台不能报错,不影响后续代码执行
*
* 希望能与 微信 JS-SDK 一样方便
* http://git.yoho.cn/mobile/AppJSBridge/blob/master/AppH5Bridge.md
* http://git.yoho.cn/mobile/appbridge old、sick、ugly
*/
let $ = require('yoho-jquery');
let tip = require('./plugin/tip');
let cookie = require('yoho-cookie');
require('js/common');
/* 空方法 */
let emptyFn = function() {};
/* 提示信息 */
let tipInfo = '暂不支持,请在YOHO!BUY应用中打开';
let unknownInfo = '未知异常,请刷新页面后重试';
let qs = window.queryString;
let yoho, $appLink;
$appLink = $('#yoho-app-link');
if (!$appLink.length) {
$('body').append('<a id="yoho-app-link" href="javascript:;" style="display:none !important;"></a>');
$appLink = $('#yoho-app-link');
}
const isiPhoneXSeries = () => {
console.log(navigator.userAgent);
let isiOS = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(navigator.userAgent || '');
if (!window || !isiOS) {
return false;
}
const xSeriesConfig = [{ // iPhone Xs(Max,Pro Max)
devicePixelRatio: 3,
width: 414,
height: 896,
},
{ // iPhone XR(11)
devicePixelRatio: 2,
width: 414,
height: 896,
},
{ // iPhone X(Xs,Pro)
devicePixelRatio: 3,
width: 375,
height: 812,
},
];
const {
devicePixelRatio,
screen: {
width,
height
}
} = window;
// eslint-disable-next-line max-len
return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height);
};
yoho = {
/**
* 判断是否是 APP
*/
isNowApp: /yohonow/i.test(navigator.userAgent || ''),
isMarsApp: /YohoMars/i.test(navigator.userAgent || ''),
isYohoApp: /YohoBuy/i.test(navigator.userAgent || ''),
isMiniApp: /miniProgram/i.test(navigator.userAgent || '') || qs && qs.client_type === 'miniapp',
isApp: /YohoMars/i.test(navigator.userAgent || '') ||
/YohoBuy/i.test(navigator.userAgent || '') ||
/yohonow/i.test(navigator.userAgent || '') ||
qs && qs.app_version,
isiOS: /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(navigator.userAgent || ''),
isAndroid: /Android/i.test(navigator.userAgent || ''),
isWechat: /micromessenger/i.test(navigator.userAgent || ''),
isiPhoneXSeries: isiPhoneXSeries() || false,
/**
* JS 与 APP 共享的对象
*/
data: window.yohoInterfaceData,
ready: function(callback) {
// App 未注入 yohoInterface 的时候,延迟执行
if (this.isApp && !window.yohoInterface) {
document.addEventListener('deviceready', callback);
} else {
return callback();
}
},
/**
* 调用APP原生方法
* @param method 方法名称
* @param args 传递给 APP 的参数 {"index":tab_index}
* @param success 调用成功的回调方法
* @param fail 调用失败的回调方法
*/
invokeMethod: function(method, args, success, fail) {
let appInterface = window.yohoInterface;
if (this.isApp) {
if (appInterface) {
appInterface.triggerEvent(success || emptyFn, fail || emptyFn, {
method: method,
arguments: args
});
} else {
tip.show(unknownInfo);
}
} else {
tip.show(tipInfo);
}
},
/**
* 原生调用 JS 方法
* @param name 方法名
* @param callback 回调
*/
addNativeMethod: function(name, callback) {
let appInterface = window.yohoInterface;
// 延迟 500ms 注入
setTimeout(function() {
if (appInterface) {
appInterface[name] = callback;
}
}, 500);
},
parseUrl: function(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
};
},
getClientUserInfo: function() {
return new Promise((resolve, reject) => {
let userInfo = {};
let successFn = function(info) {
$.extend(userInfo, info);
if (userInfo.hasOwnProperty('uid') && userInfo.hasOwnProperty('session_key')) {
return resolve(userInfo);
}
};
this.invokeMethod('get.uid', {}, uid => {
successFn({uid: uid});
}, () => {
return reject();
});
this.invokeMethod('get.sessionId', {}, sessionId => {
successFn({session_key: sessionId});
}, () => {
return reject();
});
});
},
getUid: function() {
if (yoho.isApp) {
return qs.uid || cookie.get('_YOHOUID') || cookie.get('app_uid');
}
return window.getUid();
},
/**
* 判断是否是 登录
*/
isLogin: function() {
return this.getUid();
},
goLogin: function(refer, data) {
if (this.isMiniApp) {
return false;
}
let url;
if (refer.indexOf('//') === 0) {
refer = location.protocol + refer;
}
url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
refer = yoho.parseUrl(refer || location.href);
if (this.isApp) {
url = location.href;
if (this.isAndroid) {
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=' + (data || JSON.stringify({
action: 'go.weblogin',
params: {
priority: 'N',
jumpurl: {
url: refer.path,
param: refer.query
},
needlogout: 'Y'
}
}));
}
$appLink.attr('href', url);
$appLink[0].click();
return false;
},
goH5: function(link, data) {
let url = link;
if (!link) {
return;
}
link = yoho.parseUrl(link || location.href);
if (yoho.isApp) {
url = url + '&openby:yohobuy=' + (data || JSON.stringify({
action: 'go.h5',
params: {
islogin: 'N',
type: 0,
updateflag: Date.now() + '',
url: link.path,
param: link.query
}
}));
}
$appLink.attr('href', url);
$appLink[0].click();
return false;
},
goHome: function() {
var url = '//m.yohobuy.com/';
if (yoho.isApp) {
url = url + '?openby:yohobuy=' + JSON.stringify({
action: 'go.home'
});
}
$appLink.attr('href', url);
$appLink[0].click();
return false;
}
};
module.exports = yoho;