AdService.ts 2.2 KB
import WxApi from "../wx/WxApi";
import WxBanner from "../wx/WxBanner";
import WxCustom from "../wx/WxCustom";
import WxInterstitial from "../wx/WxInterstitial";
import { GAMEDATA } from "../base/SDKConst";
import LogService from "./LogService";
import DouyinInterstitial from "../douyin/DouyinInterstitial";



export default class AdService {
    private constructor() {
    }
    /**
     * 创建banner
     * @param adUnitId 
     * @param opts 
     */
    createBanner(adUnitId: string, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) {
        return WxBanner.I.create(adUnitId, opts);
    }
    /**
     * banner 显示 ps:创建默认显示
     */
    showBanner() {
        WxBanner.I.show()
    }
    /**
     * banner 隐藏
     */
    hideBanner() {
        WxBanner.I.hide();
    }
    /**
     * banner 销毁
     */
    destoryBanner() {
        WxBanner.I.destory();
    }


    /**
     * 插屏
     * @param adUnitId 
     */
    createInterstitialAd(adUnitId: string) {
        if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
            return DouyinInterstitial.showInterstitialAd(adUnitId);
        } else {
            return WxInterstitial.showInterstitialAd(adUnitId)
        }
    }

    /**
     * 创建原生广告
     * @param adUnitId 
     * @param opts 
     */
    createCustom(adUnitId: string, opts: { top: number; left: number; adIntervals?: number }) {
        return WxCustom.I.create(adUnitId, opts);
    }
    /**
    * 原生广告 显示 ps:创建默认显示
    */
    showCustom() {
        WxCustom.I.show()
    }
    /**
     * 格子广告 隐藏
     */
    hideCustom() {
        WxCustom.I.hide();
    }
    /**
     * 格子广告 销毁
     */
    destoryCustom() {
        WxCustom.I.destory();
    }



    // 导出
    navigateToMiniProgram(data, type, opts?) {
        let { id, appid, path, game } = data;
        LogService.I.jumps(id, type);
        if (!path) path = `?channel_id=${GAMEDATA.channel_id}`;
        return WxApi.I.navigateToMiniProgram(appid, path, opts)
    }







    private static instance: AdService;
    static get I(): AdService {
        return this.instance || (this.instance = new AdService());
    }
}