forked from suanmei/callapp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (162 loc) · 4.62 KB
/
Copy pathindex.js
File metadata and controls
185 lines (162 loc) · 4.62 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
/**
* @author suanmei <[email protected]>
*/
import { getIOSVersion, getBrowser } from './sources/browser';
import {
evokeByLocation,
evokeByIFrame,
evokeByTagA,
checkOpen,
} from './sources/evoke';
class CallApp {
/**
*Creates an instance of CallApp.
* @param {object=} options - 配置项
* @memberof CallApp
*/
constructor(options) {
this.options = options || {};
}
/**
* 搭建基本的 url scheme
* @param {object} config - 参数项
* @returns {string} url scheme
* @memberof CallApp
*/
buildScheme(config) {
const { path, param } = config;
const query = typeof param !== 'undefined'
? Object.keys(param).map(key => `${key}=${param[key]}`).join('&')
: '';
return `${this.options.protocol}://${path}?${query}`;
}
/**
* 生成业务需要的 url scheme(区分是否是外链)
* @param {object} config - 参数项
* @returns {string} url scheme
* @memberof CallApp
*/
generateScheme(config) {
const { outChain } = this.options;
let uri = this.buildScheme(config);
if (typeof outChain !== 'undefined' && outChain) {
const { protocal, path, key } = outChain;
uri = `${protocal}://${path}?${key}=${encodeURIComponent(uri)}`;
}
return uri;
}
/**
* 生成 android intent
* @param {object} config - 唤端参数项
* @returns {string} intent
* @memberof CallApp
*/
generateIntent(config) {
const { outChain } = config;
const { intent, fallback } = this.options;
const intentParam = Object.keys(intent).map(key => `${key}=${intent[key]};`).join('');
let urlPath = this.buildScheme(config);
if (typeof outChain !== 'undefined' && !outChain) {
const { path, key } = config.outChain;
return `intent://${path}?${key}=${encodeURIComponent(urlPath)}/
#Intent;${intentParam};S.browser_fallback_url=${fallback};end;`;
}
urlPath = urlPath.slice(urlPath.indexOf('//') + 2);
return `intent://${urlPath}/#Intent;${intentParam};end;`;
}
/**
* 生成 universalLink
* @param {object} config - 唤端参数项
* @returns {string} universalLink
* @memberof CallApp
*/
generateUniversalLink(config) {
const { host, pathKey } = this.options.universal;
const { path, param } = config;
const query = typeof param !== 'undefined'
? Object.keys(param).map(key => `${key}=${param[key]}`).join('&')
: '';
return `//${host}?${pathKey}=${path}${query ? '&' : ''}${query}`;
}
/**
* 生成 universalLink
* @param {object} config - 唤端参数项
* @returns {string} universalLink
* @memberof CallApp
*/
generateYingYongBao(config) {
const url = this.generateScheme(config);
// 支持 AppLink
return `${this.options.yingyongbao}&android_schema=${encodeURIComponent(url)}`;
}
/**
* 唤端失败跳转 app store
* @memberof CallApp
*/
fallToAppStore() {
checkOpen(() => {
evokeByLocation(this.options.appstore);
});
}
/**
* 唤端失败跳转通用(下载)页
* @memberof CallApp
*/
fallToFbUrl() {
checkOpen(() => {
evokeByLocation(this.options.fallback);
});
}
/**
* 唤端失败调用自定义回调函数
* @memberof CallApp
*/
static fallToCustomCb(callback) {
checkOpen(() => {
callback();
});
}
/**
* 唤起客户端
* 根据不同 browser 执行不同唤端策略
* @param {object} config - 唤端参数项
* @memberof CallApp
*/
open(config) {
const browser = getBrowser();
const { universal, appstore, logFunc } = this.options;
const { callback } = config;
const supportUniversal = typeof universal !== 'undefined';
let checkOpenFall = null;
if (typeof logFunc !== 'undefined') {
logFunc();
}
if (browser.isIos) {
if (browser.isWechat) {
evokeByLocation(appstore);
} else if ((getIOSVersion() < 9)) {
evokeByIFrame(this.generateScheme(config));
checkOpenFall = this.fallToAppStore;
} else if (!supportUniversal) {
evokeByLocation(this.generateScheme(config));
} else {
evokeByLocation(this.generateUniversalLink(config));
}
// Android
} else if (browser.isWechat) {
evokeByLocation(this.generateYingYongBao(config));
} else if (browser.isOriginalChrome) {
evokeByTagA(this.generateIntent(config));
} else {
evokeByIFrame(this.generateScheme(config));
checkOpenFall = this.fallToFbUrl;
}
if (typeof callback !== 'undefined') {
CallApp.fallToCustomCb(callback);
return;
}
if (!checkOpenFall) return;
checkOpenFall.call(this);
}
}
export default CallApp;