AppSdk.ts 6.1 KB
import { EventEnum } from "../event/EventEnum";
import { EventCenter } from "../event/EventCenter";
import ObjectInstance from "../uitl/ObjectInstance";

export default class AppSdk {
    private _width: number;
    private _height: number;
    private _channel: string;
    private _version: string;
    private _level: number;
    private _gameConfig: number;

    public pangolinId: string;
    public adnetId: string;

    init() {
        this.getSystemInfo();
    }

    private get myBridge() {
        if (MyBridge) {
            return MyBridge;
        }
    }



    /**获取基本版本信息以及手机的配置 */
    private getSystemInfo(key: number = 0) {
        let ret = this.myBridge.getSystemInfo(key);
        let { w, h, versionName, channel, pangolin, adnet, level } = JSON.parse(ret);
        [this._width, this._height, this._version,
        this._channel, this.pangolinId,
        this.adnetId, this._level] = [w, h, versionName, channel, pangolin, adnet, level];
    }

    initGameConfig() {
        this._gameConfig = this.getGameConfig();
    }

    /**
     * 震动
     * @param key 0 短震动 1 长震动
     */
    vibrate(key: number = 1) {
        this.myBridge.openSystemFun(key);
    }

    /**隐藏启动屏 */
    hideSplash() {
        this.myBridge.hideSplash();
    }

    /**弹出提现 */
    withDrawal() {
        this.myBridge.withDrawal();
    }
    /**意见反馈 */
    feedback() {
        this.myBridge.feedback();
    }
    /**
     * key 默认0 ,签到 1
     */
    personal() {
        this.myBridge.personal();
    }
    /**邀请好友*/
    invitation() {
        this.myBridge.invitation();
    }


    /**邀请好友*/
    openReward() {
        this.myBridge.openReward();
    }
    /**签到*/
    openSignin() {
        this.myBridge.openSingin();
    }


    putData(key: String, value: String) {

        this.myBridge.putData(key, value);
    }
    /**
     * @param key  存数据的时候的唯一key
     * @return  存储的数据
     * */
    getData(key: String) {
        let ret = this.myBridge.getData(key);
        return JSON.parse(ret);
    }

    /**
     * 获取金币配表
     * @param key 1红包   2 摇一摇红包
     */
    getGameConfig() {
        let result = this.myBridge.getGameConfig();
        return JSON.parse(result);
    }
    /**
     * 要求发放金币
     * @param key 1红包
     */
    async addGold(key=1) {
        return new Promise<any>(async (resolve, reject) => {
            await this.myBridge.addGold(key, res => {
                res = JSON.parse(res)
                const { code, data, msg } = res;
                if ('0' === code || !code) {
                    resolve(data)
                } else {
                    reject(msg);
                }
            });
        })
    }
    /**获取总金币 */
    async myCoin() {
        return new Promise<any>(async (resolve, reject) => {
            await this.myBridge.myCoin(res => {
                res = JSON.parse(res)
                const { code, data, msg } = res;
                if ('0' === code || !code) {
                    resolve(data['coin'])
                } else {
                    reject(msg);
                }
            });
        })
    }


    /**获取当前提现的档位 */
    async minWithDrawal() {
        return new Promise<any>(async (resolve, reject) => {
            await this.myBridge.minWithDrawal(res => {
                res = JSON.parse(res)
                const { code, data, msg } = res;
                if ('0' === code || !code) {
                    resolve(data['money'])
                } else {
                    reject(msg);
                }
            });
        })
    }

    /**上报埋点 */
    appDotLog(key: string, value?) {
        this.myBridge.appDotLog(key, value);
    }
    /**热云打点 */
    appDotTracKing(key: string) {
        this.myBridge.appDotTracKing(key);
    }

    setCallback(funName: string, pramas?: any) {
        console.log('---------------setCallback---- this.myBridge---' + JSON.stringify(this.myBridge))
        return new Promise((resolve, reject) => {
            this.myBridge[funName](pramas, res => {
                let { code, data } = JSON.parse(res);
                if ('0' === code) {
                    resolve(data);
                } else {
                    reject(0);
                }
            })
        })
    }

    closeAdverDialog() {
        this.myBridge.closeAdverDialog();
    }
    closeAdBanner() {
        this.myBridge.closeAdBanner();
    }
    /**激励视频 */
    showRewardVideoAd(value: IVideo, JBCallback: Function) {
        this.myBridge.showRewardVideoAd(value, JBCallback)
    }
    /** 全屏广告*/
    loadFullScreenVideoAd(data: any, JBCallback: Function) {
        this.myBridge.showRewardVideoAd(data, JBCallback)
    }
    /**信息流广告 */
    loadNativeExpressAd(data: any, JBCallback: Function) {
        this.myBridge.loadNativeExpressAd(data, JBCallback)
    }
    /**banner */
    loadBannerExpressAd(data: any, JBCallback: Function) {
        this.myBridge.loadBannerExpressAd(data, JBCallback)
    }
    /**插屏广告 */
    showInteractionExpressAd(data: any, JBCallback: Function) {
        this.myBridge.showInteractionExpressAd(data, JBCallback)
    }

    get level() {
        return this._level;
    }
    get width() {
        return this._width;
    }

    get height() {
        return this._height;
    }

    get version() {
        return this._version;
    }

    get channel() {
        return this._channel;
    }

    protected get bridge() {
        return null;
    }

    get gameConfig() {
        return this._gameConfig;
    }
    static get I(): AppSdk {
        return ObjectInstance.get(AppSdk);
    }
}
//     //这里是浏览器环境下, 接收web传过来的消息
window.onMyBridgeReady = () => {
    console.log("onMyBridgeReady load finish, cost:" + (new Date().getTime()) + "ms");
    MyBridge.App.onResume = (...ret) => {
        console.log('     laya onResume', ret)
       
    }
    MyBridge.App.onPause = (...ret) => {
        console.log('    laya onPause', ret)
    }
}

interface IVideo {
    preloading: number,
    type: number,
}