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