open-app.js
3.11 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
/**
* 移动端尝试打开 app
*/
const qs = require('yoho-qs');
const cookie = require('yoho-cookie');
const u = navigator.userAgent;
const isFromYOHO = /m\.yohobuy\.com/i.test(document.referrer);
const isApp = /yohobuy/i.test(u) ||
!!window.yohoInterface ||
/app_version=/i.test(location.search) ||
/openrefer=/i.test(location.search);
const isiOS = /(iPhone|iPad|iPod|iOS)/i.test(u); // ios终端
const isAndroid = /Android/i.test(u); // android终端
const isWechatDevtool = /wechatdevtools/i.test(u); // 微信开发者工具
// const iOSVersion = parseInt((u.match(/OS (\d+)_(\d+)_?(\d+)?/i) || [])[1], 10); // iOS 版本
const nodownload = document.getElementById('no-download'); // 页面不需要下载
const urlBlacklist = ['m.yohobuy.com/brands', 'm.yohobuy.com/passport'];
const blackCheck = urlBlacklist.some(function(url) {
return new RegExp(url, 'i').test(location.href);
});
/**
* 从referrer中获取信息,设定mktcode
*/
const channelMap = {
'baidu.com': 100000000000055,
'so.com': 100000000000049,
'sogou.com': 100000000000053,
'bing.com': 100000000000057,
'm.sm.cn': 100000000000059,
'google.com': 100000000000061
};
const getMktcBySeo = function() {
var mktc,
rf = document.referrer;
for (let domain in channelMap) {
if (rf.indexOf(domain) > -1) {
mktc = channelMap[domain];
break;
}
}
return mktc;
};
const canOpenApp = () => {
if (isWechatDevtool || isApp || isFromYOHO || nodownload || qs.nodownload || qs.no_openapp || blackCheck) {
return false;
}
return isAndroid || isiOS || qs.openapp;
};
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 || getMktcBySeo() || '100000000000349';
let clientId = cookie.get('_yasvd');
if (ct) {
appPath = appPath.replace('goapp?', 'goapp?ct=' + ct + '&');
}
if (clientId) {
appPath = appPath.replace('goapp?', 'goapp?client_id=' + clientId + '&');
}
return appPath;
};
if (canOpenApp()) {
setTimeout(function() {
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]);
}
}, 1000);
setTimeout(function() {
let appPath = getAppPath();
let ifr;
if (isiOS) {
window.location.href = appPath;
} else {
ifr = document.createElement('iframe');
ifr.src = appPath;
ifr.style.display = 'none';
document.body.appendChild(ifr);
}
}, 2000);
}