import GameManager from "./GameManager"; import LoadingCatAnimation from "./LoadingCatAnimation"; import Utils from "./module/Pay/Utils"; import { STAMINA_PACK } from "./module/Config/ShopCatalog"; import { beginStaminaPackOffer, canShowStaminaPack, closeStaminaPackOffer, getStaminaPackRecord } from "./module/Config/StaminaPackState"; import { MiniGameSdk } from "./Sdk/MiniGameSdk"; declare const wx: any; const { ccclass, property } = cc._decorator; @ccclass export default class StaminaPack extends cc.Component { @property(cc.Label) timeLabel: cc.Label = null; @property(cc.Button) buyButton: cc.Button = null; @property(cc.Button) closeButton: cc.Button = null; private paying = false; private checkingIosOrder = false; private pendingOrder = ""; private confirmedOrder = ""; private uid = ""; private onShowListener: () => void = null; static tryOpen(): boolean { const canvas = cc.find("Canvas"); if (!canvas) return false; if (canvas.getChildByName("heathpop2")) return true; if (!canShowStaminaPack()) return false; const prefab = GameManager._instance && GameManager._instance.staminaPackPrefab; if (!prefab) return false; const node = cc.instantiate(prefab); beginStaminaPackOffer(); canvas.addChild(node); node.zIndex = 1001; return true; } onLoad() { this.uid = cc.fx.GameConfig.GM_INFO.uid; this.closeConfirmBox(); this.setPaying(false); this.node.addComponent(cc.BlockInputEvents); this.buyButton.node.on("click", this.buyProduct, this); this.closeButton.node.on("click", this.closePop, this); cc.director.on("stamina-pack-granted", this.closeAfterPurchase, this); this.refreshCountdown(); this.schedule(this.refreshCountdown, 1); if (typeof wx !== "undefined") { this.onShowListener = () => this.checkIosOrder(); wx.onShow(this.onShowListener); } this.checkIosOrder(); } private refreshCountdown() { const seconds = Math.max(0, Math.ceil((getStaminaPackRecord().offerEnd - Date.now()) / 1000)); this.timeLabel.string = Math.floor(seconds / 60).toString().padStart(2, "0") + ":" + (seconds % 60).toString().padStart(2, "0"); } closePop() { cc.fx.AudioManager._instance.playEffect("anniu_little", null); closeStaminaPackOffer(); this.node.destroy(); } private closeAfterPurchase() { this.node.active = false; this.node.destroy(); } private setPaying(value: boolean) { this.paying = value; if (!cc.isValid(this, true)) return; this.buyButton.interactable = !value; const loading = this.node.getChildByName("Loading"); if (value) { this.closeConfirmBox(); LoadingCatAnimation.play(loading); } else LoadingCatAnimation.stop(loading); } private openConfirmBox() { this.setPaying(false); if (cc.isValid(this, true)) this.node.getChildByName("ConfirmBox").active = true; } private closeConfirmBox() { if (cc.isValid(this, true)) this.node.getChildByName("ConfirmBox").active = false; } againGet() { if (this.paying) return; cc.fx.AudioManager._instance.playEffect("anniu_little", null); this.closeConfirmBox(); if (this.pendingOrder) this.checkNativeOrder(); else this.checkIosOrder(); } buyProduct() { if (this.paying) return; cc.fx.AudioManager._instance.playEffect("anniu_little", null); if (this.pendingOrder) { this.checkNativeOrder(); return; } if (cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder) { this.checkIosOrder(); return; } const systemType = typeof wx !== "undefined" && wx.getSystemInfoSync().platform === "ios" ? "ios" : "Android"; this.setPaying(true); if (systemType === "ios" && !cc.fx.GameConfig.GM_INFO.iosCanPay) { this.iosOldPay(); return; } Utils.buyProp(STAMINA_PACK.id, 1, STAMINA_PACK.price, systemType, STAMINA_PACK.id, res => { if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return; if (res?.errCode === 16) { this.iosOldPay(); return; } if (!res || res.err || !res.outTradeNo) { this.setPaying(false); MiniGameSdk.API.showToast(res?.errCode === -2 ? "取消充值" : "充值失败"); return; } this.pendingOrder = res.outTradeNo; this.checkNativeOrder(); }); } private checkNativeOrder() { const order = this.pendingOrder; this.setPaying(true); if (this.confirmedOrder === order) { this.finishNativeOrder(order); return; } // 沿用新手礼包的查单重试及 30 秒等待上限,不重新创建订单。 Utils.getPayInfo(data => { if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return; if (data?.code === 1 && data.data?.pay_state === 2) { this.confirmedOrder = order; this.finishNativeOrder(order); } else if (data?.code === 1 && data.data?.pay_state === 1) { this.pendingOrder = ""; this.setPaying(false); } else this.openConfirmBox(); }, order, { timeoutMs: 30000 }); } private finishNativeOrder(order: string) { Utils.setPayInfo(result => { if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return; this.setPaying(false); if (result?.code === 1) { this.pendingOrder = this.confirmedOrder = ""; this.completePurchase(order); } else this.openConfirmBox(); }, order); } private iosOldPay() { Utils.GoKEFu({ price: STAMINA_PACK.price, payment_name: STAMINA_PACK.id, payment_count: 1 }, result => { if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return; if (!this.checkingIosOrder) this.setPaying(false); if (result !== "success") { cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder = ""; MiniGameSdk.API.showToast("未能打开支付,请重试"); } }); } private checkIosOrder() { const order = cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder; // 返回事件可能早于客服回调,购买中不能阻止查单。 if (this.uid !== cc.fx.GameConfig.GM_INFO.uid || !order || this.checkingIosOrder) return; this.checkingIosOrder = true; this.setPaying(true); Utils.getIosPayInfo(order, result => { this.checkingIosOrder = false; if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return; this.setPaying(false); if (result?.code === 1 && result.data?.payment_name === STAMINA_PACK.id) { cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder = ""; this.completePurchase(order); } else if (result?.code === 0 && !result.networkError) { cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder = ""; MiniGameSdk.API.showToast(result.msg || "充值失败"); } else this.openConfirmBox(); }); } private completePurchase(order: string) { // 正常发货与登录补单共用 shopBuy,统一去重、计冷却并打开奖励窗。 cc.fx.GameTool.shopBuy(STAMINA_PACK.id, false, { outTradeNo: order }); MiniGameSdk.API.yinli_Pay(STAMINA_PACK.price, order, "购买金币道具:" + STAMINA_PACK.id); const canvas = cc.find("Canvas"); const home = canvas && canvas.getComponent("JiaZai"); if (home) { home.updateCoin(); home.setHealthInfo(false); } const scene = canvas && canvas.getComponent("SceneManager"); if (scene) scene.refreshJungleRewardDisplay(); } onDestroy() { cc.director.off("stamina-pack-granted", this.closeAfterPurchase, this); if (this.onShowListener) wx.offShow(this.onShowListener); } }