/** * 认证相关API服务 */ import { http } from '@/utils/request' import type { LoginRequest, RegisterRequest, AuthResponse, UserInfo, CaptchaResponse, RefreshTokenRequest, ChangePasswordRequest, ResetPasswordRequest, SendCodeRequest, VerifyCodeRequest, OAuthLoginRequest, BindOAuthRequest, UnbindOAuthRequest, OAuthInfo, LoginHistory, OnlineUser } from '@/types/auth' /** * 认证API服务类 */ export class AuthService { /** * 用户登录 */ static async login(data: LoginRequest): Promise { const response = await http.post('/auth/login', data) return response.data } /** * 用户注册 */ static async register(data: RegisterRequest): Promise { const response = await http.post('/auth/register', data) return response.data } /** * 获取图形验证码(备用) */ static async getCaptcha(): Promise { const response = await http.get('/auth/captcha') return response.data } /** * 发送短信验证码(用于登录/注册/找回密码等场景) */ static async sendSmsCode(phone: string) { // backend-single: GET /auth/sms-code?phone=xxx const response = await http.get('/auth/sms-code', { params: { phone } }) return response.data } /** * 用户登出 */ static async logout(): Promise { const response = await http.post('/auth/logout') return response.data } /** * 刷新Token */ static async refreshToken(data: RefreshTokenRequest): Promise { const response = await http.post('/auth/refresh-token', data) return response.data } /** * 获取当前用户信息 */ static async getCurrentUserInfo(): Promise { // backend-single: GET /auth/userInfo const response = await http.get('/auth/userInfo') return response.data } /** * 验证Token是否有效 */ static async validateToken(): Promise { const response = await http.get('/auth/validate-token') return response.data } /** * 修改密码 */ static async changePassword(data: ChangePasswordRequest): Promise { return http.post('/auth/change-password', data) } /** * 重置密码 */ static async resetPassword(data: ResetPasswordRequest): Promise { return http.post('/auth/resetPassword', data) } // 废弃:后端未提供该接口,保留新 sendSmsCode() /** * 发送验证码(已废弃:统一使用 sendSmsCode(phone)) */ static async sendCode(_data: SendCodeRequest): Promise { console.warn('sendCode 已废弃,请使用 sendSmsCode(phone)') return Promise.resolve() } /** * 验证验证码(不使用,登录由后端校验短信验证码) */ static async verifyCode(_data: VerifyCodeRequest): Promise { console.warn('verifyCode 不需要在前端调用,登录时由后端校验短信验证码') return Promise.resolve(true) } /** * 第三方登录 */ static async oauthLogin(data: OAuthLoginRequest): Promise { return http.post('/auth/oauth/login', data) } /** * 绑定第三方账号 */ static async bindOAuth(data: BindOAuthRequest): Promise { return http.post('/auth/oauth/bind', data) } /** * 解绑第三方账号 */ static async unbindOAuth(data: UnbindOAuthRequest): Promise { return http.post('/auth/oauth/unbind', data) } /** * 获取第三方账号绑定信息 */ static async getOAuthInfo(): Promise { return http.get('/auth/oauth/info') } // 移除权限接口,该接口不存在 /** * 获取登录历史 */ static async getLoginHistory(page = 1, size = 10): Promise<{ list: LoginHistory[] total: number page: number size: number }> { return http.get<{ list: LoginHistory[] total: number page: number size: number }>('/auth/login-history', { params: { page, size } }) } /** * 获取在线用户列表(管理员功能) */ static async getOnlineUsers(page = 1, size = 10): Promise<{ list: OnlineUser[] total: number page: number size: number }> { return http.get<{ list: OnlineUser[] total: number page: number size: number }>('/auth/online-users', { params: { page, size } }) } /** * 强制下线用户(管理员功能) */ static async forceLogout(userId: string): Promise { return http.post(`/auth/force-logout/${userId}`) } /** * 检查账号是否存在 */ static async checkAccountExists(account: string): Promise { const response = await http.get('/auth/check-account', { params: { account } }) return response.data } /** * 检查邮箱是否存在 */ static async checkEmailExists(email: string): Promise { const response = await http.get('/auth/check-email', { params: { email } }) return response.data } /** * 检查手机号是否存在 */ static async checkPhoneExists(phone: string): Promise { const response = await http.get('/auth/check-phone', { params: { phone } }) return response.data } } // 导出默认实例 export default AuthService