BaseAppSdk.ts 4.76 KB
import AddGoldData from "./sdkData/AddGoldData";
import MyCoinData from "./sdkData/MyCoinData";
import { GoldConfigData } from "./sdkData/GoldConfigData";

/** 返回参数类型 */
interface IAppResult<T> {
    /** 错误码 */
    code?: number;
    /** 返回数据 */
    data?: T;
}


export default class BaseAppSdk {
    protected _bridge: any;
    private netOnArr: Array<Function> = [];
    private netOffArr: Array<Function> = [];
    private _width: number;
    private _height: number;
    private _channel: string;
    private _version: string;
    private _level: number;

    public pangolinId: string;
    public adnetId: string;

    init() {
        let conch = window['conch'];
        if (conch) {
            conch.setNetworkEvtFunction(this.networkEvtFunc.bind(this));
        }
        this.callApp('setAppCallback', 1, 'AppSdk.onShow()');
        this.callApp('setAppCallback', 2, 'AppSdk.onHide()');
        this.getSystemInfo();

    }

    private getSystemInfo() {
        this.callAppWithBack(ret => {
            console.log('---------------getSystemInfo-------' + JSON.stringify(ret))
            let { w, h, versionName, channel, pangolin, adnet, level } = JSON.parse(ret);
            this._width = w;
            this._height = h;
            this._version = versionName;
            this._channel = channel;
            this.pangolinId = pangolin;
            this.adnetId = adnet;
            this._level = level;
        }, 'getSystemInfo', 0);
    }

    private onShow() {
        // EventCenter.emit(EventEnum.APP_SHOW);
    }

    private onHide() {
        // EventCenter.emit(EventEnum.APP_HIDE);

    }

    vibrate() {
        this.callApp('openSystemFun', 0);
    }

    hideSplash() {
        this.callApp('hideSplash');
    }

    /**弹出提现 */
    withDrawal() {
        this.callApp('withDrawal');
    }
    /**意见反馈 */
    feedback() {
        this.callApp('feedback');
    }

    /**
     * key 默认0 ,签到 1
     */
    personal(key = 0) {
        this.callApp('personal', key);
    }
    /**邀请好友*/
    invitation() {
        this.callApp('invitation');
    }


    currentShut(value: number) {
        this.callApp('currentShut', value);
    }
    /**
     * 获取金币配表
     * @param key 1红包   2 摇一摇红包
     */

    async  getGameConfig(key: number = 1) {
        let result = await this.onCallBack('getGameConfig', key)
        GoldConfigData.I.initData(result);
        return result;
    }

    /**
     * 要求发放金币
     * @param key 1红包
     */

    async addGold(key: number = 1) {
        let result = await this.onCallBack('addGold', key);
        AddGoldData.initData(result);
        return result;
    }
    async myCoin() {
        let result = await this.onCallBack('myCoin')
        MyCoinData.initData(result);
        return result;

    }
    /**获取当前提现的档位 */
    async minWithDrawal() {
        let result = await this.onCallBack('minWithDrawal');
        if (!Boolean(parseInt(result.code + ''))) {
            return parseInt(result.data['money']) * 100;
        }
        return 0;
    }




    onCallBack(cmd: string, ...params) {
        return new Promise<IAppResult<any[]>>(async (resolve, reject) => {
            await this.callAppWithBack(res => {
                res = JSON.parse(res)
                // res = { code: 1000 }
                if (!res.code) {
                    resolve({ code: res.code, data: res.data });
                } else {
                    resolve({ code: res.code })
                }
            }, cmd, ...params);
        })



    }

    callApp(cmd: string, ...params) {
    }

    callAppWithBack(callBack: Function, cmd: string, ...params) {
    }

    private networkEvtFunc(type: number) {
        let funcArr;
        if (type == 0) {
            funcArr = this.netOffArr;
        } else if (type == 5) {
            funcArr = this.netOnArr;
        }
        if (funcArr) {
            for (let i = 0; i < funcArr.length; i++) {
                funcArr[i]();
            }
        }
    }

    onNetOn(func: Function) {
        this.netOnArr.push(func);
    }



    onNetOff(func: Function) {
        this.netOffArr.push(func);
    }

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

    protected createClass(name: string) {
        return window['PlatformClass'].createClass(name);
    }
}