2025-08-27 14:29:37 +08:00
|
|
|
/**
|
|
|
|
|
* 订单数据
|
|
|
|
|
*/
|
|
|
|
|
import { BaseData } from "./BaseData";
|
|
|
|
|
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
|
|
|
|
import type { OrderVO, OrderStatus } from 'db://assets/Scripts/chat18x/payment/types';
|
|
|
|
|
import { mapQueryOrderStatus, shouldPoll } from 'db://assets/Scripts/chat18x/payment/PaymentUtil';
|
|
|
|
|
import { sys } from "cc";
|
|
|
|
|
|
|
|
|
|
const ORDER_STORAGE_KEY = "order_data_v1";
|
|
|
|
|
|
|
|
|
|
export class OrderData extends BaseData {
|
|
|
|
|
// 订单列表
|
|
|
|
|
private _orders = new Map<string, OrderVO>();
|
|
|
|
|
|
|
|
|
|
public reset(): void {
|
|
|
|
|
this._orders.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public clear(): void {
|
|
|
|
|
this._orders.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
super.destroy();
|
|
|
|
|
this._orders = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 下单创建:保存 payUrl + orderId,标记处理中 */
|
|
|
|
|
public saveOrderData(res: proto.cs.ICSCreateOrderRes) {
|
|
|
|
|
const orderId = res.orderId || "";
|
|
|
|
|
if (!orderId) return;
|
|
|
|
|
const vo: OrderVO = {
|
|
|
|
|
orderId, // 订单 id
|
|
|
|
|
payUrl: res.payUrl || "", // 支付url
|
2025-09-18 17:12:10 +08:00
|
|
|
payType: res.payType, // 支付类型
|
|
|
|
|
network: res.network || 0, // 网络类型
|
|
|
|
|
wallet: res.wallet || "", // 充值钱包地址
|
|
|
|
|
amount: res.amount || "", // 充值金额
|
|
|
|
|
expire: res.Expire || 0, // 过期时间,单位: 秒
|
2025-09-18 18:28:14 +08:00
|
|
|
expireUnix: res.ExpireUnix || "", // 过期时间戳,秒
|
2025-08-27 14:29:37 +08:00
|
|
|
status: "CREATED" as OrderStatus, // 订单状态
|
|
|
|
|
retCode: 0, // 失败时的原因码
|
2025-09-18 18:28:14 +08:00
|
|
|
createdAt: Date.now(), // 订单创建时间,毫秒
|
2025-08-27 14:29:37 +08:00
|
|
|
};
|
|
|
|
|
this._orders.set(orderId, vo);
|
|
|
|
|
console.log("保存订单数据:", this._orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取订单数据 */
|
|
|
|
|
private getOrderDataById(id: string): OrderVO | null {
|
|
|
|
|
if (!this._orders) return null;
|
|
|
|
|
return this._orders.get(id) ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取订单状态 */
|
|
|
|
|
public getStatusById(id: string): OrderStatus | null {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return null;
|
|
|
|
|
return oneData.status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 查询结果:更新订单状态 */
|
|
|
|
|
applyQueryOrder(orderId: string, res: proto.cs.ICSQueryOrderRes) {
|
|
|
|
|
const vo = this.getOrderDataById(orderId);
|
|
|
|
|
if (!vo) return;
|
|
|
|
|
// 更新数据
|
|
|
|
|
vo.status = mapQueryOrderStatus(res.status);
|
|
|
|
|
vo.retCode = res.retCode ?? 0;
|
|
|
|
|
this._orders.set(orderId, vo);
|
2025-09-11 16:32:16 +08:00
|
|
|
console.log("更新订单数据:", vo);
|
2025-08-27 14:29:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 获取所有需要继续轮询查询的订单 id */
|
|
|
|
|
public getAllShouldPollId(): string[] {
|
|
|
|
|
const map = this._orders;
|
|
|
|
|
const ids: string[] = [];
|
|
|
|
|
map.forEach((vo, id) => {
|
|
|
|
|
if (shouldPoll(vo.status)) {
|
|
|
|
|
ids.push(id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 18:28:14 +08:00
|
|
|
/** 根据订单 id 获取支付url */
|
|
|
|
|
public getPayUrlById(id: string): string {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return "";
|
|
|
|
|
return oneData.payUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取支付类型 */
|
|
|
|
|
public getPayTypeById(id: string): number {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return 0;
|
|
|
|
|
return oneData.payType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取网络类型 */
|
|
|
|
|
public getNetworkById(id: string): number {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return 0;
|
|
|
|
|
return oneData.network;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取充值钱包地址 */
|
|
|
|
|
public getWalletById(id: string): string {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return "";
|
|
|
|
|
return oneData.wallet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取充值金额 */
|
|
|
|
|
public getAmountById(id: string): string {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return "";
|
|
|
|
|
return oneData.amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取过期时间,单位: 秒 */
|
|
|
|
|
public getExpireById(id: string): number {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return 0;
|
|
|
|
|
return oneData.expire;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取过期时间戳,秒 */
|
|
|
|
|
public getExpireUnixById(id: string): string {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return "";
|
|
|
|
|
return oneData.expireUnix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取订单失败时的原因码 */
|
|
|
|
|
public getRetCodeById(id: string): number {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return 0;
|
|
|
|
|
return oneData.retCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 根据订单 id 获取订单创建时间,毫秒 */
|
|
|
|
|
public getCreatedAtById(id: string): number {
|
|
|
|
|
let oneData = this.getOrderDataById(id);
|
|
|
|
|
if (!oneData) return 0;
|
|
|
|
|
return oneData.createdAt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 14:29:37 +08:00
|
|
|
/** --------------------------------------------------------- 缓存逻辑 --------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
private ensureMap(): Map<string, OrderVO> {
|
|
|
|
|
if (!this._orders) {
|
|
|
|
|
this._orders = new Map<string, OrderVO>();
|
|
|
|
|
}
|
|
|
|
|
return this._orders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 从本地缓存加载订单数据(覆盖内存中的 Map) */
|
|
|
|
|
public loadStorage(): void {
|
|
|
|
|
try {
|
|
|
|
|
const raw = sys.localStorage.getItem(ORDER_STORAGE_KEY);
|
|
|
|
|
if (!raw) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const parsed = JSON.parse(raw) as { v: number; ts: number; orders: OrderVO[] };
|
|
|
|
|
const list = Array.isArray(parsed?.orders) ? parsed.orders : [];
|
|
|
|
|
const map = this.ensureMap();
|
|
|
|
|
map.clear();
|
|
|
|
|
for (const vo of list) {
|
|
|
|
|
// 简单校验必要字段
|
|
|
|
|
if (vo && vo.orderId) {
|
|
|
|
|
map.set(vo.orderId, vo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log("加载本地订单数据:", map);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn("[OrderData] loadStorage parse error:", e);
|
|
|
|
|
this.ensureMap().clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 将当前订单 Map 落盘到本地缓存 */
|
|
|
|
|
public saveStorage(): void {
|
|
|
|
|
try {
|
|
|
|
|
const map = this.ensureMap();
|
|
|
|
|
const orders: OrderVO[] = Array.from(map.values());
|
|
|
|
|
const payload = {
|
|
|
|
|
v: 1,
|
|
|
|
|
ts: Date.now(),
|
|
|
|
|
orders,
|
|
|
|
|
};
|
|
|
|
|
sys.localStorage.setItem(ORDER_STORAGE_KEY, JSON.stringify(payload));
|
|
|
|
|
console.log("保存本地订单数据:", payload);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn("[OrderData] saveStorage error:", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 删除本地缓存(同时清空内存中的 Map) */
|
|
|
|
|
public removeStorage(): void {
|
|
|
|
|
try {
|
|
|
|
|
sys.localStorage.removeItem(ORDER_STORAGE_KEY);
|
|
|
|
|
} finally {
|
|
|
|
|
this.ensureMap().clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|