384 lines
17 KiB
TypeScript
384 lines
17 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 "./JiaZai";
|
|
import MapConroler from "./Map";
|
|
import Utils from "./module/Pay/Utils";
|
|
import NumberToImage from "./NumberToImage";
|
|
import SceneManager from "./SceneManager";
|
|
import { MiniGameSdk } from "./Sdk/MiniGameSdk";
|
|
|
|
const { ccclass, property } = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class Revive extends cc.Component {
|
|
|
|
|
|
|
|
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;
|
|
// 检测微信小游戏切到后台
|
|
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
|
|
// 定义监听函数
|
|
this.onShowListener = () => {
|
|
this.onShow();
|
|
};
|
|
//@ts-ignore
|
|
wx.onShow(this.onShowListener);
|
|
}
|
|
}
|
|
|
|
onShow() {
|
|
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
|
|
console.log("从后台进入前台订单号:", cc.fx.GameConfig.GM_INFO.iosOutTradeNo);
|
|
if (cc.fx.GameConfig.GM_INFO.iosOutTradeNo != null && cc.fx.GameConfig.GM_INFO.iosOutTradeNo != "") {
|
|
console.log("有苹果订单号,开始轮训");
|
|
const iosOutTradeNo = cc.fx.GameConfig.GM_INFO.iosOutTradeNo;
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = "";
|
|
this.openLoad();
|
|
this.btn_Touch = true;
|
|
Utils.getIosPayInfo(iosOutTradeNo,
|
|
(data) => {
|
|
console.log("获得轮训结果:", data);
|
|
if (data.code == 1) {
|
|
console.log("购买成功");
|
|
const dataSuccess = {
|
|
outTradeNo: iosOutTradeNo,
|
|
price: this.iosPrice,
|
|
payment_name: this.iosProductId,
|
|
payment_num: this.iosCount,
|
|
type: "ios",
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment", dataSuccess);
|
|
let name = "购买金币道具:" + this.iosProductId;
|
|
MiniGameSdk.API.yinli_Pay(this.iosPrice, iosOutTradeNo, name)
|
|
Utils.setPayInfo(
|
|
(res) => {
|
|
this.closeLoad();
|
|
//console.log("设置轮训结果:", res);
|
|
if (res.code === 1) {
|
|
console.log("_________正式发货");
|
|
MiniGameSdk.API.showToast("充值成功");
|
|
cc.fx.GameTool.shopBuy(this.iosProductId, false);
|
|
if (this.iosProductId == "reborn_Gift") {
|
|
this.buyGift();
|
|
}
|
|
//console.log("充值成功获得金币");
|
|
}
|
|
else {
|
|
MiniGameSdk.API.showToast("网络异常,充值奖励将在登录后再次发放");
|
|
const dataFail4 = {
|
|
outTradeNo: iosOutTradeNo,
|
|
price: this.iosPrice,
|
|
payment_name: this.iosProductId,
|
|
payment_num: this.iosCount,
|
|
type: "ios",
|
|
fail_reason: "成功付款,但是发货时请求服务器失败,充值成功未发货",
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment_fail", dataFail4);
|
|
}
|
|
if (this.node.parent.parent.parent.parent.getComponent("SceneManager")) {
|
|
this.node.parent.parent.parent.parent.getComponent("SceneManager").updateCoin();
|
|
}
|
|
}, iosOutTradeNo)
|
|
}
|
|
else if (data.code == 0) {
|
|
console.log("用户自己取消充值");
|
|
MiniGameSdk.API.showToast("充值失败");
|
|
this.closeLoad();
|
|
this.btn_Touch = true;
|
|
const dataFail = {
|
|
outTradeNo: iosOutTradeNo,
|
|
price: this.iosPrice,
|
|
payment_name: this.iosProductId,
|
|
payment_num: this.iosCount,
|
|
type: "ios",
|
|
fail_reason: "用户取消充值",
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment_fail", dataFail);
|
|
}
|
|
else if (data.code == 2) {
|
|
this.closeLoad();
|
|
console.log("轮训超时");
|
|
MiniGameSdk.API.showToast("订单已关闭");
|
|
const dataFail = {
|
|
outTradeNo: iosOutTradeNo,
|
|
price: this.iosPrice,
|
|
payment_name: this.iosProductId,
|
|
payment_num: this.iosCount,
|
|
type: "ios",
|
|
fail_reason: "用户充值后,轮训结果超时",
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment_fail", dataFail);
|
|
}
|
|
|
|
this.btn_Touch = true;
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = null;
|
|
})
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
start() {
|
|
if (cc.fx.GameConfig.GM_INFO.revive == 1) {
|
|
this.node.active = false;
|
|
}
|
|
}
|
|
init() {
|
|
this.btn_Touch = true;
|
|
|
|
}
|
|
|
|
//购买
|
|
buyGift() {
|
|
Utils.rebornGift(0, (data) => {
|
|
console.log("购买'✅ ", data.code);
|
|
if (data.code == 1) {
|
|
MapConroler._instance.setPropNum();
|
|
const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
|
|
const jiazaiComp = jiazaiNode.getComponent(JiaZai);
|
|
if (jiazaiComp) {
|
|
setTimeout(() => {
|
|
jiazaiComp.updateCoin();
|
|
}, 300);
|
|
} else {
|
|
console.log("无法获取JiaZai组件");
|
|
}
|
|
let shop = cc.find("Canvas/shop");
|
|
if (shop) {
|
|
let shopComp = shop.getComponent("shop");
|
|
if (shopComp) {
|
|
shopComp.openShop();
|
|
}
|
|
}
|
|
}
|
|
})
|
|
// update (dt) {}
|
|
}
|
|
|
|
buyProduct(customData) {
|
|
// cc.fx.GameTool.shopBuy("revive", false);
|
|
// MapConroler._instance.setPropNum();
|
|
if (!this.btn_Touch) {
|
|
return;
|
|
}
|
|
this.btn_Touch = false;
|
|
const productId = customData;
|
|
let id = "10011";
|
|
let price = 100;
|
|
let count = 1;
|
|
id = productId;
|
|
switch (productId) {
|
|
case "reborn_Gift":
|
|
price = 600;
|
|
break;
|
|
}
|
|
console.log("获得商品id:", id, count, price);
|
|
// 判断设备系统
|
|
let systemType = "Android";
|
|
try {
|
|
//@ts-ignore
|
|
const systemInfo = wx.getSystemInfoSync();
|
|
if (systemInfo.platform === 'ios') {
|
|
systemType = "ios";
|
|
}
|
|
} catch (e) {
|
|
console.error('获取系统信息失败', e);
|
|
}
|
|
|
|
// Utils.GoKEFu();
|
|
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) => {
|
|
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,
|
|
price: 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,
|
|
price: 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,
|
|
price: 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,
|
|
price: price,
|
|
payment_name: productId,
|
|
payment_num: 1,
|
|
type: systemType,
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment", dataSuccess);
|
|
let name = "购买金币道具:" + productId;
|
|
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 == "reborn_Gift") {
|
|
this.buyGift();
|
|
}
|
|
//console.log("充值成功获得金币");
|
|
}
|
|
else {
|
|
MiniGameSdk.API.showToast("网络异常,充值奖励将在登录后再次发放");
|
|
const dataFail4 = {
|
|
outTradeNo: Utils.outTradeNo,
|
|
price: price,
|
|
payment_name: productId,
|
|
payment_num: 1,
|
|
type: systemType,
|
|
fail_reason: "成功付款,但是发货时请求服务器失败,充值成功未发货",
|
|
}
|
|
cc.fx.GameTool.shushu_Track("payment_fail", dataFail4);
|
|
}
|
|
|
|
if (this.node.parent.parent.parent.parent.getComponent("SceneManager")) {
|
|
this.node.parent.parent.parent.parent.getComponent("SceneManager").updateCoin();
|
|
}
|
|
}, Utils.outTradeNo)
|
|
}
|
|
else {
|
|
|
|
const dataFail3 = {
|
|
outTradeNo: Utils.outTradeNo,
|
|
price: 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.parent.parent.parent.getComponent("SceneManager")) {
|
|
this.node.parent.parent.parent.parent.getComponent("SceneManager").updateCoin();
|
|
}
|
|
}
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
openLoad() {
|
|
this.node.parent.parent.parent.parent.getChildByName("Loading").active = true;
|
|
this.node.parent.parent.parent.parent.getChildByName("Loading").getChildByName("load").stopAllActions();
|
|
this.node.parent.parent.parent.parent.getChildByName("Loading").getChildByName("load").runAction(cc.rotateTo(2, 1080).repeatForever());
|
|
}
|
|
|
|
closeLoad() {
|
|
this.node.parent.parent.parent.parent.getChildByName("Loading").active = false;
|
|
}
|
|
|
|
|
|
offShow() {
|
|
if (cc.fx.GameConfig.GM_INFO.revive == 0) {
|
|
return;
|
|
}
|
|
|
|
// 移除 wx.onShow 监听器
|
|
if (cc.sys.platform === cc.sys.WECHAT_GAME && this.onShowListener) {
|
|
//@ts-ignore
|
|
wx.offShow(this.onShowListener);
|
|
console.log("🔥🔥🔥🔥🔥🔥🔥🔥")
|
|
}
|
|
|
|
}
|
|
|
|
|
|
onDestroy() {
|
|
}
|
|
}
|