2025-08-29 16:39:41 +08:00
|
|
|
import type { ApiResponse } from "../client/types";
|
|
|
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|
|
|
|
import type { IAuthService } from "./IAuthService";
|
|
|
|
|
|
|
|
|
|
export class MockAuthService implements IAuthService {
|
|
|
|
|
private delay(ms = 180) { return new Promise<void>(r => setTimeout(r, ms)); }
|
|
|
|
|
private ok<T>(data: T): ApiResponse<T> {
|
2025-09-08 15:10:51 +08:00
|
|
|
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
|
2025-08-29 16:39:41 +08:00
|
|
|
}
|
|
|
|
|
// private err<T = unknown>(message = "mock login error"): ApiResponse<T> {
|
|
|
|
|
// return ({ ok: false, error: { message } } as unknown) as ApiResponse<T>;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 登录
|
|
|
|
|
async login(req: proto.cs.ICSLoginReq): Promise<ApiResponse<proto.cs.ICSLoginRes>> {
|
2025-09-08 15:10:51 +08:00
|
|
|
// 延时
|
2025-08-29 16:39:41 +08:00
|
|
|
await this.delay(150);
|
|
|
|
|
|
2025-09-08 15:10:51 +08:00
|
|
|
const isGuest = req?.platType === "guest";
|
|
|
|
|
const userId = req?.userId ?? "uid";
|
|
|
|
|
const name = isGuest ? `guest_${userId.toString().slice(-6)}` : "tester";
|
|
|
|
|
|
|
|
|
|
// 过期时间(秒级时间戳,7天后过期)
|
2025-08-29 16:39:41 +08:00
|
|
|
const expireSeconds = Math.floor(Date.now() / 1000) + 7 * 24 * 3600;
|
|
|
|
|
|
2025-09-08 15:10:51 +08:00
|
|
|
// 模拟用户ID
|
|
|
|
|
const accId = Math.floor(Math.random() * 1000000);
|
|
|
|
|
|
|
|
|
|
// 模拟 CDN 前缀
|
|
|
|
|
const cdn = "https://cdn.mockserver.com/";
|
|
|
|
|
|
2025-08-29 16:39:41 +08:00
|
|
|
const res: proto.cs.ICSLoginRes = {
|
|
|
|
|
name,
|
|
|
|
|
token: `mock_token_${Date.now()}`,
|
|
|
|
|
refreshToken: `mock_refresh_${Date.now()}`,
|
|
|
|
|
expire: expireSeconds,
|
2025-09-08 15:10:51 +08:00
|
|
|
accId,
|
|
|
|
|
cdn,
|
2025-08-29 16:39:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
}
|