349 lines
11 KiB
TypeScript
349 lines
11 KiB
TypeScript
import { _decorator, Label, Node, ScrollView } from "cc";
|
|
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";
|
|
import { InnerMsgCode } from "../../../Main/Config/InnerMsgCode";
|
|
import { DataManager, DataId } from "../../data/DataManager";
|
|
import { WalletData } from "../../data/WalletData";
|
|
import { ShopData } from "../../data/ShopData";
|
|
import { GirlData } from "../../data/GirlData";
|
|
import { ShopService } from "db://assets/Scripts/chat18x/network/services/ShopService";
|
|
import { PaymentService } from "db://assets/Scripts/chat18x/payment/PaymentService";
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|
import { NavigationManager } from "../../manager/NavigationManager";
|
|
import { Web3PopPanel } from "./Web3PopPanel";
|
|
import { TipsPanel } from "./TipsPanel";
|
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
|
|
|
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 ScrollView: ScrollView;
|
|
|
|
private currentPayType: proto.cs.EnmPayType =
|
|
proto.cs.EnmPayType.EPT_Cash_INR;
|
|
private defaultNetworkType: proto.cs.EnmNetworkType =
|
|
proto.cs.EnmNetworkType.ENT_TRON;
|
|
public web3PopPanel: Web3PopPanel;
|
|
@property([PurchasePanelItem])
|
|
public items: PurchasePanelItem[] = [];
|
|
getDefaultNetworkType(): proto.cs.EnmNetworkType {
|
|
return this.defaultNetworkType;
|
|
}
|
|
|
|
cashBtnSelecting;
|
|
cashBtnNotSelect;
|
|
web3BtnSelecting;
|
|
web3BtnNotSelect;
|
|
|
|
base: Node;
|
|
openUIDataCT(data: any): void {
|
|
this.base = data.base;
|
|
}
|
|
onLoadCT(): void {
|
|
this.node.active = false;
|
|
this.scheduleOnce(() => {
|
|
this.node.active = true;
|
|
}, 0.2);
|
|
|
|
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.ScrollView = this._nodeTab.ScrollView.getComponent(ScrollView);
|
|
|
|
this.cashBtnSelecting = this._nodeTab.cashBtn.getChildByName("selecting");
|
|
this.cashBtnNotSelect = this._nodeTab.cashBtn.getChildByName("notSelect");
|
|
this.web3BtnSelecting = this._nodeTab.web3Btn.getChildByName("selecting");
|
|
this.web3BtnNotSelect = this._nodeTab.web3Btn.getChildByName("notSelect");
|
|
|
|
this.bandBtn();
|
|
|
|
//订阅商业化数据事件
|
|
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, () => {
|
|
this.onBalanceChange();
|
|
});
|
|
Utils.addInnerEL(InnerMsgCode.VipExpireChange, this, () => {
|
|
this.onVipExpireChange();
|
|
});
|
|
Utils.addInnerEL(
|
|
InnerMsgCode.PayOrderCreateSucc,
|
|
this,
|
|
this.createOrRefreshWeb3PayPanel
|
|
);
|
|
|
|
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
|
|
this.onVipExpireChange();
|
|
});
|
|
|
|
for (let index = 0; index < this.items.length; index++) {
|
|
const element = this.items[index];
|
|
element.init(this);
|
|
}
|
|
this.Show();
|
|
}
|
|
|
|
bandBtn() {
|
|
GButton.BandClick(
|
|
this._nodeTab.Buy7DayVipBt,
|
|
() => {
|
|
// 购买7天会员
|
|
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
|
|
const memberId = shopData.get7DayMemberId();
|
|
this.reqBuyGood(memberId);
|
|
},
|
|
this
|
|
);
|
|
|
|
GButton.BandClick(
|
|
this._nodeTab.Buy30DayVipBtn,
|
|
() => {
|
|
// 购买30天会员
|
|
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
|
|
const memberId = shopData.get30DayMemberId();
|
|
this.reqBuyGood(memberId);
|
|
},
|
|
this
|
|
);
|
|
GButton.BandClick(
|
|
this._nodeTab.cashBtn,
|
|
() => {
|
|
// 购买7天会员
|
|
this.onClickCashBtn();
|
|
},
|
|
this
|
|
);
|
|
GButton.BandClick(
|
|
this._nodeTab.web3Btn,
|
|
() => {
|
|
// 购买7天会员
|
|
this.onClickWeb3Btn();
|
|
},
|
|
this
|
|
);
|
|
GButton.BandClick(
|
|
this._nodeTab.ReturnBtn,
|
|
() => {
|
|
NavigationManager.Instance.closeSubPanel(this);
|
|
},
|
|
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();
|
|
this.onClickCashBtn();
|
|
}
|
|
}
|
|
|
|
shopData: ShopData;
|
|
updateView() {
|
|
this.getPlayerMerData();
|
|
|
|
this.shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
|
|
// VIP会员
|
|
const memberId7 = this.shopData.get7DayMemberId();
|
|
const price7 = this.shopData.getOriginalPriceById(memberId7);
|
|
this.vipPrice7.string = `$ ${price7}`;
|
|
const memberId30 = this.shopData.get30DayMemberId();
|
|
const price30 = this.shopData.getOriginalPriceById(memberId30);
|
|
this.vipPrice30.string = `$ ${price30}`;
|
|
this.ScrollView.scrollToBottom();
|
|
}
|
|
|
|
// 0:cash 1:web3
|
|
updateShops(type: number = 0) {
|
|
// 充值商品 缺:分开获取现金充值或者web3币充值
|
|
const rechargeIds = this.shopData.getRechargeIds();
|
|
for (let index = 0; index < this.items.length; index++) {
|
|
const element = this.items[index];
|
|
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;
|
|
}
|
|
}
|
|
|
|
purchaseGood(id: number, networkType: proto.cs.EnmNetworkType) {
|
|
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
|
|
const isRechargeId = shopData.isRechargeId(id);
|
|
if (isRechargeId) {
|
|
// 需要外部支付进行购买
|
|
this.startPayment(id, this.currentPayType, networkType);
|
|
}
|
|
//测试用
|
|
// Utils.sendInnerMsg(InnerMsgCode.PayOrderCreateSucc, {
|
|
// orderId: 123,
|
|
// payType: 1,
|
|
// goodId: 1001,
|
|
// });
|
|
}
|
|
|
|
createOrRefreshWeb3PayPanel(param: any) {
|
|
if (param.payType == proto.cs.EnmPayType.EPT_Cash_INR) return;
|
|
let data = {
|
|
orderId: param.orderId,
|
|
payType: param.payType,
|
|
goodId: param.goodId,
|
|
networkType: this.getDefaultNetworkType(),
|
|
basePanel: this.base,
|
|
payPanel: this,
|
|
};
|
|
|
|
if (this.web3PopPanel == null)
|
|
NavigationManager.Instance.openSubPanel("Web3PopPanel", this.base, data);
|
|
else {
|
|
data.networkType = undefined;
|
|
this.web3PopPanel.refreshData(data);
|
|
this.web3PopPanel.refreshView();
|
|
}
|
|
}
|
|
|
|
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);
|
|
if (leftTime == null) {
|
|
LanguageUtils.getText("purchasepanel.notvip");
|
|
} else {
|
|
this.vipLeftTime.string = LanguageUtils.getText(
|
|
"purchasepanel.vip_left"
|
|
).replace("${leftTime}", leftTime);
|
|
}
|
|
}
|
|
|
|
// 使用余额购买商品
|
|
async reqBuyGood(goodId: number) {
|
|
// 请求购买商品
|
|
const reqData = {
|
|
goodId: goodId,
|
|
girlId: 0,
|
|
};
|
|
let res = await ShopService.I.reqBuyGood(reqData);
|
|
logger.log("请求使用余额购买商品的响应数据:", res);
|
|
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);
|
|
// 刷新界面
|
|
|
|
TipsPanel.show(LanguageUtils.getText("payment.succeed"));
|
|
} else {
|
|
if (res && res.code) {
|
|
const key = "error_code_" + res.code;
|
|
TipsPanel.show(LanguageUtils.getText(key));
|
|
}
|
|
}
|
|
}
|
|
|
|
// 创建订单
|
|
async startPayment(goodId: number, payType: number, network: number) {
|
|
// 请求数据
|
|
const reqData = {
|
|
goodId,
|
|
payType,
|
|
network,
|
|
};
|
|
let res = await PaymentService.I.startPayment(reqData);
|
|
logger.log("支付完成的响应数据:", res);
|
|
if (res) {
|
|
let status = res.status;
|
|
if (status === -1) {
|
|
logger.log("支付失败!!!,错误码:", res.retCode);
|
|
|
|
const key = "error_code_" + res.retCode;
|
|
TipsPanel.show(LanguageUtils.getText(key));
|
|
} else if (status === 0) {
|
|
logger.log("支付成功!!!");
|
|
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
|
|
walletData.balance = Number(res.balance);
|
|
walletData.vipExpire = Number(res.vipExpire);
|
|
|
|
TipsPanel.show(LanguageUtils.getText("payment.succeed"));
|
|
}
|
|
}
|
|
}
|
|
|
|
curPayType = -1;
|
|
onClickCashBtn() {
|
|
if (this.curPayType == 0) return;
|
|
this.curPayType = 0;
|
|
this.cashBtnSelecting.active = this.web3BtnNotSelect.active = true;
|
|
this.cashBtnNotSelect.active = this.web3BtnSelecting.active = false;
|
|
this.updateShops(0);
|
|
}
|
|
onClickWeb3Btn() {
|
|
if (this.curPayType == 1) return;
|
|
this.curPayType = 1;
|
|
this.cashBtnSelecting.active = this.web3BtnNotSelect.active = false;
|
|
this.cashBtnNotSelect.active = this.web3BtnSelecting.active = true;
|
|
this.updateShops(1);
|
|
}
|
|
|
|
onDestroy() {
|
|
Utils.removeInnerEL(InnerMsgCode.BalanceChange, this, () => {
|
|
this.onBalanceChange();
|
|
});
|
|
Utils.removeInnerEL(InnerMsgCode.VipExpireChange, this, () => {
|
|
this.onVipExpireChange();
|
|
});
|
|
Utils.removeInnerEL(InnerMsgCode.PayOrderCreateSucc, this, () => {
|
|
this.createOrRefreshWeb3PayPanel;
|
|
});
|
|
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
|
|
this.onVipExpireChange();
|
|
});
|
|
super.onDestroy();
|
|
}
|
|
}
|