TTAdSdk.ts 4.64 KB
import AppSdk from "./AppSdk";
import TeaSdk, { DotKey } from "./TeaSdk";
import ObjectInstance from "../uitl/ObjectInstance";

export default class TTAdSdk {
    /**
     * 
     * @param preload  0 直接加载;1 预加载
     */
    async showRewardVideoAd(preload: number = 0, point: string = DotKey.proloading) {

        let data = {
            type: 1,
            preloading: preload,
        }
        return new Promise((resolve, reject) => {
            let isComplete = false;
            AppSdk.I.showRewardVideoAd(data, ret => {
                console.log("视频回调", JSON.stringify(ret))
                let { code, msg } = JSON.parse(ret);
                switch (code) {
                    case -1:
                        reject('error');
                       /**'暂无视频广告!每日0点重置'*/;
                        break;
                    case 0:
                        break;
                    case 100:
                        break;
                    case 102:
                        isComplete = true;
                        break;
                    case 101:
                        console.log("视频点了关闭")
                        if (isComplete) {
                            console.log("视频观看完闭并点了关闭")
                            // TeaSdk.I.dot(DotKey.reward_ad, { from: point });
                            resolve(1);
                        } else {
                            reject('unComplete');
                        }
                        break;
                    default:
                        reject('加载失败');
                        // TeaSdk.I.dot(DotKey.reward_ad_fail, { from: point, code: code });
                        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, preload: number = 0) {
        let scaleX = AppSdk.I.width / Laya.stage.width;
        let scaleY = AppSdk.I.height / Laya.stage.height;
        let data = {
            type: 1,
            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 loadNativeAdYLH(x, y, width, height, preload: number = 0) {
        return new Promise((resolve, reject) => {
            let scaleX = AppSdk.I.width / Laya.stage.width;
            let scaleY = AppSdk.I.height / Laya.stage.height;
            let data = {
                type: 1,
                preloading: preload,
                w: width * scaleX,
                h: height * scaleY,
                left: x * scaleX,
                bottom: AppSdk.I.height - height * scaleY - y * scaleY,
            }
            // console.log(JSON.stringify(data))
            // console.log(AppSdk.I.height, height, y, scaleY)
            AppSdk.I.loadNativeAdYLH(JSON.stringify(data), res => {
                resolve(JSON.parse(res))
            })
        })
    }

    openLoadNativeAdEvent() {
        AppSdk.I.openLoadNativeAdEvent()
    }

    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;
    }
}