cb/assets/shop/script/shop.ts
2025-07-02 18:21:32 +08:00

135 lines
4.7 KiB
TypeScript

import Utils from "../../Script/module/Pay/Utils";
import List from "../../Script/module/RankList/List";
import NumberToImage from "../../Script/NumberToImage";
import { MiniGameSdk } from "../../Script/Sdk/MiniGameSdk";
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
//商店界面
@property(cc.Node)
shop: cc.Node = null;
//商品列表
@property(cc.Node)
itemList: cc.Node = null;
// onLoad () {}
//金币数量
@property(cc.Node)
coin: cc.Node = null;
btn_Touch: boolean = true;
onLoad() {
this.btn_Touch = true;
}
start() {
this.btn_Touch = true;
this.openShop();
}
//打开商店界面
openShop() {
// 商品数据数组
const products = [
{ product_id: "gold_pack_1", name: "金币包1", price: 600, coin:1200, title: "3x2六档金币" },
{ product_id: "gold_pack_2", name: "金币包2", price: 3600, coin:8000, title: "" },
{ product_id: "gold_pack_3", name: "金币包3", price: 6800, coin:16000, title: "" },
{ product_id: "gold_pack_4", name: "金币包4", price: 12800, coin:32000, title: "" },
{ product_id: "gold_pack_5", name: "金币包5", price: 32800, coin:100000, title: "" },
{ product_id: "gold_pack_6", name: "金币包6", price: 64800, coin:240000, title: "" },
];
for (let i = 1; i <= 6 && i < this.itemList.children.length; i++) {
const spriteComp = this.itemList.children[i].children[0].getComponent(cc.Sprite);
const price = this.itemList.children[i].children[1];
const title = this.itemList.children[i].children[2];
const product = products[i - 1];
if (spriteComp && product) {
// TODO: 根据 product_id 或 name 设置 spriteComp.spriteFrame
}
if (price && product) {
NumberToImage.numberToImageNodes(product.price / 100, 25, 20, "cost_", price, false)
}
if (title && product) {
NumberToImage.numberToImageNodes(product.coin, 40, 25, "scoin_", title, true)
}
}
NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
}
//关闭商店界面
closeShop() {
//销毁预制体
this.shop.destroy();
}
//点击充值购买
buyProduct(event, customData) {
if(!this.btn_Touch){
return;
}
this.btn_Touch = false;
const productId = customData;
let id = "10011";
let price = 100;
let count = 1;
id = productId;
switch(productId){
case "gold_pack_1":
price = 100;
break;
case "gold_pack_2":
price = 100;
break;
case "gold_pack_3":
price = 100;
break;
case "gold_pack_4":
price = 100;
break;
case "gold_pack_5":
price = 100;
break;
case "gold_pack_6":
price = 200;
break;
}
console.log("获得商品id:",id,count,price);
Utils.buyProp(id,count,price,(res)=>{
console.log("获得充值结果",res);
if(res == null){
MiniGameSdk.API.showToast("网络异常,充值失败请稍后再试");
this.btn_Touch = true;
}
else{
Utils.getPayInfo((data) =>{
console.log("获得轮训结果:",data);
if(data.data.pay_state == 1){
this.btn_Touch = true;
MiniGameSdk.API.showToast("取消充值");
cc.fx.GameTool.shopBuy(productId);
console.log("充值取消未获得金币");
NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
this.node.parent.getComponent("JiaZai").updateCoin();
}
else if(data.data.pay_state == 2){
this.btn_Touch = true;
MiniGameSdk.API.showToast("充值成功");
cc.fx.GameTool.shopBuy(productId);
console.log("充值成功获得金币");
NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true);
this.node.parent.getComponent("JiaZai").updateCoin();
}
})
}
});
}
// update (dt) {}
}