/** * 订单数据 */ 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(); 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 status: "CREATED" as OrderStatus, // 订单状态 retCode: 0, // 失败时的原因码 createdAt: Date.now(), // 订单创建时间 }; 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); } /** 获取所有需要继续轮询查询的订单 id */ public getAllShouldPollId(): string[] { const map = this._orders; const ids: string[] = []; map.forEach((vo, id) => { if (shouldPoll(vo.status)) { ids.push(id); } }); return ids; } /** --------------------------------------------------------- 缓存逻辑 --------------------------------------------------------- */ private ensureMap(): Map { if (!this._orders) { this._orders = new Map(); } 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(); } } }