40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import type { ApiResponse } from "../client/types";
|
||||
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|||
|
|
import type { IShopService } from "./IShopService";
|
|||
|
|
|
|||
|
|
export class MockShopService implements IShopService {
|
|||
|
|
private delay(ms = 180) { return new Promise<void>(r => setTimeout(r, ms)); }
|
|||
|
|
private randInt(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; }
|
|||
|
|
private priceUSD(min = 0.99, max = 49.99) {
|
|||
|
|
const v = Math.random() * (max - min) + min;
|
|||
|
|
return v.toFixed(2); // 字符串,保留两位小数
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private ok<T>(data: T): ApiResponse<T> {
|
|||
|
|
return ({ ok: true, data } as unknown) as ApiResponse<T>;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private genGood(id: number, vip = false): proto.cs.IGood {
|
|||
|
|
return {
|
|||
|
|
id,
|
|||
|
|
name: vip ? `VIP-${id}` : `Diamond Pack ${id}`,
|
|||
|
|
desc: vip ? `VIP access for ${this.randInt(24, 168)} hours` : `Diamonds x${this.randInt(60, 2000)}`,
|
|||
|
|
price: this.priceUSD(vip ? 4.99 : 0.99, vip ? 49.99 : 29.99),
|
|||
|
|
count: vip ? this.randInt(24, 168) : this.randInt(60, 2000), // VIP=小时,普通=数量
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取商品列表
|
|||
|
|
async reqShopList(_req: proto.cs.ICSGetShopReq): Promise<ApiResponse<proto.cs.ICSGetShopRes>> {
|
|||
|
|
await this.delay();
|
|||
|
|
|
|||
|
|
// 普通商品(<=2000)+ VIP 商品(>2000)
|
|||
|
|
const normals: proto.cs.IGood[] = Array.from({ length: 5 }, (_, i) => this.genGood(1000 + i, false));
|
|||
|
|
const vips: proto.cs.IGood[] = Array.from({ length: 3 }, (_, i) => this.genGood(2001 + i, true));
|
|||
|
|
|
|||
|
|
// 注意:你的 proto 定义中字段名为 "Goods"(大写 G)
|
|||
|
|
const res: proto.cs.ICSGetShopRes = { Goods: [...normals, ...vips] };
|
|||
|
|
return this.ok(res);
|
|||
|
|
}
|
|||
|
|
}
|