feat: 新手礼包
This commit is contained in:
+83
-206
@@ -12,6 +12,7 @@ import { MiniGameSdk } from "./Sdk/MiniGameSdk";
|
||||
import { LQCollideSystem } from "./lq_collide_system/lq_collide_system";
|
||||
import AudioManager from "./module/Music/AudioManager";
|
||||
import Utils from "./module/Pay/Utils";
|
||||
import { applyStarterPackStatus, starterPackRemaining, starterPackTimeText, starterPackDay, starterPackPopupKey, starterPackReward } from "./module/Config/StarterPack";
|
||||
import { initProvinceLocator } from "./module/Position/GetPosition";
|
||||
import LoadingCatAnimation from "./LoadingCatAnimation";
|
||||
import BlockAssetManager from "./module/Tool/BlockAssetManager";
|
||||
@@ -78,6 +79,8 @@ export default class JiaZai extends cc.Component {
|
||||
private jungleSettlementRequestTimeout: any = null;
|
||||
private pendingJungleSettlement: any = null;
|
||||
private starterPackDefaultPosition: cc.Vec2 = null;
|
||||
private starterPackOpening = false;
|
||||
private starterPackChecking = false;
|
||||
|
||||
// 缓存 reward 预制体
|
||||
private static cachedRewardPrefab: cc.Prefab | null = null;
|
||||
@@ -413,7 +416,7 @@ export default class JiaZai extends cc.Component {
|
||||
this.checkShare();
|
||||
this.queueHomeAutomaticTask("首页头像", () => this.closeAvatar());
|
||||
this.setShareInfo();
|
||||
this.checkTasks();
|
||||
// this.checkTasks();//TODO:恢复
|
||||
if (!cc.fx.GameTool.maxLevel()) {
|
||||
this.specialLevelShow();
|
||||
}
|
||||
@@ -2621,10 +2624,12 @@ export default class JiaZai extends cc.Component {
|
||||
coinTemp = 1000;
|
||||
}
|
||||
else if (order.itemid == "starter_pack") {
|
||||
coinTemp = 3000;
|
||||
cc.fx.GameConfig.GM_INFO.doubleCoin = 5;
|
||||
cc.fx.GameConfig.GM_INFO.hp_Max = 5;
|
||||
cc.fx.GameConfig.GM_INFO.hp = 5;
|
||||
coinTemp = starterPackReward().coin;
|
||||
cc.fx.GameConfig.GM_INFO.starterPackPurchased = true;
|
||||
if (canUpdateHome()) {
|
||||
this.newbieGift = false;
|
||||
this.startStarter_pack();
|
||||
}
|
||||
} else if (order.itemid == "battlepass") {
|
||||
// 通行证订单
|
||||
this.reissuePaaCheck();
|
||||
@@ -2632,7 +2637,7 @@ export default class JiaZai extends cc.Component {
|
||||
let title = "充值补发奖励金币:" + coinTemp;
|
||||
// MiniGameSdk.API.showToast(title);
|
||||
if (order.itemid != "month_Card")
|
||||
cc.fx.GameTool.shopBuy(productId, true);
|
||||
cc.fx.GameTool.shopBuy(productId, true, productId === "starter_pack" ? { outTradeNo: order.outTradeNo } : null);
|
||||
// console.log("充值成功获得金币");
|
||||
let price = parseInt(order.goodsPrice) || 0;
|
||||
// const dataSuccess = {
|
||||
@@ -2817,79 +2822,47 @@ export default class JiaZai extends cc.Component {
|
||||
// ============================================
|
||||
// 打开通行证弹窗
|
||||
openStarter_pack(event?: cc.Event, customEventData?: string) {
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()) return;
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()
|
||||
|| starterPackRemaining() <= 0 || this.starterPackOpening) return;
|
||||
this.vibrateButtonClick(event);
|
||||
|
||||
if (!JiaZai.cachedActionPrefab) {
|
||||
let eventName = "";
|
||||
let btnName = ""
|
||||
if (customEventData !== undefined) {
|
||||
btnName = customEventData;
|
||||
eventName = "click"
|
||||
} else {
|
||||
btnName = "xinshouBtn";
|
||||
eventName = "auto";
|
||||
const show = () => {
|
||||
this.starterPackOpening = false;
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()
|
||||
|| starterPackRemaining() <= 0) return;
|
||||
const first = !this.actionpNode;
|
||||
if (first) {
|
||||
this.actionpNode = cc.instantiate(JiaZai.cachedActionPrefab);
|
||||
this.node.addChild(this.actionpNode);
|
||||
this.actionpNode.zIndex = 2;
|
||||
}
|
||||
let dataInfo = {
|
||||
interface_id: "NewbieGift", // 界面名称
|
||||
reason: eventName, // 事件名称
|
||||
is_first_exposure: false, // 是否首次曝光
|
||||
entry_point: btnName, // 按钮名称
|
||||
}
|
||||
cc.fx.GameTool.shushu_Track("interface_exposure", dataInfo);
|
||||
|
||||
// cc.error('Action prefab is not loaded yet.');
|
||||
this.loadHomeBundleWithDependencies("action_bundle", 5, (err: Error, bundle: cc.AssetManager.Bundle) => {
|
||||
if (err) {
|
||||
cc.error(err.message || err);
|
||||
return;
|
||||
}
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()) return;
|
||||
bundle.load('prefab/newbieGift', cc.Prefab, (err: Error, prefab: cc.Prefab) => {
|
||||
if (err) {
|
||||
cc.error(err.message || err);
|
||||
return;
|
||||
}
|
||||
JiaZai.cachedActionPrefab = prefab;
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()) return;
|
||||
if (!this.actionpNode) {
|
||||
// 第一次使用,创建节点
|
||||
this.actionpNode = cc.instantiate(JiaZai.cachedActionPrefab);
|
||||
if (this.node && this.actionpNode)
|
||||
this.node.addChild(this.actionpNode);
|
||||
this.actionpNode.zIndex = 2;
|
||||
this.actionpNode.getComponent("NewbieGift").init(true);
|
||||
this.actionpNode.getChildByName("time").active = false;
|
||||
} else {
|
||||
// 非第一次使用,直接激活节点
|
||||
this.actionpNode.active = true;
|
||||
this.actionpNode.getComponent("NewbieGift").init(false);
|
||||
this.actionpNode.getChildByName("time").active = false;
|
||||
}
|
||||
});
|
||||
this.actionpNode.active = true;
|
||||
this.actionpNode.getComponent("NewbieGift").init(first);
|
||||
// Count only a displayed popup, including the initial activation and
|
||||
// manual opens. Failed loads must not consume today's reminder.
|
||||
cc.fx.StorageMessage.setStorage(starterPackPopupKey(), starterPackDay());
|
||||
this.newbieGift = false;
|
||||
cc.fx.GameTool.shushu_Track("interface_exposure", {
|
||||
interface_id: "NewbieGift", reason: event ? "click" : "auto",
|
||||
is_first_exposure: first, entry_point: customEventData || "xinshouBtn",
|
||||
});
|
||||
};
|
||||
if (JiaZai.cachedActionPrefab) {
|
||||
show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.actionpNode) {
|
||||
// 第一次使用,创建节点
|
||||
this.actionpNode = cc.instantiate(JiaZai.cachedActionPrefab);
|
||||
if (this.node && this.actionpNode)
|
||||
this.node.addChild(this.actionpNode);
|
||||
this.actionpNode.zIndex = 2;
|
||||
this.actionpNode.getComponent("NewbieGift").init(true);
|
||||
} else {
|
||||
// 非第一次使用,直接激活节点
|
||||
this.actionpNode.active = true;
|
||||
this.actionpNode.getComponent("NewbieGift").init(false);
|
||||
}
|
||||
//@ts-ignore
|
||||
if (typeof wx !== 'undefined') {
|
||||
//@ts-ignore
|
||||
wx.offShow(this.onShowListener);
|
||||
//@ts-ignore
|
||||
wx.offHide(this.onHideListener);
|
||||
}
|
||||
this.starterPackOpening = true;
|
||||
this.loadHomeBundleWithDependencies("action_bundle", 5, (err: Error, bundle: cc.AssetManager.Bundle) => {
|
||||
if (err) { this.starterPackOpening = false; cc.error(err); return; }
|
||||
if (!this.isHomeRuntimeAlive() || cc.fx.GameConfig.isGameplayEntryPreparing()) {
|
||||
this.starterPackOpening = false;
|
||||
return;
|
||||
}
|
||||
bundle.load('prefab/newbieGift', cc.Prefab, (loadError: Error, prefab: cc.Prefab) => {
|
||||
if (loadError) { this.starterPackOpening = false; cc.error(loadError); return; }
|
||||
JiaZai.cachedActionPrefab = prefab;
|
||||
show();
|
||||
});
|
||||
});
|
||||
}
|
||||
// ============================================
|
||||
// 关闭通行证方法
|
||||
@@ -3777,44 +3750,24 @@ export default class JiaZai extends cc.Component {
|
||||
|
||||
startStarter_pack() {
|
||||
this.stopStarter_pack();
|
||||
if (this.scheduleCallback3) {
|
||||
this.unschedule(this.scheduleCallback3);
|
||||
}
|
||||
this.scheduleCallback3 = function () {
|
||||
let nowTime = Math.floor(Date.now() / 1000);
|
||||
if (cc.fx.GameConfig.GM_INFO.starter_packTime < nowTime) {
|
||||
this.stopStarter_pack();
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
return;
|
||||
this.scheduleCallback3 = () => {
|
||||
const time = starterPackRemaining();
|
||||
const button = this.getStarterPackHomeButton();
|
||||
if (button) {
|
||||
const label = button.getChildByName("time").getComponent(cc.Label);
|
||||
label.string = time > 0 ? starterPackTimeText(time) : "已结束";
|
||||
}
|
||||
if (this.actionpNode && cc.isValid(this.actionpNode)) {
|
||||
this.actionpNode.getComponent("NewbieGift").refreshCountdown();
|
||||
}
|
||||
|
||||
let time = cc.fx.GameConfig.GM_INFO.starter_packTime - nowTime;
|
||||
if (time <= 0) {
|
||||
time = 0;
|
||||
this.stopStarter_pack();
|
||||
this.newbieGift = false;
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
this.stopStarter_pack();
|
||||
}
|
||||
else {
|
||||
var timeTemp = cc.fx.GameTool.getTimeMargin2(time);
|
||||
// 计算小时数
|
||||
let hours = Math.floor(Math.floor(time) / 3600);
|
||||
if (hours >= 24) {
|
||||
let days = Math.floor(hours / 24);
|
||||
let remainingHours = hours % 24;
|
||||
timeTemp = days + "天" + remainingHours + "小时";
|
||||
}
|
||||
this.Stamina.parent.getChildByName("xinshou").getChildByName("time").getComponent(cc.Label)
|
||||
.string = timeTemp;
|
||||
if (this.actionpNode) {
|
||||
this.actionpNode.getChildByName("time").getComponent(cc.Label).
|
||||
string = timeTemp;
|
||||
|
||||
|
||||
}
|
||||
// 同步显示
|
||||
}
|
||||
}.bind(this);
|
||||
};
|
||||
this.schedule(this.scheduleCallback3, 1);
|
||||
this.scheduleCallback3();
|
||||
}
|
||||
// ============================================
|
||||
// 停止通行证倒计时方法
|
||||
@@ -4033,108 +3986,32 @@ export default class JiaZai extends cc.Component {
|
||||
// 主要功能:使用缓存控制每天只弹一次
|
||||
// ============================================
|
||||
checkStarter_pack() {
|
||||
if (this.deferHomePopupWhileTransfer("starterPack", () => this.checkStarter_pack())) {
|
||||
return;
|
||||
}
|
||||
// return;
|
||||
//如果没有充值新手礼包,每天弹一次新手礼包
|
||||
if (cc.fx.GameConfig.GM_INFO.level < 15 || !cc.fx.GameConfig.GM_INFO.canIos) {
|
||||
return;
|
||||
}
|
||||
const timestamp = Date.now();
|
||||
Utils.getStarter_pack((data) => {
|
||||
if (data.code == 1) {
|
||||
if (data.data.starter_pack == 0) {
|
||||
// console.log("用户没有触发新手礼包,准备触发,并且给弹窗");
|
||||
this.winStreakShow = false;
|
||||
Utils.setStarter_pack((res) => {
|
||||
if (res.code == 1) {
|
||||
this.newbieGift = true;
|
||||
// let top = this.node.getChildByName("Load").getChildByName("Top");
|
||||
// top.getChildByName("yicon").active = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(true);
|
||||
cc.fx.GameConfig.GM_INFO.starter_packTime = res.data.starter_pack / 1000;
|
||||
if (!this.actionpNode) this.openStarter_pack();
|
||||
// console.log("打开新手礼包");
|
||||
const nowTime = Math.floor(Date.now() / 1000);
|
||||
let time = cc.fx.GameConfig.GM_INFO.starter_packTime - nowTime;
|
||||
if (time > 0) {
|
||||
this.startStarter_pack();
|
||||
time = cc.fx.GameTool.getTimeMargin2(time);
|
||||
if (this.Stamina)
|
||||
this.Stamina.parent.getChildByName("xinshou").getChildByName("time").getComponent(cc.Label)
|
||||
.string = time + "";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
else if (data.data.starter_pack < timestamp) {
|
||||
this.winStreakShow = true;
|
||||
// console.log("用户新手礼包已经过期,不触发");
|
||||
this.newbieGift = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
}
|
||||
else if (data.data.starter_packState == 1) {
|
||||
this.winStreakShow = true;
|
||||
// console.log("用户已经购买过新手礼包,不触发");
|
||||
this.newbieGift = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
}
|
||||
else {
|
||||
// console.log("用户在新手礼包时间范围内,给弹窗");
|
||||
cc.fx.GameConfig.GM_INFO.starter_packTime = data.data.starter_pack / 1000;
|
||||
const nowTime = Math.floor(Date.now() / 1000);
|
||||
let time = cc.fx.GameConfig.GM_INFO.starter_packTime - nowTime;
|
||||
if (time > 0) {
|
||||
this.startStarter_pack();
|
||||
time = cc.fx.GameTool.getTimeMargin2(time);
|
||||
if (this.Stamina)
|
||||
this.Stamina.parent.getChildByName("xinshou").getChildByName("time").getComponent(cc.Label)
|
||||
.string = time + "";
|
||||
}
|
||||
let date = new Date();
|
||||
let day = date.getDate() + 1; // 当前日期(1-31)
|
||||
let month = date.getMonth(); // 当前月份(1-12)
|
||||
let year = date.getFullYear(); // 当前年份
|
||||
let dateStr = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`; // 格式化为 YYYY-MM-DD
|
||||
// 从缓存中获取上次弹窗的日期
|
||||
let cachedDateStr = cc.fx.StorageMessage.getStorage("Starter_pack");
|
||||
// 如果缓存中的日期和当前日期不一致
|
||||
if (cachedDateStr !== dateStr) {
|
||||
// 更新缓存
|
||||
this.newbieGift = true;
|
||||
// let top = this.node.getChildByName("Load").getChildByName("Top");
|
||||
// top.getChildByName("yicon").active = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(true);
|
||||
cc.fx.StorageMessage.setStorage("Starter_pack", dateStr);
|
||||
if (!this.actionpNode) this.openStarter_pack();
|
||||
// console.log("打开新手礼包");
|
||||
this.winStreakShow = false;
|
||||
}
|
||||
else {
|
||||
this.winStreakShow = true;
|
||||
this.newbieGift = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(true);
|
||||
// console.log("当天已经给过,不再弹窗新手礼包");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this.deferHomePopupWhileTransfer("starterPack", () => this.checkStarter_pack())) return;
|
||||
if (cc.fx.GameConfig.GM_INFO.level < 15 || !cc.fx.GameConfig.GM_INFO.canIos || this.starterPackChecking) return;
|
||||
this.starterPackChecking = true;
|
||||
const apply = (res) => {
|
||||
this.starterPackChecking = false;
|
||||
if (!this.isHomeRuntimeAlive()) return;
|
||||
if (!res || res.code !== 1) {
|
||||
this.newbieGift = false;
|
||||
if (this.Stamina)
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
this.setStarterPackHomeButtonVisible(false);
|
||||
return;
|
||||
}
|
||||
})
|
||||
applyStarterPackStatus(res.data);
|
||||
const available = starterPackRemaining() > 0;
|
||||
this.setStarterPackHomeButtonVisible(available);
|
||||
this.startStarter_pack();
|
||||
this.newbieGift = available && cc.fx.StorageMessage.getStorage(starterPackPopupKey()) !== starterPackDay();
|
||||
this.winStreakShow = !this.newbieGift;
|
||||
if (this.newbieGift) this.openStarter_pack();
|
||||
};
|
||||
Utils.getStarter_pack(res => {
|
||||
if (res && res.code === 1 && Number(res.data.starter_pack) === 0 && Number(res.data.starter_packState) !== 1) {
|
||||
Utils.setStarter_pack(apply);
|
||||
} else apply(res);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// ============================================
|
||||
// 获取分享信息方法
|
||||
// 功能:从微信启动参数中获取分享人的关卡和UID
|
||||
@@ -5973,7 +5850,7 @@ export default class JiaZai extends cc.Component {
|
||||
// 主要功能:检测新手礼包弹窗是否显示,隐藏月卡弹窗
|
||||
// ============================================
|
||||
update(dt) {
|
||||
if (this.newbieGift && this.monthlyCardNode) {
|
||||
if (this.monthlyCardNode) {
|
||||
if (this.actionpNode) {
|
||||
if (this.monthlyCardNode.active == true && this.actionpNode.active == true) {
|
||||
this.monthlyCardNode.active = false;
|
||||
|
||||
@@ -474,7 +474,9 @@ export class GameConfig {
|
||||
iosReviveOrder: string; //ios订单号复活
|
||||
iosStarterOrder: string; //ios订单号新手
|
||||
iosPassCheckOrder: string; // ios通行证订单
|
||||
starter_packTime: number; //新手礼包活动时间
|
||||
starter_packTime: number; // 新手礼包截止时间,秒
|
||||
starterPackServerOffset: number; // 服务端时间与本地时间差,毫秒
|
||||
starterPackPurchased: boolean;
|
||||
wxFriend: boolean; //微信好友信息授权
|
||||
wxUserInfo: boolean; //微信用户信息授权
|
||||
winState: boolean; //是否延续连胜状态
|
||||
@@ -685,7 +687,9 @@ export class GameConfig {
|
||||
iosReviveOrder: "", //ios订单号复活
|
||||
iosStarterOrder: "", //ios订单号新手
|
||||
iosPassCheckOrder: "", // ios通行证订单
|
||||
starter_packTime: 0, //新手礼包活动时间
|
||||
starter_packTime: 0, // 新手礼包截止时间
|
||||
starterPackServerOffset: 0,
|
||||
starterPackPurchased: false,
|
||||
wxFriend: false, //微信好友信息授权
|
||||
wxUserInfo: false, //微信用户信息授权
|
||||
winState: false, //是否延续连胜状态
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/** Purchases and login compensation use the same client-owned reward. */
|
||||
export function starterPackReward() {
|
||||
return { coin: 8000, hammer: 8, freeze: 8, magic_wand: 8 };
|
||||
}
|
||||
|
||||
export function starterPackNow(): number {
|
||||
return Date.now() + (cc.fx.GameConfig.GM_INFO.starterPackServerOffset || 0);
|
||||
}
|
||||
|
||||
export function starterPackRemaining(): number {
|
||||
if (cc.fx.GameConfig.GM_INFO.starterPackPurchased) return 0;
|
||||
return Math.max(0, Math.ceil(cc.fx.GameConfig.GM_INFO.starter_packTime - starterPackNow() / 1000));
|
||||
}
|
||||
|
||||
export function starterPackTimeText(seconds: number): string {
|
||||
const value = Math.max(0, Math.floor(seconds));
|
||||
return [Math.floor(value / 3600), Math.floor(value / 60) % 60, value % 60]
|
||||
.map(part => String(part).padStart(2, '0')).join(':');
|
||||
}
|
||||
|
||||
export function applyStarterPackStatus(data: any): void {
|
||||
const info = cc.fx.GameConfig.GM_INFO;
|
||||
info.starter_packTime = (Number(data.starter_pack) || 0) / 1000;
|
||||
// A late status response cannot undo a purchase confirmed in this session.
|
||||
info.starterPackPurchased = info.starterPackPurchased || Number(data.starter_packState) === 1;
|
||||
if (Number(data.serverTime) > 0) info.starterPackServerOffset = Number(data.serverTime) - Date.now();
|
||||
}
|
||||
|
||||
export function starterPackDay(): string {
|
||||
// Keep the existing local calendar-day policy, using server-corrected time.
|
||||
const date = new Date(starterPackNow());
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
export function starterPackPopupKey(): string {
|
||||
return 'Starter_pack_' + cc.fx.GameConfig.GM_INFO.uid;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"uuid": "bf11bdec-ae45-4d49-ba7d-d23281e8c91e",
|
||||
"importer": "typescript",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ export default class Utils {
|
||||
static outTradeNo: string = "";
|
||||
// static httpip: string = `https://laf.nika4games.com/`; //五彩消消乐
|
||||
static httpip: string = `https://q6rvwvtnga.sealoshzh.site/`; //消消王者
|
||||
// static testHttpip: string = `https://sor779u2w8.sealoshzh.site/`; //测试服务器
|
||||
static testHttpip: string = `https://q6rvwvtnga.sealoshzh.site/`; //消消王者测试服暂替
|
||||
static testHttpip: string = `https://sor779u2w8.sealoshzh.site/`; //测试服务器
|
||||
// static testHttpip: string = `https://q6rvwvtnga.sealoshzh.site/`; //消消王者测试服暂替
|
||||
//#region 安卓支付
|
||||
/**获取用户信息*/
|
||||
static getUserInfo(callBack, from?: string) {
|
||||
@@ -118,6 +118,7 @@ export default class Utils {
|
||||
// console.error('支付请求超时');
|
||||
// callBack("请求支付超时");
|
||||
// }, timeoutDuration);
|
||||
const orderNumber = res.data.outTradeNo;
|
||||
//@ts-ignore
|
||||
wx.requestMidasPaymentGameItem({
|
||||
signData: res.data.signData,
|
||||
@@ -126,7 +127,7 @@ export default class Utils {
|
||||
success(res, errCode) {
|
||||
// clearTimeout(timeoutId); // 清除超时定时器
|
||||
//console.log('成功', res, errCode);
|
||||
callBack(res);
|
||||
callBack({ ...res, outTradeNo: orderNumber });
|
||||
},
|
||||
fail({ errMsg, errCode }) {
|
||||
// clearTimeout(timeoutId); // 清除超时定时器
|
||||
@@ -140,6 +141,8 @@ export default class Utils {
|
||||
callBack(data);
|
||||
}
|
||||
})
|
||||
} else {
|
||||
callBack({ err: "创建订单失败", errCode: -1, errMsg: res.msg || "创建订单失败,请重试" });
|
||||
}
|
||||
|
||||
});
|
||||
@@ -147,7 +150,8 @@ export default class Utils {
|
||||
|
||||
//#region 安卓获取支付结果
|
||||
/** 安卓获取支付结果 */
|
||||
static getPayInfo(callBack) {
|
||||
static getPayInfo(callBack, order: string = Utils.outTradeNo,
|
||||
options?: { timeoutMs: number; onProgress?: (response: any) => void }) {
|
||||
// 延迟时间数组,按照 1 秒 3 次、2 秒 5 次、5 秒 6 次、15 秒 5 次的规则生成
|
||||
const delays = [
|
||||
...Array(3).fill(3000),
|
||||
@@ -156,23 +160,42 @@ export default class Utils {
|
||||
...Array(5).fill(15000)
|
||||
];
|
||||
let attempt = 0; // 轮询次数
|
||||
let completed = false;
|
||||
let retryTimer = null;
|
||||
let deadlineTimer = null;
|
||||
const finish = (response) => {
|
||||
if (completed) return;
|
||||
completed = true;
|
||||
clearTimeout(retryTimer);
|
||||
clearTimeout(deadlineTimer);
|
||||
callBack(response);
|
||||
};
|
||||
const timeout = () => {
|
||||
if (completed) return;
|
||||
MiniGameSdk.API.showToast(options
|
||||
? "支付结果尚未确认,请稍后重新领取"
|
||||
: "网络异常,如付款成功,重进游戏可领取奖励");
|
||||
finish({ code: 0, data: { pay_state: -1 }, message: '轮询超时' });
|
||||
};
|
||||
if (options?.timeoutMs > 0) deadlineTimer = setTimeout(timeout, options.timeoutMs);
|
||||
|
||||
const poll = () => {
|
||||
if (completed) return;
|
||||
if (attempt >= delays.length) {
|
||||
MiniGameSdk.API.showToast("网络异常,如付款成功,重进游戏可领取奖励");
|
||||
callBack({ code: 0, data: { pay_state: -1 }, message: '轮询超时' });
|
||||
timeout();
|
||||
return;
|
||||
}
|
||||
|
||||
//console.log("请求uid:", Utils.uid);
|
||||
//console.log("outTradeNo:", Utils.outTradeNo);
|
||||
Utils.POST("wx/getPayInfo", { uid: Utils.uid, outTradeNo: Utils.outTradeNo }, res => {
|
||||
//console.log("查询字符结果:", res);
|
||||
if (res.code === 1 && (res.data.pay_state === 1 || res.data.pay_state === 2)) {
|
||||
callBack(res);
|
||||
Utils.POST("wx/getPayInfo", { uid: Utils.uid, outTradeNo: order }, res => {
|
||||
if (completed) return;
|
||||
if (options?.onProgress) options.onProgress(res);
|
||||
if (res?.code === 1 && (res.data?.pay_state === 1 || res.data?.pay_state === 2)) {
|
||||
finish(res);
|
||||
} else {
|
||||
attempt++;
|
||||
setTimeout(poll, delays[attempt - 1]);
|
||||
retryTimer = setTimeout(poll, delays[attempt - 1]);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -193,7 +216,7 @@ export default class Utils {
|
||||
let orderTemp = Utils.outTradeNo;
|
||||
if (order) orderTemp = order;
|
||||
//console.log("告知服务器发货的订单号:", orderTemp);
|
||||
Utils.POST("wx/getOrderReward", { outTradeNo: orderTemp }, res => {
|
||||
Utils.POST("wx/getOrderReward", { outTradeNo: orderTemp, uid: cc.fx.GameConfig.GM_INFO.uid }, res => {
|
||||
//console.log("告知服务器发货:", res);
|
||||
if (res.code === 1) {
|
||||
console.log("告知服务器发货成功:", res);
|
||||
@@ -334,6 +357,16 @@ export default class Utils {
|
||||
public static POST(url, param: object | any, callback) {
|
||||
var xhr = cc.loader.getXMLHttpRequest();
|
||||
xhr.timeout = 5000;//超时时间
|
||||
let completed = false;
|
||||
const finish = (response) => {
|
||||
if (completed) return;
|
||||
completed = true;
|
||||
callback(response);
|
||||
};
|
||||
const networkFailure = () => finish({ code: 0, data: null, msg: "网络请求失败,请重试" });
|
||||
xhr.onerror = networkFailure;
|
||||
xhr.ontimeout = networkFailure;
|
||||
xhr.onabort = networkFailure;
|
||||
let dataStr = '';
|
||||
let isDebug = false;
|
||||
param.token = cc.fx.GameConfig.GM_INFO.token;
|
||||
@@ -370,27 +403,27 @@ export default class Utils {
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr && xhr.readyState) {
|
||||
if (xhr.readyState === 4 && xhr.responseText) {
|
||||
if (xhr.readyState === 4) {
|
||||
let response: any = xhr.responseText;
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
// callback(true, JSON.parse(response));
|
||||
try {
|
||||
response = JSON.parse(response);
|
||||
} catch {
|
||||
response = { data: "数据有误", code: 0 };
|
||||
response = { data: null, code: 0, msg: "数据有误" };
|
||||
}
|
||||
if (response.code == 0) {
|
||||
if (response.msg.toString() == "token校验失败") {
|
||||
if (response.msg === "token校验失败") {
|
||||
if (MiniGameSdk) {
|
||||
MiniGameSdk.API.showToast("游戏已更新,请关闭游戏重新进入");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
callback(response);
|
||||
finish(response);
|
||||
|
||||
} else {
|
||||
callback({ data: "网络请求失败,请检查网络连接", code: 0 });
|
||||
networkFailure();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import Freeze from "../../prop/Freeze";
|
||||
import SceneManager from "../../SceneManager";
|
||||
import { MiniGameSdk } from "../../Sdk/MiniGameSdk";
|
||||
import Utils from "../Pay/Utils";
|
||||
import { starterPackReward } from "../Config/StarterPack";
|
||||
//@ts-ignore
|
||||
//最大工具类 各种公共方法,以及处理上传,获取后端接口数据
|
||||
var GameTool = {
|
||||
@@ -924,7 +925,10 @@ var GameTool = {
|
||||
},
|
||||
|
||||
//商城购买
|
||||
shopBuy(productId, compensate) {
|
||||
shopBuy(productId, compensate, orderContext: any = null) {
|
||||
const starterOrderKey = productId === "starter_pack" && orderContext?.outTradeNo
|
||||
? "StarterPackGranted_" + cc.fx.GameConfig.GM_INFO.uid + "_" + orderContext.outTradeNo : "";
|
||||
if (starterOrderKey && cc.fx.StorageMessage.getStorage(starterOrderKey)) return;
|
||||
let coin = 0;
|
||||
let price = 0;
|
||||
let getCoin = 0;
|
||||
@@ -1174,24 +1178,23 @@ var GameTool = {
|
||||
}
|
||||
this.getShopProp(propData3, compensate);
|
||||
break;
|
||||
case "starter_pack":
|
||||
cc.fx.GameTool.changeCoin(3000);
|
||||
coin = 3000;
|
||||
case "starter_pack": {
|
||||
const reward = starterPackReward();
|
||||
coin = reward.coin;
|
||||
price = 300;
|
||||
let propData4 = {
|
||||
"freeze": 5,
|
||||
"hammer": 5,
|
||||
"magic_wand": 5,
|
||||
"price": price
|
||||
}
|
||||
this.getShopProp(propData4, compensate);
|
||||
cc.fx.GameTool.changeCoin(coin);
|
||||
this.getShopProp({ freeze: reward.freeze, hammer: reward.hammer,
|
||||
magic_wand: reward.magic_wand, price }, compensate);
|
||||
rewardData = [
|
||||
{ type: "coin", count: coin },
|
||||
{ type: "freeze", count: 5 },
|
||||
{ type: "hammer", count: 5 },
|
||||
{ type: "magic", count: 5 }
|
||||
]
|
||||
{ type: "freeze", count: reward.freeze },
|
||||
{ type: "hammer", count: reward.hammer },
|
||||
{ type: "magic", count: reward.magic_wand },
|
||||
];
|
||||
cc.fx.GameConfig.GM_INFO.starterPackPurchased = true;
|
||||
if (starterOrderKey) cc.fx.StorageMessage.setStorage(starterOrderKey, true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user