forked from suanmei/callapp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (127 loc) · 3.54 KB
/
index.js
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
/**
* @author suanmei <[email protected]>
*/
import { getIOSVersion, getWeChatVersion, getBrowser } from './sources/browser';
import * as generate from './sources/generate';
import {
evokeByLocation,
evokeByIFrame,
evokeByTagA,
checkOpen,
} from './sources/evoke';
class CallApp {
/**
*Creates an instance of CallApp.
* @param {object=} options - 配置项
* @memberof CallApp
*/
constructor(options) {
const defaultOptions = { timeout: 2000 };
this.options = Object.assign(defaultOptions, options);
}
/**
* 注册为方法
* generateScheme | generateIntent | generateUniversalLink | generateYingYongBao | checkOpen
*/
generateScheme(config) {
return generate.generateScheme(config, this.options);
}
generateIntent(config) {
return generate.generateIntent(config, this.options);
}
generateUniversalLink(config) {
return generate.generateUniversalLink(config, this.options);
}
generateYingYongBao(config) {
return generate.generateYingYongBao(config, this.options);
}
checkOpen(cb) {
return checkOpen(cb, this.options.timeout);
}
/**
* 唤端失败跳转 app store
* @memberof CallApp
*/
fallToAppStore() {
this.checkOpen(() => {
evokeByLocation(this.options.appstore);
});
}
/**
* 唤端失败跳转通用(下载)页
* @memberof CallApp
*/
fallToFbUrl() {
this.checkOpen(() => {
evokeByLocation(this.options.fallback);
});
}
/**
* 唤端失败调用自定义回调函数
* @memberof CallApp
*/
fallToCustomCb(callback) {
this.checkOpen(() => {
callback();
});
}
/**
* 唤起客户端
* 根据不同 browser 执行不同唤端策略
* @param {object} config - 唤端参数项
* @memberof CallApp
*/
open(config) {
const browser = getBrowser();
const {
universal,
appstore,
logFunc,
intent,
} = this.options;
const { callback } = config;
const supportUniversal = typeof universal !== 'undefined';
const schemeURL = this.generateScheme(config);
let checkOpenFall = null;
if (typeof logFunc !== 'undefined') {
logFunc();
}
if (browser.isIos) {
// 近期ios版本qq禁止了scheme和universalLink唤起app,安卓不受影响 - 18年12月23日
// ios qq浏览器禁止了scheme和universalLink - 2019年5月1日
// ios 微信自 7.0.5 版本放开了 Universal Link 的限制
if ((browser.isWechat && getWeChatVersion() < '7.0.5') || browser.isQQ || browser.isQQBrowser) {
evokeByLocation(appstore);
} else if ((getIOSVersion() < 9)) {
evokeByIFrame(schemeURL);
checkOpenFall = this.fallToAppStore;
} else if (!supportUniversal) {
evokeByLocation(schemeURL);
checkOpenFall = this.fallToAppStore;
} else {
evokeByLocation(this.generateUniversalLink(config));
}
// Android
} else if (browser.isWechat) {
evokeByLocation(this.generateYingYongBao(config));
} else if (browser.isOriginalChrome) {
if (typeof intent !== 'undefined') {
evokeByLocation(this.generateIntent(config));
} else {
// scheme 在 andriod chrome 25+ 版本上必须手势触发
evokeByTagA(schemeURL);
checkOpenFall = this.fallToFbUrl;
}
} else {
evokeByIFrame(schemeURL);
checkOpenFall = this.fallToFbUrl;
}
if (typeof callback !== 'undefined') {
this.fallToCustomCb(callback);
return;
}
if (!checkOpenFall) return;
checkOpenFall.call(this);
}
}
export default CallApp;