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; //体力信息 @property(cc.Node) Stamina: cc.Node = null; btn_Touch: boolean = true; private onShowListener: () => void; private scheduleCallback: Function = null; private currentCoin: number = 0; private coinAnimTime: number = 0; private coinAnimDuration: number = 1.5; // 1.5秒 private coinStart: number = 0; private coinEnd: number = 0; private coinAnimating: boolean = false; //飞金币动画 @property(cc.Node) coinAnim: cc.Node = null; private buy: boolean = false; onLoad() { this.btn_Touch = true; // 检测微信小游戏切到后台 if (cc.sys.platform === cc.sys.WECHAT_GAME) { // 定义监听函数 this.onShowListener = () => { this.onShow(); }; //@ts-ignore wx.onShow(this.onShowListener); } } start() { this.btn_Touch = true; this.openShop(); this.setHealthInfo(); } //打开商店界面 openShop() { Utils.outTradeNo = null; // 商品数据数组 const products = [ { product_id: "gold_1", name: "金币包1", price: 600, coin: 1200, title: "3x2六档金币" }, { product_id: "gold_2", name: "金币包2", price: 3600, coin: 8000, title: "" }, { product_id: "gold_3", name: "金币包3", price: 6800, coin: 16000, title: "" }, { product_id: "gold_4", name: "金币包4", price: 12800, coin: 32000, title: "" }, { product_id: "gold_5", name: "金币包5", price: 32800, coin: 100000, title: "" }, { product_id: "gold_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, 35, 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); } startCoinAnim(target: number) { if (this.coinAnimating && this.coinEnd === target) return; this.coinStart = this.currentCoin; this.coinEnd = target; this.coinAnimTime = 0; this.coinAnimating = true; } //优化 protected update(dt: number): void { if (this.coin && this.coinAnimating) { this.coinAnimTime += dt; let t = this.coinAnimTime / this.coinAnimDuration; if (t >= 1) { this.currentCoin = this.coinEnd; this.coinAnimating = false; NumberToImage.numberToImageNodes(this.currentCoin, 30, 15, "coin_", this.coin, true); } else { this.currentCoin = Math.floor(this.coinStart + (this.coinEnd - this.coinStart) * t); NumberToImage.numberToImageNodes(this.currentCoin, 30, 15, "coin_", this.coin, true); } } } playCoinAnim(target?: number) { this.coinAnim.active = true; this.coinAnim.getComponent(sp.Skeleton).setAnimation(0, "feijinbi", false); // 监听动画完成事件 this.coinAnim.getComponent(sp.Skeleton).setCompleteListener(() => { // 动画播放完成后销毁节点 this.coinAnim.active = false; }); this.startCoinAnim(5000); } //关闭商店界面 closeShop() { // 移除 wx.onShow 监听器 if (cc.sys.platform === cc.sys.WECHAT_GAME && this.onShowListener) { //@ts-ignore wx.offShow(this.onShowListener); } Utils.outTradeNo = null; //销毁预制体 if (this.node.parent.getComponent("JiaZai")) { this.node.parent.getComponent("JiaZai").closeShop(); } else if (this.node.parent.getComponent("SceneManager")) { this.node.parent.getComponent("SceneManager").closeShop(); } // this.shop.destroy(); } onShow() { if (cc.fx.GameConfig.GM_INFO.iosOutTradeNo != null) { console.log("有苹果订单号,开始轮训"); Utils.getIosPayInfo((data) => { console.log("获得轮训结果:", data); if (data.code == 1) { cc.fx.GameConfig.GM_INFO.iosOutTradeNo = null; MiniGameSdk.API.showToast("购买金币成功"); } }) } } setHealthInfo() { if (cc.fx.GameConfig.GM_INFO.hp >= 5) { this.Stamina.getChildByName("man").active = true; this.Stamina.getChildByName("health").active = true; NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hp, 25, 15, "hp_", this.Stamina.getChildByName("health"), false); this.Stamina.getChildByName("time").active = false; } else { this.Stamina.getChildByName("man").active = false; this.Stamina.getChildByName("health").active = true; NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.hp, 25, 15, "hp_", this.Stamina.getChildByName("health"), false); this.Stamina.getChildByName("time").active = true; if (cc.fx.GameConfig.GM_INFO.min_Time != 0) { let time = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time); this.Stamina.getChildByName("time").getComponent(cc.Label).string = time; this.startTimeCutDown(); } } } startTimeCutDown() { this.scheduleCallback = function () { if (this.pause) return; if (cc.fx.GameConfig.GM_INFO.min_Time <= 0) { this.stopTimeCutDown(); var timeTemp = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time); // 同步显示 if (this.Stamina && this.Stamina.getChildByName("time")) { this.Stamina.getChildByName("time").getComponent(cc.Label).string = timeTemp; } MiniGameSdk.API.showToast("恢复一点滴点体力"); cc.fx.GameTool.setUserHealth(1, (data) => { cc.fx.GameTool.getHealth(null); this.setHealthInfo(); }) } else { cc.fx.GameConfig.GM_INFO.min_Time -= 1; var timeTemp = cc.fx.GameTool.getTimeMargin(cc.fx.GameConfig.GM_INFO.min_Time); // 同步显示 if (this.Stamina && this.Stamina.getChildByName("time")) { this.Stamina.getChildByName("time").getComponent(cc.Label).string = timeTemp; } } }.bind(this); this.schedule(this.scheduleCallback, 1); } // 停止倒计时 stopTimeCutDown() { if (this.scheduleCallback) { this.unschedule(this.scheduleCallback); } } //点击充值购买 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_1": price = 600; break; case "gold_2": price = 3600; break; case "gold_3": price = 6800; break; case "gold_4": price = 12800; break; case "gold_5": price = 32800; break; case "gold_6": price = 64800; break; } console.log("获得商品id:", id, count, price); // 判断设备系统 let systemType = "Android"; try { //@ts-ignore const systemInfo = wx.getSystemInfoSync(); if (systemInfo.platform === 'ios') { systemType = "ios"; } } catch (e) { console.error('获取系统信息失败', e); } // Utils.GoKEFu(); if (systemType == "ios") { MiniGameSdk.API.showToast("IOS系统暂不支持支付"); this.btn_Touch = true; // Utils.GoKEFu(); } else { const data = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, } cc.fx.GameTool.shushu_Track("init_order", data); Utils.buyProp(id, count, price, (res) => { console.log("获得充值结果", res); if (res == null) { MiniGameSdk.API.showToast("网络异常,充值失败请稍后再试"); this.btn_Touch = true; const dataFail = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "网络异常,没有拉起支付", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail); return; } else { Utils.getPayInfo((data) => { console.log("获得轮训结果:", data); if (data.data.pay_state == 1) { this.btn_Touch = true; MiniGameSdk.API.showToast("取消充值"); const dataFail2 = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "用户取消支付", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail2); } else if (data.data.pay_state == 2) { this.btn_Touch = true; Utils.setPayInfo((data) => { console.log("设置轮训结果:", data); if (data.code === 1) { MiniGameSdk.API.showToast("充值成功"); cc.fx.GameTool.shopBuy(productId); console.log("充值成功获得金币"); const dataSuccess = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, } cc.fx.GameTool.shushu_Track("payment", dataSuccess); let name = "购买金币道具:" + productId; MiniGameSdk.API.yinli_Pay(price, Utils.outTradeNo, name) } else { MiniGameSdk.API.showToast("网络异常,充值奖励将在登录后再次发放"); const dataFail4 = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "成功付款,但是发货时请求服务器失败,充值成功未发货", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail4); } NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true); this.node.parent.getComponent("JiaZai").updateCoin(); }) } else { NumberToImage.numberToImageNodes(cc.fx.GameConfig.GM_INFO.coin, 30, 15, "coin_", this.coin, true); this.node.parent.getComponent("JiaZai").updateCoin(); const dataFail3 = { outTradeNo: Utils.outTradeNo, price: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "拉起支付后,付款时网络异常付款失败", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail3); } }) } }); } } // update (dt) {} }