/** * Auth API * * 认证相关接口 * 原始 Dart: lib/data/services/api/auth_api_client.dart */ import { httpClient } from "./http_client"; import { ApiPath } from "./api_path"; import { AppleLoginRequest } from "@/data/dto/auth/apple_login_request"; import { FacebookLoginRequest } from "@/data/dto/auth/facebook_login_request"; import { FbIdLoginRequest } from "@/data/dto/auth/fb_id_login_request"; import { GoogleLoginRequest } from "@/data/dto/auth/google_login_request"; import { GuestLoginRequest } from "@/data/dto/auth/guest_login_request"; import { GuestLoginResponse } from "@/data/dto/auth/guest_login_response"; import { LoginRequest } from "@/data/dto/auth/login_request"; import { LoginResponse } from "@/data/dto/auth/login_response"; import { LogoutResponse } from "@/data/dto/auth/logout_response"; import { RefreshTokenRequest } from "@/data/dto/auth/refresh_token_request"; import { RefreshTokenResponse } from "@/data/dto/auth/refresh_token_response"; import { RegisterRequest } from "@/data/dto/auth/register_request"; import { SendCodeRequest } from "@/data/dto/auth/send_code_request"; import { User } from "@/data/dto/user/user"; import type { UserData } from "@/data/schemas/user/user"; /** * 后端统一响应包装 */ interface ApiEnvelope { success: boolean; data?: T; message?: string; error?: string; } function unwrap(envelope: ApiEnvelope): T { if (!envelope.success || envelope.data === undefined) { throw new Error(envelope.message ?? envelope.error ?? "API call failed"); } return envelope.data; } export class AuthApi { /** * 发送验证码 */ async sendCode(body: SendCodeRequest): Promise { await httpClient>(ApiPath.sendCode, { method: "POST", body: body.toJson(), }); } /** * 邮箱密码登录 */ async emailLogin(body: LoginRequest): Promise { const env = await httpClient>( ApiPath.emailLogin, { method: "POST", body: body.toJson() } ); return LoginResponse.fromJson(unwrap(env) as Record); } /** * 注册 */ async register(body: RegisterRequest): Promise { await httpClient>(ApiPath.register, { method: "POST", body: body.toJson(), }); } /** * Apple 登录 */ async appleLogin(body: AppleLoginRequest): Promise { const env = await httpClient>(ApiPath.appleLogin, { method: "POST", body: body.toJson(), }); return LoginResponse.fromJson(unwrap(env) as Record); } /** * Google 登录 */ async googleLogin(body: GoogleLoginRequest): Promise { const env = await httpClient>(ApiPath.googleLogin, { method: "POST", body: body.toJson(), }); return LoginResponse.fromJson(unwrap(env) as Record); } /** * Facebook 登录 */ async facebookLogin(body: FacebookLoginRequest): Promise { const env = await httpClient>( ApiPath.facebookLogin, { method: "POST", body: body.toJson() } ); return LoginResponse.fromJson(unwrap(env) as Record); } /** * Facebook ID 登录(v7.0 新增) */ async facebookIdLogin(body: FbIdLoginRequest): Promise { const env = await httpClient>( ApiPath.facebookIdLogin, { method: "POST", body: body.toJson() } ); return LoginResponse.fromJson(unwrap(env) as Record); } /** * 退出登录 */ async logout(): Promise { const env = await httpClient>(ApiPath.logout, { method: "POST", }); return LogoutResponse.fromJson(unwrap(env) as Record); } /** * 游客登录 */ async guestLogin(body: GuestLoginRequest): Promise { const env = await httpClient>(ApiPath.guestLogin, { method: "POST", body: body.toJson(), }); return GuestLoginResponse.fromJson(unwrap(env) as Record); } /** * 刷新 Token */ async refreshToken( body: RefreshTokenRequest ): Promise { const env = await httpClient>(ApiPath.refresh, { method: "POST", body: body.toJson(), }); return RefreshTokenResponse.fromJson( unwrap(env) as Record ); } /** * 获取当前用户信息 */ async getCurrentUser(): Promise { const env = await httpClient>( ApiPath.getCurrentUser ); return User.fromJson(unwrap(env) as UserData); } } /** * 全局单例 */ export const authApi = new AuthApi();