SDKTools.ts
2 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
import ShareVideoTools, { ShareVideoKeys, ShareVideoType } from "./ShareVideoTools";
/*
* SDK工具类库;
*/
export default class SDKTools {
// 判断PCSDK是否支持
static get isSupported() {
return typeof PCSDK === 'object';
}
/**
* 检测普通不是返回值为Promise的 api接口:在浏览器中或其他不支持的平台不至于报错
* @param callback 接口 api函数
* @param defaltVal 返回的默认值
*/
private static checkNormal<T>(callback: Function, defaltVal?: T): T {
if (!this.isSupported) return defaltVal;
return callback();
}
/**
* 检测普返回值为Promise的 api接口:在浏览器中或其他不支持的平台不至于报错
* @param callback 接口 api函数
* @param defaltVal 返回的默认值
*/
private static checkPromise(callback: Function, defaultVal: Promise<any> = Promise.resolve()): Promise<any> {
return this.checkNormal<Promise<any>>(callback, defaultVal);
}
static getType(shareVideoKey: ShareVideoKeys): ShareVideoType {
return this.checkNormal(() => ShareVideoTools.getType(shareVideoKey), ShareVideoType.Share);
}
static share(shareVideoKey: ShareVideoKeys, params?: any, opts?: any): void {
return this.checkNormal(() => ShareVideoTools.share(shareVideoKey, params, opts), null);
}
static dispatch(shareVideoKey: ShareVideoKeys, params?: { type?: number, success?: Function, fail?: Function, context?: any }): void {
if (!this.isSupported)
return params && params.success && params.success.call(params.context || this);
else
return this.checkNormal(() => ShareVideoTools.dispatch(shareVideoKey, params), null);
}
static dispatchType(shareVideoType: ShareVideoType, shareVideoKey: ShareVideoKeys, opts?: { success?: Function, fail?: Function, context?: any }): void {
return this.checkNormal(() => ShareVideoTools.dispatchType(shareVideoType, shareVideoKey, opts), null);
}
}