cb/assets/action_bundle/script/NewbieGift.ts
2025-11-04 10:23:00 +08:00

532 lines
22 KiB
TypeScript

// 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 "../../Script/JiaZai";
import Utils from "../../Script/module/Pay/Utils";
import NumberToImage from "../../Script/NumberToImage";
import { MiniGameSdk } from "../../Script/Sdk/MiniGameSdk";
// import JiaZai from "./JiaZai";
// import Utils from "./module/Pay/Utils";
// import NumberToImage from "./NumberToImage";
// import { MiniGameSdk } from "./Sdk/MiniGameSdk";
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewbieGift extends cc.Component {
@property(cc.Node)
monthCardTime: cc.Node = null;
//月卡按钮切换
@property(cc.Node)
monthCardBtn: cc.Node = null;
//月卡按钮
@property(cc.Node)
monthCardBtn2: cc.Node = null;
public juwai = false;
btn_Touch: boolean = false;
// LIFE-CYCLE CALLBACKS:
private onShowListener: () => void;
private iosPrice: number = 0;
private iosProductId: string = "";
private iosCount: number = 1;
public home: number = 0;
onLoad() {
this.btn_Touch = true;
// 检测微信小游戏切到后台
this.home = 0;
}
againGet() {
this.closeConfirmBox();
this.onShow();
}
onShow() {
if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
console.log("从后台进入前台订单号:", cc.fx.GameConfig.GM_INFO.iosStarterOrder);
if (cc.fx.GameConfig.GM_INFO.iosStarterOrder != null && cc.fx.GameConfig.GM_INFO.iosStarterOrder != "") {
console.log("有苹果订单号,开始轮训");
const iosStarterOrder = cc.fx.GameConfig.GM_INFO.iosStarterOrder;
this.openLoad();
this.btn_Touch = true;
Utils.getIosPayInfo(iosStarterOrder,
(data) => {
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, iosStarterOrder, name);
MiniGameSdk.API.yinli_Pay(iosAmount, iosStarterOrder, name)
this.closeLoad();
this.btn_Touch = true;
console.log("_________正式发货");
MiniGameSdk.API.showToast("充值成功");
cc.fx.GameTool.shopBuy(iosID, false);
this.buyStarter_pack(iosID);
cc.fx.GameConfig.GM_INFO.iosStarterOrder = null;
//console.log("充值成功获得金币");
}
else if (data.code == 0) {
console.log("用户自己取消充值");
MiniGameSdk.API.showToast("充值失败");
this.closeLoad();
this.btn_Touch = true;
const dataFail = {
outTradeNo: iosStarterOrder,
pay_amount: iosAmount,
payment_name: iosID,
payment_num: this.iosCount,
type: "ios",
fail_reason: "用户取消充值",
}
cc.fx.GameTool.shushu_Track("payment_fail", dataFail);
cc.fx.GameConfig.GM_INFO.iosStarterOrder = null;
}
else if (data.code == 2) {
this.closeLoad();
this.btn_Touch = true;
console.log("轮训超时");
MiniGameSdk.API.showToast("请检查网络,如充值成功,请重新登录领取", 4);
const dataFail = {
outTradeNo: iosStarterOrder,
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;
if (this.node.parent.getComponent("JiaZai"))
this.node.parent.getComponent("JiaZai").updateCoin();
})
}
}
}
start() {
// this.init();
}
init(first) {
if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform === cc.sys.BYTEDANCE_GAME) {
// 定义监听函数
this.onShowListener = () => {
console.log("微信小游戏切到前台,monthCard");
this.onShow();
};
//@ts-ignore
wx.onShow(this.onShowListener);
}
let nowTime = Math.floor(Date.now() / 1000);
let time = cc.fx.GameConfig.GM_INFO.starter_packTime - nowTime;
var timeTemp = cc.fx.GameTool.getTimeMargin2(time);
this.node.getChildByName("time").active = true;
this.node.getChildByName("time").getComponent(cc.Label).
string = timeTemp;
this.node.getChildByName("propBg1").opacity = 0;
this.node.getChildByName("propBg2").opacity = 0;
this.node.getChildByName("propBg3").opacity = 0;
this.node.getChildByName("gold_2").active = false;
this.node.getChildByName("baoxiang").active = false;
if (first) {
this.node.opacity = 255;
this.node.scale = 1;
this.node.getChildByName("ditu").active = true;
this.node.getChildByName("close").active = true;
this.btn_Touch = true;
this.setAction();
}
else {
cc.tween(this.node)
.to(0.2, { opacity: 255, scale: 1, position: cc.v3(0, 0, 0) })
.call(() => {
this.node.getChildByName("ditu").active = true;
this.node.getChildByName("close").active = true;
this.btn_Touch = true;
this.setAction();
})
.start();
}
}
//入场动画
setAction() {
this.node.getChildByName("gold_1").getComponent(sp.Skeleton).setAnimation(0, "animation", false);
setTimeout(() => {
// this.node.getChildByName("gold_2").active = true;
this.node.getChildByName("qipao3").active = true;
this.node.getChildByName("qipao3").getComponent(sp.Skeleton).setAnimation(0, "animation", false);
cc.tween(this.node.getChildByName("propBg1"))
.delay(0.1)
.to(0.2, { opacity: 255 })
.start();
}, 400);
setTimeout(() => {
this.node.getChildByName("qipao2").active = true;
this.node.getChildByName("qipao2").getComponent(sp.Skeleton).setAnimation(0, "animation", false);
cc.tween(this.node.getChildByName("propBg2"))
.delay(0.1)
.to(0.2, { opacity: 255 })
.start();
}, 600);
setTimeout(() => {
this.node.getChildByName("gold_2").active = true;
this.node.getChildByName("qipao1").active = true;
this.node.getChildByName("qipao1").getComponent(sp.Skeleton).setAnimation(0, "animation", false);
cc.tween(this.node.getChildByName("propBg3"))
.delay(0.1)
.to(0.2, { opacity: 255 })
.start();
}, 800);
setTimeout(() => {
// this.node.getChildByName("gold_2").active = true;
this.node.getChildByName("baoxiang").active = true;
}, 1000);
}
//购买月卡
buyStarter_pack(id) {
console.log("购买新手礼包成功!");
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
if (jiazaiComp) {
jiazaiComp.Stamina.parent.getChildByName("xinshou").active = false;
jiazaiComp.updateCoin();
}
this.closeStarter_pack();
// Utils.setMonthlyCard(0, (data) => {
// console.log("购买月卡", data.code);
// if (data.code == 1) {
// this.setReward(id);
// // let rewardData = [
// // { type: "coin", count: 6000 },
// // ]
// }
// })
// update (dt) {}
}
setReward(id) {
cc.fx.GameConfig.GM_INFO.iosMonthOrder = null;
this.closeLoad();
this.btn_Touch = true;
cc.fx.GameTool.shopBuy(id, false);
cc.fx.GameConfig.GM_INFO.hp_Max = 7;
cc.fx.GameConfig.GM_INFO.hp = 7;
cc.fx.GameConfig.GM_INFO.doubleCoin = 2;
this.home = 1;
cc.fx.GameTool.getMonthlyCardValidityDays().then(days => {
cc.fx.GameConfig.GM_INFO.monthTime = days.days;
//本地储存当前服务器时间
let dateStr = new Date(days.time);
cc.fx.StorageMessage.setStorage("mCardDate", dateStr);
NumberToImage.numberToImageNodes(days.days, 35, 20, "month_", this.monthCardTime, true);
});
// cc.fx.GameTool.changeCoin(6000);
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
if (jiazaiComp) {
console.log("获取到JiaZai组件", jiazaiComp);
setTimeout(() => {
jiazaiComp.setHealthInfo(false);
jiazaiComp.startTimeCutDown();
jiazaiComp.updateCoin();
}, 300);
} else {
console.log("无法获取JiaZai组件");
}
this.monthCardBtn.active = false;
this.monthCardBtn2.active = true;
this.monthCardTime.active = true;
cc.fx.GameTool.setUserHealth(0, (data) => {
cc.fx.GameTool.getHealth(null);
})
let shop = cc.find("Canvas/shop");
if (shop) {
let shopComp = shop.getComponent("shop");
if (shopComp) {
shopComp.openShop();
}
}
}
closeStarter_pack() {
// this.init();
// 移除 wx.onShow 监听器
this.node.getChildByName("ditu").active = false;
this.node.getChildByName("close").active = false;
this.node.getChildByName("gold_2").active = false;
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);
}
let posX = 0;
let posY = 0;
//jiazai
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
if (jiazaiComp) {
let top = jiazaiComp.Stamina.parent;
posX = top.x + top.getChildByName("xinshou").x;
posY = top.y + top.getChildByName("xinshou").y;
}
// let isExpired = cc.fx.GameTool.checkExpiration();
// if (jiazaiComp && this.home == 1 && isExpired) {
// this.home = 0;
// jiazaiComp.rewarded();
// console.log("123iiiii222")
// }
// if (jiazaiComp) {
// console.log("22222222")
// jiazaiComp.onGames();
// }
cc.tween(this.node)
.to(0.2, { opacity: 0, scale: 0.1, position: cc.v3(posX, posY, 0) })
.call(() => {
this.node.active = false;
})
.start();
}
buyProduct(event, customData) {
if (!this.btn_Touch) {
return;
}
this.btn_Touch = false;
const productId = customData;
let id = "starter_pack";
let price = 100;
let count = 1;
id = productId;
switch (productId) {
case "starter_pack":
price = 300;
break;
}
console.log("获得商品id:", id, count, price);
// 判断设备系统
let systemType = "Android";
try {
//@ts-ignore
let systemInfo = null;
if (cc.sys.platform === cc.sys.WECHAT_GAME)
//@ts-ignore
systemInfo = wx.getSystemInfoSync();
else if (cc.sys.platform === cc.sys.BYTEDANCE_GAME)
//@ts-ignore
systemInfo = tt.getSystemInfoSync();
if (systemInfo.platform === 'ios') {
systemType = "ios";
}
} catch (e) {
console.error('获取系统信息失败', e);
}
if (systemType == "ios") {
// MiniGameSdk.API.showToast("IOS系统暂不支持支付");
// this.btn_Touch = true;
this.openLoad();
this.btn_Touch = true;
let iosPayInfo = {
price: price,
payment_name: productId,
payment_count: 1,
}
this.iosPrice = price;
this.iosProductId = productId;
this.iosCount = 1;
Utils.GoKEFu(iosPayInfo, (res) => {
if (res == "success") {
console.log("客服回话成功");
}
else {
console.log("客服回话失败");
this.closeLoad();
}
});
}
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();
this.btn_Touch = true;
//console.log("7.14_____________________", "调用充值接口");
Utils.buyProp(id, count, price, systemType, productId, (res) => {
//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) {
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 {
Utils.getPayInfo((data) => {
//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: Utils.outTradeNo,
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) {
this.btn_Touch = true;
// 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, Utils.outTradeNo, name);
MiniGameSdk.API.yinli_Pay(price, Utils.outTradeNo, name)
console.log("7.14_______________充值成功,轮训成功,准备发货");
Utils.setPayInfo(
(res) => {
console.log("设置轮训结果:", res);
if (res.code === 1) {
console.log("7.14_________正式发货");
MiniGameSdk.API.showToast("充值成功");
cc.fx.GameTool.shopBuy(productId, false);
if (productId == "starter_pack") {
this.buyStarter_pack(productId);
}
//console.log("充值成功获得金币");
}
else {
MiniGameSdk.API.showToast("网络异常,充值奖励将在登录后再次发放");
const dataFail4 = {
outTradeNo: Utils.outTradeNo,
pay_amount: price,
payment_name: productId,
payment_num: 1,
type: systemType,
fail_reason: "成功付款,但是发货时请求服务器失败,充值成功未发货",
}
cc.fx.GameTool.shushu_Track("payment_fail", dataFail4);
}
if (this.node.parent.getComponent("JiaZai"))
this.node.parent.getComponent("JiaZai").updateCoin();
}, Utils.outTradeNo)
}
else {
const dataFail3 = {
outTradeNo: Utils.outTradeNo,
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;
if (this.node.parent.getComponent("JiaZai"))
this.node.parent.getComponent("JiaZai").updateCoin();
}
})
}
});
}
}
openLoad() {
this.node.getChildByName("Loading").active = true;
this.node.getChildByName("Loading").getChildByName("load").stopAllActions();
this.node.getChildByName("Loading").getChildByName("load").runAction(cc.rotateTo(2, 1080).repeatForever());
}
openConfirmBox() {
this.closeLoad();
this.node.getChildByName("ConfirmBox").active = true;
}
closeConfirmBox() {
this.node.getChildByName("ConfirmBox").active = false;
}
closeLoad() {
this.node.getChildByName("Loading").active = false;
}
onDestroy() {
}
update() {
}
}