SDKTools.ts 2 KB
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);
    }
}