shareTools.ts 6.84 KB
import { UIManager } from "../../framework/core/view/UIManager";
import { AudioManager } from "../../framework/mananger/AudioManager";
import TAMgr from "../../framework/ta/TAMgr";
import WXSDK from "../../framework/wxsdk/WXSDK";
import { EventModels, EventPool } from "../const/EventModels";
import { GameConst } from "../const/GameConst";
import { EActivitySubTaskType, EWindowExitCode, WindowType } from "../const/types";
import { mapController } from "../modules/map/MapController";
import { PayShopManager } from "../modules/server/manager/PayShopManager";
import { shopController } from "../modules/shop/ShopController";
import { Utils } from "../utils/Utils";
import MaskView from "../view/common/MaskView";
import { Analytics } from "./Analytics";
import { Ge } from "./Ge";
import { SDKTools } from "./SDKTools";
import { WxHelper } from "./WxHelper";

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

    public static isTest: boolean = false;

    // private static getAdCount() {
    //     try {
    //         let adCount = SDKTools.getParamsString("ad_count", '10,10');
    //         let arr = adCount.split(',').map(Number);
    //         if (SDKTools.isnew) {
    //             return +arr[0];
    //         } else {
    //             return +arr[1];
    //         }
    //     } catch (error) {
    //         return 10;
    //     }
    // }
    /**
     * 验证分享:可处理成功、失败
     * @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 async share(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) {
        const type = this.getShareType(shareKey);
        // 免广告卡、体验卡
        if ((type == 2 && (!opts || opts.share_type != 1)) || (opts && opts.share_type == 2)) {
            if (PayShopManager.I.isExperienceOrNoAd()) {
                Analytics.I.dot("video_no_ad", { form: shareKey });
                params && params.success && params.success();
                return;
            } else {
                // 非免广告卡、体验卡用户 检查是否买过体验卡
                if (PayShopManager.I.checkShowExperience()) {
                    if (shopController.buyExperienceTips != Utils.getDayTime(0)) {
                        shopController.buyExperienceTips = Utils.getDayTime(0);;
                        UIManager.instance.showWindow2(WindowType.ExperienceGiftWindow);
                        params && params.fail && params.fail();
                        return;
                    }
                }
            }
        }
        // 广告卡
        if (((type == 2 && (!opts || opts.share_type != 1)) || (opts && opts.share_type == 2)) && mapController.getPackageItemNum(GameConst.ITEM_ADCARD_ID) > 0) {
            const ret = await UIManager.instance.waitWindow2(WindowType.AdCardWindow);
            if (ret == EWindowExitCode.Yes) {
                Analytics.I.dot("ad_card", { ad_card: shareKey });
                params && params.success && params.success();
                return;
            }
        }

        if ((typeof wx == 'undefined'&&typeof my == 'undefined') || this.isTest) {
            params && params.success && params.success();
            // 分享
            const evt = EventPool.get(EventModels.AcitvitySubTaskEvent);
            evt.type = EActivitySubTaskType.Share;
            evt.centerEmit();
            return
        }

        // // 看广告n次后处理为分享
        // let count = this.getAdCount();
        // let isClear = false;
        // // console.log("改为分享前:", settingsController.videoCount, count);
        // if (settingsController.videoCount >= count) {
        //     if (type != 1 || (opts && opts.share_type == 2)) {// 视频 or // 强制视频
        //         opts = {
        //             ...opts,
        //             share_type: 1,//强制改为分享
        //         };
        //         isClear = true;
        //         // console.log("改为分享了");
        //     }
        // }

        this.onShowAd = true;
        AudioManager.I.pauseAll();
        if (!opts || !opts.closeSimulate) {
            MaskView.Show()
        }
        WXSDK.share.share(shareKey.toString(), params, opts).then(async res => {
            this.onShowAd = false;
            // if (isClear) {
            //     settingsController.videoCount = 0;
            // }
            if (res.type == 2) {
                // 视频
                // settingsController.videoCount++;
                Ge.I.adShowEvent("video", shareKey.toString());
                TAMgr.Ins.userAdd({ total_ad_num: 1 });
            } else {
                // 分享
                const evt = EventPool.get(EventModels.AcitvitySubTaskEvent);
                evt.type = EActivitySubTaskType.Share;
                evt.centerEmit();
                TAMgr.Ins.dot('share_source', { from: shareKey });
            }
            params && params.success && params.success(res);
            AudioManager.I.resumeAll();
            MaskView.Hide();
        }).catch(async err => {
            AudioManager.I.resumeAll();
            MaskView.Hide();
            this.onShowAd = false;
            if (!params || !params.fail) {
                if (err && err.msg) {
                    WxHelper.showToast(err.msg)
                }
            } else {
                params && params.fail && params.fail(err);
            }
        })
    }

    /**
     * 必定视频 不走后台配置
     * @param shareKey 
     * @param params 
     */
    static video(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) {
        this.share(shareKey, params, { ...opts, share_type: 2 });
    }
    /**
     * 纯净分享   不处理回调
     * @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
        }
    }

    /**
     * 分享到朋友圈  bate 只支持安卓
     */
    static onShareTimeline(title: string, imageUrl: string, imagePreviewUrl?: string, query?: string) {
        WXSDK.share.onShareTimeline(title, imageUrl, imagePreviewUrl, query);
    }

}