/** * 商品数据 */ import { BaseData } from "./BaseData"; import proto from 'db://assets/Scripts/proto/proto.pb.js'; export class ShopData extends BaseData { // 商品数据 private _goods = new Map(); // 商品 id private _goodIds: number[] = []; public reset(): void { this._goods.clear(); this._goodIds = []; } public clear(): void { this._goods.clear(); this._goodIds = []; } public destroy(): void { super.destroy(); this._goods = null; this._goodIds = null; } /** 设置商品数据 */ public set goods(res: proto.cs.ICSGetShopRes) { const list = res.Goods ?? []; this._goods.clear(); this._goodIds = []; for (const g of list) { const vo: proto.cs.IGood = { id: g.id, // 商品 id name: g.name || "", // 商品名字 desc: g.desc || "", // 商品描述 price: g.price || "0.00", // 商品价格 count: g.count || 0, // 商品数量 }; this._goods.set(vo.id, vo); this._goodIds.push(vo.id); } console.log("设置商品数据:", this._goods); } /** 获取所有商品数据 */ public get goods(): Map { return this._goods; } /** 获取所有商品数据的数量 */ public get goodsCount(): number { return this._goods ? this._goods.size : 0; } /** 获取所有商品 id */ public get goodIds(): number[] { return this._goodIds; } /** 获取vip商品数据 */ public get vipGoods(): proto.cs.IGood[] { return this._goodIds.map(id => this._goods.get(id)!).filter(g => g.id > 2000); } /** 获取普通商品数据 */ public get normalGoods(): proto.cs.IGood[] { return this._goodIds.map(id => this._goods.get(id)!).filter(g => g.id <= 2000); } /** 根据商品 id 获取商品数据 */ private getGoodDataById(id: number): proto.cs.IGood | null { if (!this._goods) return null; return this._goods.get(id) ?? null; } /** 根据商品 id 获取商品名字 */ public getNameById(id: number): string | null { let oneData = this.getGoodDataById(id); if (!oneData) return null; return oneData.name; } /** 根据商品 id 获取商品描述 */ public getDescById(id: number): string | null { let oneData = this.getGoodDataById(id); if (!oneData) return null; return oneData.desc; } /** 根据商品 id 获取商品价格 */ public getPriceById(id: number): string | null { let oneData = this.getGoodDataById(id); if (!oneData) return null; return oneData.price; } /** 根据商品 id 获取商品数量 */ public getCountById(id: number): number | null { let oneData = this.getGoodDataById(id); if (!oneData) return null; return oneData.count; } }