Spring774
2025-10-27 a621979f62588ed1a899d17c937dc3aee8670ec1
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Userdata from "./Userdata";
import Menu from "./Menu";
import Global from "./Global";
import SoundManager from "./SoundManager";
import { Sdk } from "./Sdk/Sdk";
 
const { ccclass, property } = cc._decorator;
 
@ccclass
export class ShopLayer extends cc.Component {
 
    @property(cc.Label)
    gemLabel: cc.Label = null;
 
    @property([cc.Label])
    costLabels: cc.Label[] = [];
 
    shopData: any = [
        { gem: 3600, sku: 'gems3600', price: 5.99 },
        { gem: 8000, sku: 'gems8000', price: 11.99 },
        { gem: 20800, sku: 'gems20800', price: 23.99 },
        { gem: 52000, sku: 'gems52000', price: 47.99 }
    ];
 
    onLoad() {
        Sdk.ins.getProducts((catalog) => {
            if (catalog) {
                for (let i = 0, len = catalog.length; i < len; ++i) {
                    if (catalog[i].productID == this.shopData[0].sku) {
                        this.costLabels[0].string = catalog[i].priceCurrencyCode + ' ' + catalog[i].price;
                    } else if (catalog[i].productID == this.shopData[1].sku) {
                        this.costLabels[1].string = catalog[i].priceCurrencyCode + ' ' + catalog[i].price;
                    } else if (catalog[i].productID == this.shopData[2].sku) {
                        this.costLabels[2].string = catalog[i].priceCurrencyCode + ' ' + catalog[i].price;
                    } else if (catalog[i].productID == this.shopData[3].sku) {
                        this.costLabels[3].string = catalog[i].priceCurrencyCode + ' ' + catalog[i].price;
                    }
                }
            }
        });
    }
 
    onDisable() {
        Menu.instance.shopLayer = null;
    }
 
    onEnable() {
        Menu.instance.shopLayer = this;
        let shopHandler = cc.find("ShopHandler");
        if (!shopHandler) {
            // 加载 Prefab
            cc.loader.loadRes("prefabs/ui/ShopHandler", (err, prefab) => {
                let _node = cc.instantiate(prefab);
                _node.name = "ShopHandler";
                cc.game.addPersistRootNode(_node);
            });
        }
 
        this.updateGemInfo();
 
        this.checkOrders();
    }
 
    checkOrders() {
        Sdk.ins.isPaymentReady((ready) => {
            if (ready) {
                Sdk.ins.getPurchases((result, purchases) => {
                    if (result) {
                        if (purchases) {
                            for (let i = 0, len = purchases.length; i < len; ++i) {
                                let sku = purchases[i].productID;
                                let token = purchases[i].purchaseToken;
                                for (let j = 0, jlen = this.shopData.length; j < jlen; ++j) {
                                    if (sku == this.shopData[j].sku) {
                                        Sdk.ins.consumePurchase(token, (consumeResult) => {
                                            if (consumeResult) {
                                                let gem = this.shopData[j].gem;
                                                Userdata.addGem(gem);
                                                Userdata.raisePurchaseTimes();
                                                Userdata.saveUserdata();
 
                                                this.updateGemInfo();
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }
                });
            }
        });
    }
 
    updateGemInfo() {
        this.gemLabel.string = Userdata.Gem.toString();
    }
 
    onClickClose() {
        SoundManager.playButton();
 
        this.node.parent.emit('updateInfo');
        let _node = cc.find('Canvas');
        if (_node) {
            _node.emit('updateInfo');
        }
 
        this.node.removeFromParent();
        this.destroy();
    }
 
    private buyGemByIndex(index: number) {
        cc.log("buy index = " + index);
        let gem: number = this.shopData[index].gem;
        let sku: string = this.shopData[index].sku;
        let price: number = this.shopData[index].price;
        Sdk.ins.isPaymentReady((ready) => {
            if (ready) {//payment ready
                Sdk.ins.purchaseProduct(sku, (res, purchase) => {
                    if (res) {
                        Sdk.ins.consumePurchase(purchase.purchaseToken, (consumeResult) => {
                            if (consumeResult) {
                                Userdata.addGem(gem);
                                Userdata.raisePurchaseTimes();
                                Userdata.saveUserdata();
 
                                this.updateGemInfo();
                                Global.showTip('Purchase Success.');
                            } else {
                                Global.showTip('Purchase Failed.');
                            }
                        });
 
                    } else {
                        Global.showTip('Purchase Failed.');
                    }
                });
            }
        });
    }
 
    onClickBuyGem3600() {
        SoundManager.playButton();
 
        this.buyGemByIndex(0);
    }
 
    onClickBuyGem8000() {
        SoundManager.playButton();
 
        this.buyGemByIndex(1);
    }
 
    onClickBuyGem20800() {
        SoundManager.playButton();
 
        this.buyGemByIndex(2);
    }
 
    onClickBuyGem52000() {
        SoundManager.playButton();
 
        this.buyGemByIndex(3);
    }
 
}