shareTools.ts 2.99 KB
import { utils } from "../Base/utils";
import { WxHelper } from "../Base/WxHelper";
import WXSDK from "../wxsdk/WXSDK";
import { OnlineKeys, SDKTools } from "./SDKTools";

/*
* 分享与视频工具类;
*/
export class ShareTools {
    private static isFail: boolean = false;
    /**
     * 是否进入分享  onShow有插屏的话用这个判断
     */
    public static onShowAd: boolean = false;

    public static isTest: boolean = false;
    /**
     * 验证分享:可处理成功、失败
     * @param shareKey 
     * @param params params.fail  有就不处理,没有自动处理
     * @param opts 目前支持4个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享
     */
    static share(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) {
        if (typeof wx === 'undefined' || this.isTest) {
            params && params.success && params.success();
            return
        }
        this.onShowAd = true;
        WXSDK.share.share(shareKey.toString(), params, opts).then(async res => {
            this.onShowAd = false;
            params && params.success && params.success(res);
        }).catch(async err => {
            console.log("err", err)
            if (err && err.code && err.code === 1006) {
                if (Math.random() > SDKTools.getParamsInt(OnlineKeys.shareRandom, 1) && !this.isFail) {
                    this.isFail = true;
                    let modalRes = await WxHelper.showModal({
                        title: '分享失败',
                        content: `${Math.random() > 0.5 ? '分享失败,请重新分享!' : '请分享其它群试试!'}`,
                        showCancel: true,
                        confirmText: '去分享'
                    })
                    if (modalRes) {
                        ShareTools.share(shareKey, params, opts);
                        return
                    }
                } else {
                    this.isFail = false;
                }
            }
            this.onShowAd = false;
            if (!params || !params.fail) {
                utils.tips(err.msg)
            } else {
                params && params.fail && params.fail(err);
            }
        })//this.buildParams(params)
    }
    /**
     * 纯净分享   不处理回调
     * @param shareKey 
     */
    static pureShare(shareKey: string, opts = {}) {
        this.share(shareKey, {}, { closeSimulate: true, ...opts });
    }

    /**
     * 设置右上角分享key
     * @param key 
     */
    static setForwardKey(key) {
        WXSDK.share.setForwardKey(key)
    }

    /**
     * 获取分享还是视频
     * @param key 
     */
    static getShareType(key) {
        try {
            return WXSDK.share.getType(key)
        } catch (err) {
            console.log("1")
            return 2
        }
    }

}



export enum ShareKey {

}