235 lines
5.4 KiB
TypeScript
235 lines
5.4 KiB
TypeScript
/**
|
||
* 认证相关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<AuthResponse> {
|
||
const response = await http.post<AuthResponse>('/auth/login', data)
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 用户注册
|
||
*/
|
||
static async register(data: RegisterRequest): Promise<AuthResponse> {
|
||
const response = await http.post<AuthResponse>('/auth/register', data)
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 获取图形验证码(备用)
|
||
*/
|
||
static async getCaptcha(): Promise<CaptchaResponse> {
|
||
const response = await http.get<CaptchaResponse>('/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<void> {
|
||
const response = await http.post<void>('/auth/logout')
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 刷新Token
|
||
*/
|
||
static async refreshToken(data: RefreshTokenRequest): Promise<AuthResponse> {
|
||
const response = await http.post<AuthResponse>('/auth/refresh-token', data)
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户信息
|
||
*/
|
||
static async getCurrentUserInfo(): Promise<UserInfo> {
|
||
// backend-single: GET /auth/userInfo
|
||
const response = await http.get<UserInfo>('/auth/userInfo')
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 验证Token是否有效
|
||
*/
|
||
static async validateToken(): Promise<boolean> {
|
||
const response = await http.get<boolean>('/auth/validate-token')
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 修改密码
|
||
*/
|
||
static async changePassword(data: ChangePasswordRequest): Promise<void> {
|
||
return http.post<void>('/auth/change-password', data)
|
||
}
|
||
|
||
/**
|
||
* 重置密码
|
||
*/
|
||
static async resetPassword(data: ResetPasswordRequest): Promise<void> {
|
||
return http.post<void>('/auth/resetPassword', data)
|
||
}
|
||
|
||
// 废弃:后端未提供该接口,保留新 sendSmsCode()
|
||
/**
|
||
* 发送验证码(已废弃:统一使用 sendSmsCode(phone))
|
||
*/
|
||
static async sendCode(_data: SendCodeRequest): Promise<void> {
|
||
console.warn('sendCode 已废弃,请使用 sendSmsCode(phone)')
|
||
return Promise.resolve()
|
||
}
|
||
|
||
/**
|
||
* 验证验证码(不使用,登录由后端校验短信验证码)
|
||
*/
|
||
static async verifyCode(_data: VerifyCodeRequest): Promise<boolean> {
|
||
console.warn('verifyCode 不需要在前端调用,登录时由后端校验短信验证码')
|
||
return Promise.resolve(true)
|
||
}
|
||
|
||
/**
|
||
* 第三方登录
|
||
*/
|
||
static async oauthLogin(data: OAuthLoginRequest): Promise<AuthResponse> {
|
||
return http.post<AuthResponse>('/auth/oauth/login', data)
|
||
}
|
||
|
||
/**
|
||
* 绑定第三方账号
|
||
*/
|
||
static async bindOAuth(data: BindOAuthRequest): Promise<void> {
|
||
return http.post<void>('/auth/oauth/bind', data)
|
||
}
|
||
|
||
/**
|
||
* 解绑第三方账号
|
||
*/
|
||
static async unbindOAuth(data: UnbindOAuthRequest): Promise<void> {
|
||
return http.post<void>('/auth/oauth/unbind', data)
|
||
}
|
||
|
||
/**
|
||
* 获取第三方账号绑定信息
|
||
*/
|
||
static async getOAuthInfo(): Promise<OAuthInfo[]> {
|
||
return http.get<OAuthInfo[]>('/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<void> {
|
||
return http.post<void>(`/auth/force-logout/${userId}`)
|
||
}
|
||
|
||
/**
|
||
* 检查账号是否存在
|
||
*/
|
||
static async checkAccountExists(account: string): Promise<boolean> {
|
||
const response = await http.get<boolean>('/auth/check-account', {
|
||
params: { account }
|
||
})
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 检查邮箱是否存在
|
||
*/
|
||
static async checkEmailExists(email: string): Promise<boolean> {
|
||
const response = await http.get<boolean>('/auth/check-email', {
|
||
params: { email }
|
||
})
|
||
return response.data
|
||
}
|
||
|
||
/**
|
||
* 检查手机号是否存在
|
||
*/
|
||
static async checkPhoneExists(phone: string): Promise<boolean> {
|
||
const response = await http.get<boolean>('/auth/check-phone', {
|
||
params: { phone }
|
||
})
|
||
return response.data
|
||
}
|
||
}
|
||
|
||
// 导出默认实例
|
||
export default AuthService
|