MatchMaster/assets/Script/seven_day_gift/SevenDayGiftBootstrapApi.ts

73 lines
2.7 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 Utils from "../module/Pay/Utils";
// 主包用于判断是否展示的活动快照,具体奖励由分包解析。
export interface SevenDayActivityInfo {
serverNow: string | number;
activityId: string;
status: string;
triggerLevel: number;
triggerReached: boolean;
startAt: string | number;
endAt: string | number;
claimedCount: number;
todayClaimed: boolean;
canClaim: boolean;
nextRewardDay: number;
rewards: any[];
}
interface SevenDayInfoResponse {
code: number;
data: SevenDayActivityInfo | false;
msg?: string;
}
/** 七日活动查询入口,与分包内的资产同步接口分开。 */
export default class SevenDayGiftBootstrapApi {
// 与 Utils.POST 的选址规则保持一致;同源地址不视为独立测试服。
public static getTestEnvironment() {
const version = cc.fx.GameTool.getWechatGameVersion();
const testing = version === "开发版" || version === "体验版";
const baseUrl = testing ? Utils.testHttpip : Utils.httpip;
const origin = (url: string) => (url || "").trim().toLowerCase().match(/^https?:\/\/[^/]+/);
const testOrigin = origin(Utils.testHttpip);
const productionOrigin = origin(Utils.httpip);
return {
version, baseUrl,
allowed: testing && !!testOrigin && !!productionOrigin && testOrigin[0] !== productionOrigin[0],
};
}
// 主包只查询活动信息,不提前加载活动资源。
public static getInfo(callback: (response: SevenDayInfoResponse) => void) {
const endpoint = "signInActivityInfo";
const params = { uid: this.getUid() };
let completed = false; // 超时后到达的响应不再处理。
const timeoutHandle = setTimeout(() => {
if (completed) return;
completed = true;
cc.warn("[SevenDayGiftBootstrapApi] 请求超时:" + endpoint);
callback({ code: 0, data: null, msg: "请求超时,请稍后重试" });
}, 5500);
Utils.POST(endpoint, params, response => {
// console.log("[signInActivityInfo] 回复" + (completed ? "(已超时或重复,不再处理)" : ""), JSON.stringify(response, null, 2));
if (completed) return;
completed = true;
clearTimeout(timeoutHandle);
callback(response);
});
}
// 优先使用本地登录 UID,并同步到运行时账号信息。
public static getUid(): string {
const storedUid = cc.fx.StorageMessage.getStorage("uid");
if (storedUid !== undefined && storedUid !== null && storedUid !== "") {
cc.fx.GameConfig.GM_INFO.uid = storedUid;
}
return cc.fx.GameConfig.GM_INFO.uid || "";
}
}