open-app.js
2.81 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
const getAppPath = () => {
let appPath = document.getElementById('main-wrap').dataset.apppath || 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"gender":"1","channel":"2"}}';
return appPath;
};
const u = navigator.userAgent;
const isFromYOHO = /m\.yohobuy\.com/i.test(document.referrer);
const isApp = /yoho/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 nodownload = document.getElementById('no-download'); // 页面不需要下载
const getIOSVersion = () => {
const verion = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
return verion ? parseInt(verion[1], 10) : 0;
};
const ua = navigator.userAgent;
const isOriginalChrome = /chrome\/[\d.]+ Mobile Safari\/[\d.]+/i.test(ua) &&
isAndroid &&
ua.indexOf('Version') < 0;
const getFallUrl = (schemeUrl) => {
const appPath = (schemeUrl || '').replace('yohobuy://yohobuy.com/goapp?', '') || 'openby:yohobuy={"action":"go.home","params":{"gender":"1","channel":"2"}}';
return `https://union.yoho.cn/union/app-downloads.html?${appPath}`;
};
const checkOpenFall = (url, callFunc, noFall) => {
let time = Date.now();
callFunc();
const fallUrl = getFallUrl(url);
if (noFall) {
return;
}
window.setTimeout(function() {
if (Date.now() - time < 1200) {
window.location.href = fallUrl;
}
}, 1000);
};
const callIframe = (url, noFall) => {
checkOpenFall(url, () => {
const ifr = document.createElement('iframe');
ifr.src = url;
ifr.style.display = 'none';
document.body.appendChild(ifr);
}, noFall);
};
const callUrl = (url, noFall) => {
checkOpenFall(url, () => {
window.location.href = url;
}, noFall);
};
const callA = (url, noFall) => {
checkOpenFall(url, () => {
const ca = document.createElement('a');
ca.setAttribute('href', url);
ca.style.display = 'none';
document.body.appendChild(ca);
ca.click();
}, noFall);
};
const toAppPage = (appUrl, noFall) => {
if (isOriginalChrome) {
callA(appUrl, noFall);
} else if (isiOS) {
if (getIOSVersion() < 9) {
callIframe(appUrl, noFall);
} else {
callUrl(appUrl, noFall);
}
} else {
callIframe(appUrl, noFall);
}
};
const canOpenApp = () => {
if (isWechatDevtool || isApp || isFromYOHO || nodownload) {
return false;
}
return isAndroid || isiOS;
};
export default () => {
return new Promise((resolve) => {
if (canOpenApp()) {
setTimeout(function() {
let appPath = getAppPath();
toAppPage(appPath, true);
resolve();
}, 2000);
} else {
resolve();
}
});
};