Files
18xchat/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts
T

361 lines
11 KiB
TypeScript
Raw Normal View History

2025-09-25 18:11:27 +08:00
import { _decorator, Label, Node, ScrollView } from "cc";
2025-08-15 13:55:26 +08:00
import li_BaseView from "../../../Main/Common/li_BaseView";
import Utils from "../../../Main/Common/Utils";
import { GButton } from "../../../Main/Common/GButton";
import { PurchasePanelItem } from "../../uiitems/PurchasePanelItem";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
2025-09-18 14:06:53 +08:00
import { InnerMsgCode } from "../../../Main/Config/InnerMsgCode";
2025-09-10 11:37:30 +08:00
import { DataManager, DataId } from "../../data/DataManager";
import { WalletData } from "../../data/WalletData";
import { ShopData } from "../../data/ShopData";
2025-09-10 15:26:56 +08:00
import { GirlData } from "../../data/GirlData";
2025-09-10 11:37:30 +08:00
import { ShopService } from "db://assets/Scripts/chat18x/network/services/ShopService";
2025-09-11 10:44:49 +08:00
import { PaymentService } from "db://assets/Scripts/chat18x/payment/PaymentService";
2025-09-18 14:06:53 +08:00
import proto from "db://assets/Scripts/proto/proto.pb.js";
2025-09-25 18:11:27 +08:00
import { NavigationManager } from "../../manager/NavigationManager";
2025-09-18 22:27:02 +08:00
import { Web3PopPanel } from "./Web3PopPanel";
2025-09-21 16:26:18 +08:00
import { TipsPanel } from "./TipsPanel";
2025-09-21 20:25:36 +08:00
import { logger } from "db://assets/Scripts/Main/Common/Logger";
2025-10-10 18:21:33 +08:00
import { PaymentMethod } from "db://assets/Scripts/chat18x/payment/types";
2025-09-10 11:37:30 +08:00
2025-08-15 13:55:26 +08:00
const { ccclass, property } = _decorator;
@ccclass("PurchasePanel")
export class PurchasePanel extends li_BaseView {
private _nodeTab: any = {};
2025-09-10 11:37:30 +08:00
private coinNum: Label;
2025-08-15 13:55:26 +08:00
private vipLeftTime: Label;
2025-09-10 11:37:30 +08:00
private vipPrice7: Label;
private vipTime7: Label;
private vipPrice30: Label;
private vipTime30: Label;
2025-08-15 13:55:26 +08:00
2025-09-18 22:27:02 +08:00
private currentPayType: proto.cs.EnmPayType =
proto.cs.EnmPayType.EPT_Cash_INR;
2025-09-19 00:50:31 +08:00
private defaultNetworkType: proto.cs.EnmNetworkType =
proto.cs.EnmNetworkType.ENT_TRON;
2025-09-18 22:27:02 +08:00
public web3PopPanel: Web3PopPanel;
2025-08-15 13:55:26 +08:00
@property([PurchasePanelItem])
public items: PurchasePanelItem[] = [];
2025-09-18 22:27:02 +08:00
getDefaultNetworkType(): proto.cs.EnmNetworkType {
return this.defaultNetworkType;
}
2025-09-19 21:20:20 +08:00
cashBtnSelecting;
web3BtnSelecting;
2025-10-13 16:20:37 +08:00
openUIDataCT(data: any): void {}
2025-08-15 13:55:26 +08:00
onLoadCT(): void {
2025-09-21 16:04:51 +08:00
this.node.active = false;
this.scheduleOnce(() => {
this.node.active = true;
}, 0.2);
2025-08-15 13:55:26 +08:00
Utils.parseNode(this.node, this._nodeTab);
2025-09-10 11:37:30 +08:00
this.coinNum = this._nodeTab.coinNum.getComponent(Label);
2025-08-15 13:55:26 +08:00
this.vipLeftTime = this._nodeTab.vipText.getComponent(Label);
2025-09-10 11:37:30 +08:00
this.vipPrice7 = this._nodeTab.vipPrice7.getComponent(Label);
this.vipTime7 = this._nodeTab.vipTime7.getComponent(Label);
this.vipPrice30 = this._nodeTab.vipPrice30.getComponent(Label);
this.vipTime30 = this._nodeTab.vipTime30.getComponent(Label);
2025-09-19 21:20:20 +08:00
this.cashBtnSelecting = this._nodeTab.cashBtn.getChildByName("selecting");
this.web3BtnSelecting = this._nodeTab.web3Btn.getChildByName("selecting");
2025-08-15 13:55:26 +08:00
this.bandBtn();
//订阅商业化数据事件
2025-09-10 11:37:30 +08:00
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, () => {
this.onBalanceChange();
2025-09-18 14:06:53 +08:00
});
2025-09-10 11:37:30 +08:00
Utils.addInnerEL(InnerMsgCode.VipExpireChange, this, () => {
this.onVipExpireChange();
2025-09-18 14:06:53 +08:00
});
2025-09-19 00:50:31 +08:00
Utils.addInnerEL(
InnerMsgCode.PayOrderCreateSucc,
this,
this.createOrRefreshWeb3PayPanel
);
2025-08-15 13:55:26 +08:00
2025-09-26 11:50:20 +08:00
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.onVipExpireChange();
});
2025-09-18 14:06:53 +08:00
for (let index = 0; index < this.items.length; index++) {
const element = this.items[index];
element.init(this);
}
2025-09-10 11:37:30 +08:00
this.Show();
2025-08-15 13:55:26 +08:00
}
2025-09-10 11:37:30 +08:00
2025-08-15 13:55:26 +08:00
bandBtn() {
GButton.BandClick(
2025-09-10 15:26:56 +08:00
this._nodeTab.Buy7DayVipBt,
2025-08-15 13:55:26 +08:00
() => {
2025-09-10 16:00:11 +08:00
// 购买7天会员
2025-09-10 15:26:56 +08:00
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
const memberId = shopData.get7DayMemberId();
this.reqBuyGood(memberId);
2025-08-15 13:55:26 +08:00
},
this
);
2025-09-18 23:33:06 +08:00
2025-08-15 13:55:26 +08:00
GButton.BandClick(
2025-09-10 15:26:56 +08:00
this._nodeTab.Buy30DayVipBtn,
2025-08-15 13:55:26 +08:00
() => {
2025-09-10 16:00:11 +08:00
// 购买30天会员
2025-09-10 15:26:56 +08:00
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
const memberId = shopData.get30DayMemberId();
2025-09-18 14:06:53 +08:00
this.reqBuyGood(memberId);
2025-08-15 13:55:26 +08:00
},
this
2025-09-18 14:06:53 +08:00
);
2025-09-18 23:33:06 +08:00
GButton.BandClick(
this._nodeTab.cashBtn,
() => {
// 购买7天会员
this.onClickCashBtn();
},
this
);
GButton.BandClick(
this._nodeTab.web3Btn,
() => {
// 购买7天会员
this.onClickWeb3Btn();
},
this
);
2025-08-15 13:55:26 +08:00
GButton.BandClick(
2025-09-10 15:26:56 +08:00
this._nodeTab.ReturnBtn,
2025-08-15 13:55:26 +08:00
() => {
2025-09-21 14:28:17 +08:00
NavigationManager.Instance.closeSubPanel(this);
2025-08-15 13:55:26 +08:00
},
this
);
}
2025-09-10 11:37:30 +08:00
async Show() {
// 请求商城数据
2025-09-18 14:06:53 +08:00
const reqData = {};
2025-09-10 11:37:30 +08:00
let res = await ShopService.I.reqShopList(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
// 保存数据
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
shopData.goods = res.data;
// 根据数据,刷新界面
this.updateView();
2025-09-19 21:20:20 +08:00
this.onClickCashBtn();
2025-09-10 11:37:30 +08:00
}
}
2025-09-18 14:06:53 +08:00
shopData: ShopData;
2025-08-15 13:55:26 +08:00
updateView() {
this.getPlayerMerData();
2025-09-18 14:06:53 +08:00
this.shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
2025-09-10 11:37:30 +08:00
// VIP会员
2025-09-18 14:06:53 +08:00
const memberId7 = this.shopData.get7DayMemberId();
const price7 = this.shopData.getOriginalPriceById(memberId7);
2025-10-13 15:39:35 +08:00
this.vipPrice7.string = `${price7}`;
2025-09-18 14:06:53 +08:00
const memberId30 = this.shopData.get30DayMemberId();
const price30 = this.shopData.getOriginalPriceById(memberId30);
2025-10-13 15:39:35 +08:00
this.vipPrice30.string = `${price30}`;
2025-09-18 14:06:53 +08:00
}
// 0:cash 1:web3
updateShops(type: number = 0) {
// 充值商品 缺:分开获取现金充值或者web3币充值
const rechargeIds = this.shopData.getRechargeIds();
2025-09-10 11:37:30 +08:00
for (let index = 0; index < this.items.length; index++) {
const element = this.items[index];
2025-09-18 22:27:02 +08:00
element.refresh(type, rechargeIds[index]);
}
if (type == 0) {
this.currentPayType = proto.cs.EnmPayType.EPT_Cash_INR;
} else {
this.currentPayType = proto.cs.EnmPayType.EPT_WEB3_USD;
2025-09-10 11:37:30 +08:00
}
2025-08-15 13:55:26 +08:00
}
// 获取支付方式
getPaymentMethod(): PaymentMethod {
if (this.currentPayType === proto.cs.EnmPayType.EPT_Cash_INR) {
// 现金支付
return PaymentMethod.CashPay;
} else if (this.currentPayType === proto.cs.EnmPayType.EPT_WEB3_USD) {
// Web3支付
return PaymentMethod.Web3;
} else {
// Web3支付
return PaymentMethod.Web3;
}
}
2025-09-18 22:27:02 +08:00
purchaseGood(id: number, networkType: proto.cs.EnmNetworkType) {
2025-09-11 10:44:49 +08:00
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
const isRechargeId = shopData.isRechargeId(id);
if (isRechargeId) {
// 需要外部支付进行购买
2025-10-10 18:21:33 +08:00
this.startPayment(
id,
this.currentPayType,
networkType,
this.getPaymentMethod()
2025-10-10 18:21:33 +08:00
);
2025-09-11 10:44:49 +08:00
}
2025-09-18 23:33:06 +08:00
//测试用
// Utils.sendInnerMsg(InnerMsgCode.PayOrderCreateSucc, {
// orderId: 123,
// payType: 1,
// goodId: 1001,
// });
2025-08-15 13:55:26 +08:00
}
2025-09-18 22:27:02 +08:00
createOrRefreshWeb3PayPanel(param: any) {
2025-09-19 11:10:20 +08:00
if (param.payType == proto.cs.EnmPayType.EPT_Cash_INR) return;
2025-09-19 00:50:31 +08:00
let data = {
2025-09-18 22:27:02 +08:00
orderId: param.orderId,
payType: param.payType,
goodId: param.goodId,
2025-09-19 00:50:31 +08:00
networkType: this.getDefaultNetworkType(),
2025-09-22 16:40:56 +08:00
payPanel: this,
2025-09-18 22:27:02 +08:00
};
2025-09-22 15:31:15 +08:00
if (this.web3PopPanel == null)
2025-10-13 16:20:37 +08:00
NavigationManager.Instance.openPopupPanel("Web3PopPanel", data);
2025-09-22 15:31:15 +08:00
else {
data.networkType = undefined;
this.web3PopPanel.refreshData(data);
this.web3PopPanel.refreshView();
}
2025-09-18 22:27:02 +08:00
}
2025-08-15 13:55:26 +08:00
getPlayerMerData() {
//todo: 获取服务器玩家商业化数据
2025-09-10 11:37:30 +08:00
this.refreshCoinNum();
this.refreshVipExpire();
2025-08-15 13:55:26 +08:00
}
2025-09-10 11:37:30 +08:00
onBalanceChange() {
this.refreshCoinNum();
2025-08-15 13:55:26 +08:00
}
2025-09-10 11:37:30 +08:00
// 刷新余额
private refreshCoinNum() {
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
const balance = walletData.getOriginalBalance();
this.coinNum.string = balance.toString();
2025-09-18 14:06:53 +08:00
}
2025-09-10 11:37:30 +08:00
onVipExpireChange() {
this.refreshVipExpire();
}
// 刷新 vip失效时间戳
private refreshVipExpire() {
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
const vipExpire = walletData.vipExpire;
const leftTime = Utils.formatVipLeftTime(vipExpire);
if (leftTime == null) {
LanguageUtils.getText("purchasepanel.notvip");
} else {
this.vipLeftTime.string = LanguageUtils.getText(
"purchasepanel.vip_left"
).replace("${leftTime}", leftTime);
}
2025-09-18 14:06:53 +08:00
}
2025-08-15 13:55:26 +08:00
2025-09-10 15:26:56 +08:00
// 使用余额购买商品
async reqBuyGood(goodId: number) {
// 请求购买商品
const reqData = {
goodId: goodId,
girlId: 0,
};
let res = await ShopService.I.reqBuyGood(reqData);
2025-09-21 20:25:36 +08:00
logger.log("请求使用余额购买商品的响应数据:", res);
2025-09-10 15:26:56 +08:00
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const resData = res.data;
// 保存数据
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
walletData.balance = Number(resData.balance);
walletData.vipExpire = Number(resData.vipExpire);
// 刷新界面
2025-09-21 21:10:41 +08:00
TipsPanel.show(LanguageUtils.getText("payment.succeed"));
} else {
if (res && res.code) {
const key = "error_code_" + res.code;
TipsPanel.show(LanguageUtils.getText(key));
}
2025-09-10 15:26:56 +08:00
}
2025-09-18 14:06:53 +08:00
}
2025-09-10 15:26:56 +08:00
2025-09-11 10:44:49 +08:00
// 创建订单
2025-10-10 18:21:33 +08:00
async startPayment(
goodId: number,
payType: number,
network: number,
payMethod: PaymentMethod
) {
2025-09-11 10:44:49 +08:00
// 请求数据
const reqData = {
2025-09-18 14:06:53 +08:00
goodId,
2025-09-18 16:19:34 +08:00
payType,
network,
2025-09-11 10:44:49 +08:00
};
2025-09-28 14:15:54 +08:00
let res = await PaymentService.I.startPayment(payMethod, reqData);
2025-09-21 20:25:36 +08:00
logger.log("支付完成的响应数据:", res);
2025-09-11 15:36:15 +08:00
if (res) {
let status = res.status;
if (status === -1) {
2025-09-21 20:25:36 +08:00
logger.log("支付失败!!!,错误码:", res.retCode);
2025-09-21 16:26:18 +08:00
const key = "error_code_" + res.retCode;
TipsPanel.show(LanguageUtils.getText(key));
2025-09-11 15:36:15 +08:00
} else if (status === 0) {
2025-09-21 20:25:36 +08:00
logger.log("支付成功!!!");
2025-09-11 15:36:15 +08:00
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
walletData.balance = Number(res.balance);
2025-09-18 14:06:53 +08:00
walletData.vipExpire = Number(res.vipExpire);
2025-09-21 16:26:18 +08:00
TipsPanel.show(LanguageUtils.getText("payment.succeed"));
2025-09-11 15:36:15 +08:00
}
}
2025-09-18 14:06:53 +08:00
}
2025-09-19 21:20:20 +08:00
curPayType = -1;
2025-09-18 14:06:53 +08:00
onClickCashBtn() {
2025-09-19 21:20:20 +08:00
if (this.curPayType == 0) return;
this.curPayType = 0;
2025-10-10 18:21:33 +08:00
this.cashBtnSelecting.active = true;
this.web3BtnSelecting.active = false;
2025-09-18 14:06:53 +08:00
this.updateShops(0);
}
onClickWeb3Btn() {
2025-09-19 21:20:20 +08:00
if (this.curPayType == 1) return;
this.curPayType = 1;
2025-10-10 18:21:33 +08:00
this.cashBtnSelecting.active = false;
this.web3BtnSelecting.active = true;
2025-09-18 14:06:53 +08:00
this.updateShops(1);
}
2025-09-11 10:44:49 +08:00
2025-08-15 13:55:26 +08:00
onDestroy() {
2025-09-18 22:27:02 +08:00
Utils.removeInnerEL(InnerMsgCode.BalanceChange, this, () => {
this.onBalanceChange();
});
Utils.removeInnerEL(InnerMsgCode.VipExpireChange, this, () => {
this.onVipExpireChange();
});
2025-09-26 11:50:20 +08:00
Utils.removeInnerEL(InnerMsgCode.PayOrderCreateSucc, this, () => {
2025-09-18 22:27:02 +08:00
this.createOrRefreshWeb3PayPanel;
});
2025-09-26 11:50:20 +08:00
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.onVipExpireChange();
});
2025-08-15 13:55:26 +08:00
super.onDestroy();
}
}