Files
18xchat/assets/Scripts/chat18x/ui/panels/PurchasePanel.ts
T
2025-09-10 11:37:47 +08:00

165 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { _decorator, Component, Label, Node } from "cc";
import li_BaseView from "../../../Main/Common/li_BaseView";
import Utils from "../../../Main/Common/Utils";
import { CommercialData } from "../../data/CommercialData";
import { ConfigManager } from "../../manager/ConfigManager";
import { GButton } from "../../../Main/Common/GButton";
import { PurchasePanelItem } from "../../uiitems/PurchasePanelItem";
import { ViewManager } from "../../../Main/Manager/ViewManager";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
import { InnerEventCode, InnerMsgCode } from "../../../Main/Config/InnerMsgCode";
import { DataManager, DataId } from "../../data/DataManager";
import { WalletData } from "../../data/WalletData";
import { ShopData } from "../../data/ShopData";
import { ShopService } from "db://assets/Scripts/chat18x/network/services/ShopService";
import proto from 'db://assets/Scripts/proto/proto.pb.js';
const { ccclass, property } = _decorator;
@ccclass("PurchasePanel")
export class PurchasePanel extends li_BaseView {
private _nodeTab: any = {};
private coinNum: Label;
private vipLeftTime: Label;
private vipPrice7: Label;
private vipTime7: Label;
private vipPrice30: Label;
private vipTime30: Label;
private tryBuyId: number;
@property([PurchasePanelItem])
public items: PurchasePanelItem[] = [];
onLoadCT(): void {
Utils.parseNode(this.node, this._nodeTab);
this.coinNum = this._nodeTab.coinNum.getComponent(Label);
this.vipLeftTime = this._nodeTab.vipText.getComponent(Label);
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);
this.bandBtn();
//订阅商业化数据事件
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, () => {
this.onBalanceChange();
});
Utils.addInnerEL(InnerMsgCode.VipExpireChange, this, () => {
this.onVipExpireChange();
});
this.Show();
}
bandBtn() {
GButton.BandClick(
this._nodeTab.BuyVipBtn,
() => {
//TODO:购买vip
console.log("购买Vip");
},
this
);
GButton.BandClick(
this._nodeTab.BuyDiamondBtn,
() => {
//TODO:购买钻石
console.log("购买钻石,商品id" + this.tryBuyId);
},
this
);
GButton.BandClick(
this._nodeTab.QUIT,
() => {
//TODO:购买钻石
ViewManager.I.closeView(this.node);
},
this
);
}
async Show() {
// 请求商城数据
const reqData = {
};
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();
}
}
updateView() {
this.getPlayerMerData();
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
// VIP会员
const memberIds = shopData.getMemberIds();
for (let i = 0; i < memberIds.length; i++) {
const id = memberIds[i];
const price = shopData.getOriginalPriceById(id);
if (i === 0) {
this.vipPrice7.string = `$ ${price}`;
} else if (i === 1) {
this.vipPrice30.string = `$ ${price}`;
}
}
// 充值商品
const rechargeIds = shopData.getRechargeIds();
for (let index = 0; index < this.items.length; index++) {
const element = this.items[index];
element.init(this, rechargeIds[index]);
}
this.items.forEach((item) => item.show());
}
setDiamondTag(id: number) {
this.tryBuyId = id;
}
getPlayerMerData() {
//todo: 获取服务器玩家商业化数据
this.refreshCoinNum();
this.refreshVipExpire();
}
onBalanceChange() {
this.refreshCoinNum();
}
// 刷新余额
private refreshCoinNum() {
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
const balance = walletData.getOriginalBalance();
this.coinNum.string = balance.toString();
}
onVipExpireChange() {
this.refreshVipExpire();
}
// 刷新 vip失效时间戳
private refreshVipExpire() {
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
const vipExpire = walletData.vipExpire;
const leftTime = Utils.formatVipLeftTime(vipExpire);
this.vipLeftTime.string = LanguageUtils.getText(
"purchasepanel.vip_left"
).replace("${leftTime}", leftTime);
}
onDestroy() {
Utils.removeInnerEL(InnerMsgCode.BalanceChange, this, () => {
});
Utils.removeInnerEL(InnerMsgCode.VipExpireChange, this, () => {
});
super.onDestroy();
}
}