Files
MatchMaster/assets/Script/StaminaPack.ts
T

153 lines
6.1 KiB
TypeScript

import GameManager from "./GameManager";
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 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.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)) this.buyButton.interactable = !value;
}
buyProduct() {
if (this.paying) return;
cc.fx.AudioManager._instance.playEffect("anniu_little", null);
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;
}
const order = res.outTradeNo;
Utils.getPayInfo(data => {
if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return;
if (data?.code !== 1 || data.data?.pay_state !== 2) {
this.setPaying(false);
return;
}
Utils.setPayInfo(result => {
if (this.uid !== cc.fx.GameConfig.GM_INFO.uid) return;
this.setPaying(false);
if (result?.code === 1) this.completePurchase(order);
else MiniGameSdk.API.showToast("网络异常,充值奖励将在登录后再次发放");
}, order);
}, 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;
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 (!order || this.paying) return;
this.setPaying(true);
Utils.getIosPayInfo(order, result => {
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) {
cc.fx.GameConfig.GM_INFO.iosStaminaPackOrder = "";
MiniGameSdk.API.showToast(result.msg || "充值失败");
}
});
}
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);
}
}