64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import type { ApiResponse } from "../client/types";
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|
import type { IGirlService } from "./IGirlService";
|
|
|
|
export class MockGirlService implements IGirlService {
|
|
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 pick<T>(arr: T[]) { return arr[this.randInt(0, arr.length - 1)]; }
|
|
|
|
private ok<T>(data: T): ApiResponse<T> {
|
|
return ({ ok: true, data } as unknown) as ApiResponse<T>;
|
|
}
|
|
private err<T = unknown>(message = "mock error"): ApiResponse<T> {
|
|
return ({ ok: false, error: { message } } as unknown) as ApiResponse<T>;
|
|
}
|
|
|
|
private genBrief(id: number): proto.cs.IGirlBrief {
|
|
const tags = ["cute", "cool", "elegant", "sporty", "sweet"];
|
|
return {
|
|
id,
|
|
name: `Girl-${id}`,
|
|
age: this.randInt(18, 28),
|
|
tagKey: this.pick(tags),
|
|
priceType: (id % 2) + 1,
|
|
price: 50 + (id % 6) * 10,
|
|
avatar: `https://dummy.local/avatar/${id}.png`,
|
|
star: (id % 5) + 1,
|
|
};
|
|
}
|
|
|
|
private genPhoto(idx: number): proto.cs.IGirlPhoto {
|
|
return { pic: `https://dummy.local/photo/${idx}.jpg`, isRelease: Math.random() < 0.5, price: 20 + (idx % 5) * 5 };
|
|
}
|
|
|
|
private genDetail(id: number): proto.cs.IGirlDetail {
|
|
const photos = Array.from({ length: 4 }, (_, i) => this.genPhoto(id * 10 + i));
|
|
return { brief: this.genBrief(id), desc: `Mock detail for ${id}`, photos, isRelease: Math.random() < 0.4, chatCount: this.randInt(0, 5) };
|
|
}
|
|
|
|
// 每日推荐
|
|
async reqDailyRecommend(_req: proto.cs.ICSDailyRecommendReq): Promise<ApiResponse<proto.cs.ICSDailyRecommendRes>> {
|
|
await this.delay(180);
|
|
const count = this.randInt(4, 8);
|
|
const girls = Array.from({ length: count }, (_, i) => this.genBrief(100 + i));
|
|
return this.ok({ girls });
|
|
}
|
|
|
|
// 获取技师列表
|
|
async reqGetGirlList(req: proto.cs.ICSGetGirlListReq): Promise<ApiResponse<proto.cs.ICSGetGirlListRes>> {
|
|
await this.delay(200);
|
|
const base = (req?.category ?? 0) * 1000 + (req?.page ?? 1) * 100;
|
|
const limit = Math.max(1, Math.min(req?.limit ?? 10, 50));
|
|
const girls = Array.from({ length: limit }, (_, i) => this.genBrief(base + i));
|
|
return this.ok({ girls });
|
|
}
|
|
|
|
// 获取技师详细信息
|
|
async reqGetGirlDetail(req: proto.cs.ICSGetGirlDetailReq): Promise<ApiResponse<proto.cs.ICSGetGirlDetailRes>> {
|
|
await this.delay(160);
|
|
const id = req?.id ?? this.randInt(1, 9999);
|
|
return this.ok({ detail: this.genDetail(id) });
|
|
}
|
|
}
|