TTAdSdk.ts 3.01 KB
import AppSdk from "./AppSdk";
import ObjectInstance from "../uitl/ObjectInstance";

export default class TTAdSdk {
    /**
     * 
     * @param preload  0 直接加载;1 预加载
     */
    async showRewardVideoAd(preload: number = 0) {
        let data = {
            type: 1,
            preloading: preload,
        }
        return new Promise((resolve, reject) => {
            let isComplete = false;
            AppSdk.I.showRewardVideoAd(data, ret => {
                let { code, message } = JSON.parse(ret);
                switch (code) {
                    case -1:
                        reject('error');
                       /**'暂无视频广告!每日0点重置'*/;
                        break;
                    case 102:
                        isComplete = true;
                        break;
                    case 101:
                        if (isComplete) {
                            resolve();
                        } else {
                            reject('unComplete');
                        }
                        break;
                }
            })
        })
    }

    async loadFullScreenVideoAd(preload: number = 0) {
        let data = {
            w: AppSdk.I.width,
            h: AppSdk.I.width * 90 / 600,
            preloading: preload,
            type: 1
        }
        return new Promise((resolve, reject) => {
            AppSdk.I.loadFullScreenVideoAd(JSON.stringify(data),res=>{
                resolve(res)
            })
        })
    }

    async loadNativeExpressAd(x, y, width, height, type = 1, preload: number = 0) {
        let scaleX = AppSdk.I.width / Laya.stage.width;
        let scaleY = AppSdk.I.height / Laya.stage.height;
        let data = {
            type: type,
            preloading: preload,
            w: width * scaleX,
            h: height * scaleY,
            left: x * scaleX,
            top: y * scaleY
        }
        AppSdk.I.loadNativeExpressAd(JSON.stringify(data),res=>{
          return Promise.resolve(res)
        })
    }

    async loadBannerExpressAd(preload: number = 0) {
        let data = {
            w: AppSdk.I.width,
            h: AppSdk.I.width * 90 / 600,
            preloading: preload,
            type: 1
        }
        return new Promise((resolve, reject) => {
            AppSdk.I.loadBannerExpressAd(JSON.stringify(data),res=>{
                resolve(res)
            })
        })
    }

    closeAdverDialog() {
        AppSdk.I.closeAdverDialog();
    }
    closeAdBanner() {
        AppSdk.I.closeAdBanner();
    }

    async showInteractionExpressAd(preload: number = 0) {
        let data = {
            w: AppSdk.I.width * 0.8,
            h: AppSdk.I.height,
            preloading: preload,
        }
        return new Promise((resolve, reject) => {
            let isComplete = false;
            AppSdk.I.showInteractionExpressAd(JSON.stringify(data),res=>{
                resolve(res)
            })
        })
    }

    static get I(): TTAdSdk {
        return ObjectInstance.get(TTAdSdk) as TTAdSdk;
    }
}