open-app.js
1.54 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
/**
* 移动端尝试打开 app
*/
const qs = require('yoho-qs');
const getAppPath = () => {
let appPath = document.getElementById('main-wrap').dataset.apppath || 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"gender":"1","channel":"2"}}';
let ct = qs.mkt_code || qs.union_type;
if (ct) {
appPath = appPath.replace('goapp?', 'goapp?ct=' + ct + '&');
}
return appPath;
};
const u = navigator.userAgent;
const isFromYOHO = /m\.yohobuy\.com/i.test(document.referrer);
const isiOS = /(iPhone|iPad|iPod|iOS)/i.test(u); // ios终端
const isAndroid = /Android/i.test(u); // android终端
const iOSVersion = parseInt((u.match(/OS (\d+)_(\d+)_?(\d+)?/i) || [])[1], 10);
if (!isFromYOHO && !qs.no_openapp && (isAndroid || iOSVersion < 9 || qs.openapp)) {
let appPath = getAppPath();
let ifr;
if (window._yas && window._yas.sendCustomInfo) {
window._yas.sendCustomInfo({
op: 'YB_H5_AWAKE_APP',
param: JSON.stringify({
PAGE_NAME: encodeURIComponent(document.title),
PAGE_URL: encodeURIComponent(location.href)
})
}, true);
}
if (window._hmt && window._hmt.push) {
window._hmt.push(['_trackEvent', 'H5唤起APP', isiOS ? 'Apple' : 'Android', document.title, location.href]);
}
if (isiOS) {
window.location.href = appPath;
} else {
ifr = document.createElement('iframe');
ifr.src = appPath;
ifr.style.display = 'none';
document.body.appendChild(ifr);
}
}