// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import JiaZai from "./JiaZai"; import LoadingCatAnimation from "./LoadingCatAnimation"; import MapConroler from "./Map"; import Utils from "./module/Pay/Utils"; import NumberToImage from "./NumberToImage"; import SceneManager from "./SceneManager"; import { MiniGameSdk } from "./Sdk/MiniGameSdk"; const { ccclass, property } = cc._decorator; @ccclass export default class Revive extends cc.Component { btn_Touch: boolean = false; // LIFE-CYCLE CALLBACKS: private onShowListener: () => void; private iosPrice: number = 0; private iosProductId: string = ""; private iosCount: number = 1; private iosMap: MapConroler = null; private iosUid: string = ""; private checkingIosOrder: boolean = false; public home: number = 0; onLoad() { this.btn_Touch = true; // 检测微信小游戏切到后台 } onShow() { if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) { console.log("从后台进入前台订单号:", cc.fx.GameConfig.GM_INFO.iosReviveOrder); if (cc.fx.GameConfig.GM_INFO.iosReviveOrder != null && cc.fx.GameConfig.GM_INFO.iosReviveOrder != "") { if (this.checkingIosOrder) return; this.checkingIosOrder = true; console.log("有苹果订单号,开始轮训"); const iosReviveOrder = cc.fx.GameConfig.GM_INFO.iosReviveOrder; this.openLoad(); this.btn_Touch = false; const purchaseMap = this.iosMap; const purchaseUid = this.iosUid || cc.fx.GameConfig.GM_INFO.uid; Utils.getIosPayInfo(iosReviveOrder, (data) => { this.checkingIosOrder = false; if (purchaseUid !== cc.fx.GameConfig.GM_INFO.uid) return; console.log("获得轮训结果:", data); const iosID = data.data?.payment_name || this.iosProductId; let iosAmount = data.data?.goodsPrice || this.iosPrice; iosAmount = parseInt(iosAmount); if (data.code == 1) { console.log("购买成功"); let name = "购买金币道具:" + iosID; console.log("引力付费透传", iosAmount, iosReviveOrder, name); MiniGameSdk.API.yinli_Pay(iosAmount, iosReviveOrder, name) this.closeLoad(); this.btn_Touch = true; console.log("_________正式发货"); MiniGameSdk.API.showToast("充值成功"); this.completePurchase(iosID, iosReviveOrder, purchaseMap, purchaseUid); Utils.setPayInfo(() => {}, iosReviveOrder); if (cc.fx.GameConfig.GM_INFO.iosReviveOrder === iosReviveOrder) cc.fx.GameConfig.GM_INFO.iosReviveOrder = null; //console.log("充值成功获得金币"); this.updateGameCoin(); } else if (data.code == 0) { console.log("用户自己取消充值"); MiniGameSdk.API.showToast("充值失败"); this.closeLoad(); this.btn_Touch = true; const dataFail = { outTradeNo: iosReviveOrder, pay_amount: iosAmount, payment_name: iosID, payment_num: this.iosCount, type: "ios", fail_reason: "用户取消充值", } if (iosID != undefined && iosID != "" && iosID != null) { cc.fx.GameTool.shushu_Track("payment_fail", dataFail); } if (cc.fx.GameConfig.GM_INFO.iosReviveOrder === iosReviveOrder) cc.fx.GameConfig.GM_INFO.iosReviveOrder = null; } else if (data.code == 2) { this.closeLoad(); this.btn_Touch = true; console.log("轮训超时"); MiniGameSdk.API.showToast("请检查网络链接后, 再次领取奖励", 4); const dataFail = { outTradeNo: iosReviveOrder, pay_amount: iosAmount, payment_name: iosID, payment_num: this.iosCount, type: "ios", fail_reason: "用户充值后,轮训结果超时", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail); this.openConfirmBox(); } this.btn_Touch = true; }) } } } init() { if (!cc.fx.GameConfig.GM_INFO.canIos) { this.node.active = false; return; } this.node.active = true; this.updatePrice(); this.addListener(); this.btn_Touch = true; } private updatePrice() { const map = MapConroler._instance; const price = this.node.getChildByName("price"); const label = price && price.getComponent(cc.Label); if (!map || !label) return; // 固定数字右边缘,让两位数向左展开,保留旁边独立的“元”节点。 price.x += price.width * (1 - price.anchorX); price.anchorX = 1; label.string = String(map.getRebornGiftProduct().price / 100); } private completePurchase(productId: string, order: string, purchaseMap: MapConroler, uid: string) { if (uid !== cc.fx.GameConfig.GM_INFO.uid || !order || !/^reborn_Gift(?:_[2-5])?$/.test(productId)) return; const currentRun = purchaseMap && cc.isValid(purchaseMap, true) && MapConroler._instance === purchaseMap; if (currentRun) purchaseMap.recordRebornGiftPurchase(order); if (cc.fx.GameTool.shopBuy(productId, false, { outTradeNo: order }) === false) return; this.buyGift(); // 旧局订单仍发奖励,但不能复活新局或提高新局的礼包价格。 if (currentRun) { this.revivew(purchaseMap); } } addListener() { if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) { if (this.onShowListener) { //@ts-ignore wx.offShow(this.onShowListener); } // 定义监听函数 this.onShowListener = () => { this.onShow(); }; //@ts-ignore wx.onShow(this.onShowListener); } } //购买 buyGift() { Utils.rebornGift(1, (data) => { console.log("购买'✅ ", data.code); if (data.code == 1) { } }) // update (dt) {} } //发奖励 revivew(map: MapConroler) { cc.fx.GameConfig.GM_INFO.revive = 0; if (map) map.setPropNum(); const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点 const jiazaiComp = jiazaiNode && jiazaiNode.getComponent(JiaZai); if (jiazaiComp) { setTimeout(() => { if (cc.isValid(jiazaiComp)) jiazaiComp.updateCoin(); }, 300); } else { console.log("无法获取JiaZai组件"); } let shop = cc.find("Canvas/shop"); if (shop) { let shopComp = shop.getComponent("shop"); if (shopComp) { shopComp.openShop(); } } setTimeout(() => { if (cc.isValid(this) && this.node && cc.isValid(this.node)) { this.node.active = false; } }, 200); if (map && cc.isValid(map)) map.runRewiveCopy(); } buyProduct(customData) { // cc.fx.GameTool.shopBuy("revive", false); // MapConroler._instance.setPropNum(); if (!this.btn_Touch) { return; } if (cc.fx.GameConfig.GM_INFO.iosReviveOrder) { this.onShow(); return; } const purchaseMap = MapConroler._instance; if (!purchaseMap || !cc.isValid(purchaseMap, true)) return; const purchaseUid = cc.fx.GameConfig.GM_INFO.uid; this.btn_Touch = false; const product = purchaseMap.getRebornGiftProduct(); const productId = product.id; const id = productId; const price = product.price; const count = 1; 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" && !cc.fx.GameConfig.GM_INFO.iosCanPay) { // MiniGameSdk.API.showToast("IOS系统暂不支持支付"); // this.btn_Touch = true; this.openLoad(); this.iosOldPay(price, productId, purchaseMap, purchaseUid); } else { // MiniGameSdk.API.showToast("充值成功"); // cc.fx.GameTool.shopBuy(productId, false); // setTimeout(() => { // if (productId == "unlimited_health_bundle_1" || // productId == "unlimited_health_bundle_2" || // productId == "unlimited_health_bundle_3" // ) { // console.log("触发————————updatePower"); // this.updatePower(); // } // }, 500); this.openLoad(); console.log("7.14_____________________", "调用充值接口"); Utils.buyProp(id, count, price, systemType, productId, (res) => { if (purchaseUid !== cc.fx.GameConfig.GM_INFO.uid) return; console.log("获得充值结果", res); if (res == null) { MiniGameSdk.API.showToast("充值失败"); this.btn_Touch = true; const dataFail = { outTradeNo: Utils.outTradeNo, pay_amount: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "网络异常,没有拉起支付", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail); this.closeLoad(); return; } else if (res.err) { console.log("错误原因:", res.errMsg); if (res.errCode == 16) { // console.log("走ios旧支付"); this.iosOldPay(price, productId, purchaseMap, purchaseUid); return; } MiniGameSdk.API.showToast("充值失败"); //console.log(res); this.btn_Touch = true; let name = "支付拉起失败"; if (res.errCode == -2) { name = "用户取消充值"; } const dataFail = { outTradeNo: Utils.outTradeNo, pay_amount: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: name, } cc.fx.GameTool.shushu_Track("payment_fail", dataFail); this.closeLoad(); return; } else { const orderNumber = res.outTradeNo; if (!orderNumber) { this.btn_Touch = true; this.closeLoad(); MiniGameSdk.API.showToast("未获取到充值订单,请稍后重试"); return; } Utils.getPayInfo((data) => { if (purchaseUid !== cc.fx.GameConfig.GM_INFO.uid) return; console.log("7.14_______________充值成功,准备轮训"); console.log("获得轮训结果:", data); this.closeLoad(); if (data.data.pay_state == 1) { this.btn_Touch = true; MiniGameSdk.API.showToast("取消充值"); const dataFail2 = { outTradeNo: orderNumber, pay_amount: 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) { if (cc.isValid(purchaseMap, true) && MapConroler._instance === purchaseMap) purchaseMap.recordRebornGiftPurchase(orderNumber); // const dataSuccess = { // outTradeNo: Utils.outTradeNo, // pay_amount: price, // payment_name: productId, // payment_num: 1, // type: systemType, // } // cc.fx.GameTool.shushu_Track("payment", dataSuccess); let name = "购买金币道具:" + productId; console.log("引力付费透传", price, orderNumber, name); MiniGameSdk.API.yinli_Pay(price, orderNumber, name) console.log("7.14_______________充值成功,轮训成功,准备发货"); this.completePurchase(productId, orderNumber, purchaseMap, purchaseUid); Utils.setPayInfo( (res) => { if (purchaseUid !== cc.fx.GameConfig.GM_INFO.uid) return; this.btn_Touch = true; console.log("设置轮训结果:", res); if (res.code === 1) { console.log("7.14_________正式发货"); MiniGameSdk.API.showToast("充值成功"); //console.log("充值成功获得金币"); } else { MiniGameSdk.API.showToast("充值已到账,订单确认将在登录后重试"); const dataFail4 = { outTradeNo: orderNumber, pay_amount: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "充值已到账,确认发货请求失败", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail4); } this.updateGameCoin(); if (cc.isValid(this, true)) this.updatePrice(); }, orderNumber) } else { const dataFail3 = { outTradeNo: orderNumber, pay_amount: price, payment_name: productId, payment_num: 1, type: systemType, fail_reason: "拉起支付后,付款时网络异常付款失败", } cc.fx.GameTool.shushu_Track("payment_fail", dataFail3); this.btn_Touch = true; this.updateGameCoin(); } }, orderNumber) } }); } } openLoad() { if (!cc.isValid(this, true)) return; const canvas = cc.find("Canvas"); const loading = canvas && canvas.getChildByName("Loading"); if (loading) LoadingCatAnimation.play(loading); } closeLoad() { if (!cc.isValid(this, true)) return; const canvas = cc.find("Canvas"); const loading = canvas && canvas.getChildByName("Loading"); if (loading) LoadingCatAnimation.stop(loading); } private updateGameCoin() { const canvas = cc.find("Canvas"); const sceneManager = canvas && canvas.getComponent(SceneManager); if (sceneManager && cc.isValid(sceneManager)) sceneManager.updateCoin(); } offShow() { if (cc.fx.GameConfig.GM_INFO.revive == 0) { return; } // 移除 wx.onShow 监听器 if ((cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) && this.onShowListener) { console.log("复活移除监听"); //@ts-ignore wx.offShow(this.onShowListener); console.log("🔥🔥🔥🔥🔥🔥🔥🔥"); } } onDestroy() { if ((cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) && this.onShowListener) { //@ts-ignore wx.offShow(this.onShowListener); this.onShowListener = null; } } openConfirmBox() { if (this.node && cc.isValid(this.node) && this.node.parent) { let ConfirmBox = this.node.parent.getChildByName("ConfirmBox"); if (ConfirmBox) ConfirmBox.active = true; } this.closeLoad(); } closeConfirmBox() { if (this.node && cc.isValid(this.node) && this.node.parent) { let ConfirmBox = this.node.parent.getChildByName("ConfirmBox"); if (ConfirmBox) ConfirmBox.active = false; } } //再次领取奖励 againGet() { this.closeConfirmBox(); this.onShow(); } iosOldPay(price, productId, purchaseMap: MapConroler, uid: string) { // 旧版本ios支付 let iosPayInfo = { price: price, payment_name: productId, payment_count: 1, } this.iosPrice = price; this.iosProductId = productId; this.iosCount = 1; this.iosMap = purchaseMap; this.iosUid = uid; console.log("准备跳客服回话:"); console.log(this.iosProductId); Utils.GoKEFu(iosPayInfo, (res) => { if (res == "success") { console.log("客服回话成功"); } else { console.log("客服回话失败"); this.btn_Touch = true; cc.fx.GameConfig.GM_INFO.iosReviveOrder = null; this.closeLoad(); } }); } }