模拟数据:每日推荐

This commit is contained in:
chen wei bo
2025-09-02 15:08:06 +08:00
parent 4e18e56cf9
commit 4945bcb66f
8 changed files with 78 additions and 24 deletions
@@ -5,7 +5,7 @@ import type { ApiResponse } from "../client/types";
import proto from "db://assets/Scripts/proto/proto.pb.js";
// 模拟开关
const USE_MOCK = false;
const USE_MOCK = true;
export class GirlService implements IGirlService {
private static _I: GirlService | null = null;
@@ -1,6 +1,8 @@
import type { ApiResponse } from "../client/types";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import type { IGirlService } from "./IGirlService";
import { ConfigManager, ConfigId } from "../../manager/ConfigManager";
import { GirlConfig } from "../../config/GirlConfig";
export class MockGirlService implements IGirlService {
private delay(ms = 180) { return new Promise<void>(r => setTimeout(r, ms)); }
@@ -8,23 +10,22 @@ export class MockGirlService implements IGirlService {
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>;
return ({ code: proto.cs.EnmRetCode.SUCCESS, 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>;
return ({ code: 100, error: { message } } as unknown) as ApiResponse<T>;
}
private genBrief(id: number): proto.cs.IGirlBrief {
const tags = ["cute", "cool", "elegant", "sporty", "sweet"];
private genBrief(id: number, name: string, age: number, tagKey: string, priceType: number, price: number, avatar: string, star: number): proto.cs.IGirlBrief {
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,
name,
age,
tagKey,
priceType,
price,
avatar,
star,
};
}
@@ -34,14 +35,27 @@ export class MockGirlService implements IGirlService {
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) };
return { brief: this.genBrief(id, "", 1, "", 2, 3, "", 4), 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));
const configData = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const allId = configData.getAllId();
const recId = allId[0];
let name = configData.getNameById(recId);
let age = Number(configData.getAgeById(recId));
let tagKey = configData.getTagKeyById(recId);
let priceType = configData.getPriceTypeById(recId);
let price = 9.9;
let avatar = configData.getAvatarPathById(recId);
let star = 3;
let oneData = this.genBrief(recId, name, age, tagKey, priceType, price, avatar, star);
// 返回数据
const girls = [oneData];
return this.ok({ girls });
}
@@ -50,7 +64,7 @@ export class MockGirlService implements IGirlService {
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));
const girls = Array.from({ length: limit }, (_, i) => this.genBrief(base + i, "", 1, "", 2, 3, "", 4));
return this.ok({ girls });
}
@@ -38,6 +38,7 @@ export class MockThemeService implements IThemeService {
let oneData = this.makeTheme(oneId, key, name, category, isRelease, path);
themes.push(oneData);
}
// 返回数据
const res: proto.cs.ICSHallThemeRes = { themes };
return this.ok(res);
}