167 lines
4.4 KiB
TypeScript
167 lines
4.4 KiB
TypeScript
/**
|
|
* 商城数据
|
|
*/
|
|
import { BaseData } from "./BaseData";
|
|
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
|
|
|
export class ShopData extends BaseData {
|
|
// 商品数据
|
|
private _goods = new Map<number, proto.cs.IPurchaseConfig>();
|
|
// 商品 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.ICSGetPurchaseRes) {
|
|
const list = res.items ?? [];
|
|
this._goods.clear();
|
|
this._goodIds = [];
|
|
for (const g of list) {
|
|
const vo: proto.cs.IPurchaseConfig = {
|
|
id: g.id, // 商品 id
|
|
name: g.name, // 商品名字
|
|
count: g.count, // 商品数量
|
|
price: g.price, // 商品价格
|
|
};
|
|
this._goods.set(vo.id, vo);
|
|
this._goodIds.push(vo.id);
|
|
}
|
|
console.log("设置商品数据:", this._goods);
|
|
}
|
|
|
|
/** 获取所有商品数据 */
|
|
public get goods(): Map<number, proto.cs.IPurchaseConfig> { return this._goods; }
|
|
|
|
/** 获取所有商品数据的数量 */
|
|
public get goodsCount(): number { return this._goods ? this._goods.size : 0; }
|
|
|
|
/** 获取所有商品 id */
|
|
public get goodIds(): number[] { return this._goodIds; }
|
|
|
|
/** 是否是充值商品,首位数字为 1 */
|
|
public isRechargeId(id: number): boolean {
|
|
const firstDigit = id.toString()[0]; // 取首位字符
|
|
return firstDigit === "1";
|
|
}
|
|
|
|
/** 获取 充值商品的id数组,首位数字为 1 */
|
|
public getRechargeIds(): number[] {
|
|
let allId = this._goodIds;
|
|
if (!allId) return [];
|
|
// 获取所有 id
|
|
let resultId = [];
|
|
for (let one of allId) {
|
|
if (this.isRechargeId(one)) {
|
|
resultId.push(one);
|
|
}
|
|
}
|
|
return resultId;
|
|
}
|
|
|
|
/** 是否是会员商品,首位数字为 2 */
|
|
public isMemberId(id: number): boolean {
|
|
const firstDigit = id.toString()[0]; // 取首位字符
|
|
return firstDigit === "2";
|
|
}
|
|
|
|
/** 获取 会员商品的id数组,首位数字为 2 */
|
|
public getMemberIds(): number[] {
|
|
let allId = this._goodIds;
|
|
if (!allId) return [];
|
|
// 获取所有 id
|
|
let resultId = [];
|
|
for (let one of allId) {
|
|
if (this.isMemberId(one)) {
|
|
resultId.push(one);
|
|
}
|
|
}
|
|
return resultId;
|
|
}
|
|
|
|
/** 获取7天会员的 id */
|
|
public get7DayMemberId(): number {
|
|
const ids = this.getMemberIds();
|
|
const length = ids.length;
|
|
if (length <= 0) return 0;
|
|
return ids[0];
|
|
}
|
|
|
|
/** 获取30天会员的 id */
|
|
public get30DayMemberId(): number {
|
|
const ids = this.getMemberIds();
|
|
const length = ids.length;
|
|
if (length <= 1) return 0;
|
|
return ids[1];
|
|
}
|
|
|
|
/** 是否是聊天商品,首位数字为 3 */
|
|
public isChatId(id: number): boolean {
|
|
const firstDigit = id.toString()[0]; // 取首位字符
|
|
return firstDigit === "3";
|
|
}
|
|
|
|
/** 获取 聊天商品的id数组,首位数字为 3 */
|
|
public getChatIds(): number[] {
|
|
let allId = this._goodIds;
|
|
if (!allId) return [];
|
|
// 获取所有 id
|
|
let resultId = [];
|
|
for (let one of allId) {
|
|
if (this.isChatId(one)) {
|
|
resultId.push(one);
|
|
}
|
|
}
|
|
return resultId;
|
|
}
|
|
|
|
/** 根据商品 id 获取商品数据 */
|
|
private getGoodDataById(id: number): proto.cs.IPurchaseConfig | 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 获取商品的原来价格,除100 */
|
|
public getOriginalPriceById(id: number): number {
|
|
let oneData = this.getGoodDataById(id);
|
|
if (!oneData) return 0;
|
|
let price = oneData.price;
|
|
return price/100;
|
|
}
|
|
|
|
/** 根据商品 id 获取商品价格 */
|
|
public getPriceById(id: number): number {
|
|
let oneData = this.getGoodDataById(id);
|
|
if (!oneData) return 0;
|
|
return oneData.price;
|
|
}
|
|
|
|
/** 根据商品 id 获取商品数量 */
|
|
public getCountById(id: number): number | null {
|
|
let oneData = this.getGoodDataById(id);
|
|
if (!oneData) return null;
|
|
return oneData.count;
|
|
}
|
|
}
|