Spring774
2025-10-28 c612f86f2f11b6fe9506f5c4e6fb491c975dbffc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { SdkDefault } from "./SdkDefault";
 
export class SdkAndroid extends SdkDefault {
    private className = "com/utils/Sdk";
    private lastShowInterAdTime: number = 0;
    private _rewardVideoCallback: (result: boolean) => void = null;
    private _purchaseCallback: (result: boolean, purchases?: any) => void = null;
    private _purchasesCallback: (result: boolean, purchases?: any[]) => void = null;
 
    private skus = [
        'coins1200',
        'coins2700',
        'coins4400',
        'coins6800'
    ];
 
    /**
     * 开始游戏
     * @param cb 
     */
    startGame(cb?: Function): void {
        cc.log('SdkDefault startGame.');
        cb && cb();
        setTimeout(() => {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "removeLaunchImage", "()V");
        }, 0.3);//延迟一会删除splash
    }
 
    share(imageBase64: any, text: string) {
        jsb.reflection.callStaticMethod(this.className, "share", "(Ljava/lang/String;Ljava/lang/String;)V", imageBase64, text);
    }
 
    logEvent(eventName: string, params?: { [key: string]: string }) {
        let extraStr = params ? JSON.stringify(params) : JSON.stringify({});
        jsb.reflection.callStaticMethod(this.className, "logEvent", "(Ljava/lang/String;ILjava/lang/String;)V", eventName, extraStr);
    }
 
    /////////////////////////////////////////////////////Purchase
    getProducts(cb?: (catalog: Array<any>) => void) {
        cb && cb(null);
    }
 
    getPurchases(cb?: (result: boolean, purchases?: any[]) => void) {
        this._purchasesCallback = cb;
        this._purchaseCallback = null;
        jsb.reflection.callStaticMethod(this.className, "queryInventoryByJS", "(Ljava/lang/String;Ljava/lang/String;)V", JSON.stringify(this.skus), JSON.stringify([]));
    }
 
    getPriceCallback = function(json){
        if (!this.priceObj){
            this.priceObj = {};
        }
    
        cc.log('price json = ', json);
        let obj = json;//JSON.parse(json);
    
        if (obj){
            let _keys = Object.keys(obj);
            for(let i = 0, len = _keys.length; i < len; ++i){
                this.priceObj[_keys[i]] = obj[_keys[i]];//记录本地价格
            }
    
            let node = cc.find('Canvas');
            if (node){
                node.emit('updateSkuPrice', this.priceObj);
            }
        }
    };
 
    purchaseProduct(sku: string, cb?: (result: boolean, purchase?: any) => void) {
        this._purchaseCallback = cb;
        this._purchasesCallback = null;
        jsb.reflection.callStaticMethod(this.className, "purchase", "(Ljava/lang/String;)V", sku);
        // cb && cb(true, { purchaseToken: sku })
    }
 
    //从js返回的数据
    purchaseCallback(isSuccess: boolean, sku: string) {
        if (this._purchaseCallback) {
            setTimeout(() => {
                this._purchaseCallback(isSuccess, { purchaseToken: sku });
            }, 100);
        }
 
        if (this._purchasesCallback) {
            setTimeout(() => {
                this._purchasesCallback(isSuccess, [{ productID: sku, purchaseToken: sku }]);
            }, 100);
        }
    }
 
    consumePurchase(token: string, cb?: (result: boolean) => void) {
        cb && cb(true);//不需要消耗,在java端已处理,直接返回
    }
 
    isPaymentReady(cb?: (result: boolean) => void) {
        cb && cb(true);
    }
 
 
    //Ads
    showInterAd() {
        cc.log('show InterAd');
        if (Date.now() - this.lastShowInterAdTime > 30000) {//最少间隔30秒
            jsb.reflection.callStaticMethod(this.className, "showInterAd", "()V");
            this.lastShowInterAdTime = Date.now();
        }
    }
 
    showVideoAd(cb?: (result: boolean) => void) {
        this._rewardVideoCallback = cb;
        cc.log('show VideoAd');
        jsb.reflection.callStaticMethod(this.className, "showRewardVideo", "()V");
        // cb && cb(true);
    }
 
    setRewardVideoShowCallback(result: boolean) {
        if (this._rewardVideoCallback) {
            setTimeout(() => {
                this._rewardVideoCallback(result);
            }, 100);
        }
    }
 
    vibrate(milliSeconds: number) {
        jsb.reflection.callStaticMethod(this.className, "vibrate", "(I)V", milliSeconds);
    }
 
    isVibrateSupport(): boolean {
        return jsb.reflection.callStaticMethod(this.className, "isVibrateSupport", "()Z");
    }
 
    rate() {
        jsb.reflection.callStaticMethod(this.className, "rate", "()V");
    }
 
    postScore(best: number) {
        jsb.reflection.callStaticMethod(this.className, "uploadScore", "(I)V", best);
    }
 
    showLeaderboard(score: number){
        jsb.reflection.callStaticMethod(this.className, "showLeaderboard", "(I)V", score);
    }
}