/** * 认证服务 * 处理登录、注册等认证相关的 API 请求 */ import { request } from '@/src/utils/api'; import type { LoginFormData, RegisterFormData, ForgotPasswordFormData, ResetPasswordFormData, ChangePasswordFormData, PhoneLoginFormData, } from '@/src/schemas/auth'; import type { User } from '@/src/schemas/user'; /** * API 响应接口 */ interface ApiResponse { code: number; message: string; data: T; } /** * 登录响应 */ interface LoginResponse { user: User; token: string; refreshToken?: string; } /** * 认证服务类 */ class AuthService { /** * 邮箱登录 */ async login(data: LoginFormData): Promise { const response = await request.post>( '/auth/login', data ); return response.data; } /** * 手机号登录 */ async phoneLogin(data: PhoneLoginFormData): Promise { const response = await request.post>( '/auth/phone-login', data ); return response.data; } /** * 注册 */ async register(data: RegisterFormData): Promise { const response = await request.post>( '/auth/register', data ); return response.data; } /** * 登出 */ async logout(): Promise { await request.post('/auth/logout'); } /** * 发送忘记密码邮件 */ async forgotPassword(data: ForgotPasswordFormData): Promise { await request.post('/auth/forgot-password', data); } /** * 重置密码 */ async resetPassword(data: ResetPasswordFormData): Promise { await request.post('/auth/reset-password', data); } /** * 修改密码 */ async changePassword(data: ChangePasswordFormData): Promise { await request.post('/auth/change-password', data); } /** * 发送验证码 */ async sendVerificationCode(phone: string): Promise { await request.post('/auth/send-code', { phone }); } /** * 刷新 token */ async refreshToken(refreshToken: string): Promise<{ token: string }> { const response = await request.post>( '/auth/refresh-token', { refreshToken } ); return response.data; } /** * 验证 token 是否有效 */ async verifyToken(): Promise { try { await request.get('/auth/verify-token'); return true; } catch { return false; } } } // 导出单例 export const authService = new AuthService(); export default authService;