176 lines
4.1 KiB
TypeScript
176 lines
4.1 KiB
TypeScript
import httpService from './httpService'
|
|
import tokenManager from '../utils/tokenManager'
|
|
import type { ApiResponse } from './httpService'
|
|
|
|
// 用户接口
|
|
export interface User {
|
|
id: string
|
|
nickname: string
|
|
avatar?: string
|
|
phone?: string
|
|
email?: string
|
|
status: 'active' | 'inactive' | 'banned'
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
// 登录响应接口
|
|
export interface LoginResponse {
|
|
token: string
|
|
refreshToken: string
|
|
user: User
|
|
expiresAt: number
|
|
}
|
|
|
|
// 认证服务类
|
|
class AuthService {
|
|
// 用户登录
|
|
async login(data: { phone: string; code: string }): Promise<ApiResponse<LoginResponse>> {
|
|
const response = await httpService.post('/auth/login', data, {
|
|
needAuth: false,
|
|
showLoading: true,
|
|
loadingText: '登录中...'
|
|
})
|
|
|
|
// 登录成功后保存token
|
|
if (response.success && response.data) {
|
|
tokenManager.setToken({
|
|
accessToken: response.data.token,
|
|
refreshToken: response.data.refreshToken,
|
|
expiresAt: response.data.expiresAt
|
|
})
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
// 发送验证码
|
|
async sendSmsCode(phone: string): Promise<ApiResponse<{ success: boolean }>> {
|
|
return httpService.post('/auth/sms-code', { phone }, {
|
|
needAuth: false,
|
|
showLoading: true,
|
|
loadingText: '发送中...'
|
|
})
|
|
}
|
|
|
|
// 刷新token
|
|
async refreshToken(): Promise<ApiResponse<{ token: string; expiresAt: number }>> {
|
|
const refreshToken = tokenManager.getRefreshToken()
|
|
if (!refreshToken) {
|
|
throw new Error('没有刷新令牌')
|
|
}
|
|
|
|
const response = await httpService.post('/auth/refresh', {
|
|
refreshToken
|
|
}, {
|
|
needAuth: false
|
|
})
|
|
|
|
// 更新token
|
|
if (response.success && response.data) {
|
|
tokenManager.setToken({
|
|
accessToken: response.data.token,
|
|
expiresAt: response.data.expiresAt
|
|
})
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
// 用户登出
|
|
async logout(): Promise<void> {
|
|
try {
|
|
// 调用登出接口
|
|
await httpService.post('/auth/logout', {}, {
|
|
showLoading: true,
|
|
loadingText: '退出中...'
|
|
})
|
|
} catch (error) {
|
|
console.error('登出接口调用失败:', error)
|
|
} finally {
|
|
// 清除本地token
|
|
tokenManager.clearTokens()
|
|
}
|
|
}
|
|
|
|
// 获取当前用户信息
|
|
async getCurrentUser(): Promise<ApiResponse<User>> {
|
|
return httpService.get('/auth/me')
|
|
}
|
|
|
|
// 更新用户信息
|
|
async updateProfile(data: Partial<User>): Promise<ApiResponse<User>> {
|
|
return httpService.put('/auth/profile', data, {
|
|
showLoading: true,
|
|
loadingText: '更新中...'
|
|
})
|
|
}
|
|
|
|
// 修改密码
|
|
async changePassword(data: {
|
|
oldPassword: string
|
|
newPassword: string
|
|
}): Promise<ApiResponse<{ success: boolean }>> {
|
|
return httpService.put('/auth/password', data, {
|
|
showLoading: true,
|
|
loadingText: '修改中...'
|
|
})
|
|
}
|
|
|
|
// 绑定手机号
|
|
async bindPhone(data: {
|
|
phone: string
|
|
code: string
|
|
}): Promise<ApiResponse<{ success: boolean }>> {
|
|
return httpService.post('/auth/bind-phone', data, {
|
|
showLoading: true,
|
|
loadingText: '绑定中...'
|
|
})
|
|
}
|
|
|
|
// 解绑手机号
|
|
async unbindPhone(data: {
|
|
code: string
|
|
}): Promise<ApiResponse<{ success: boolean }>> {
|
|
return httpService.post('/auth/unbind-phone', data, {
|
|
showLoading: true,
|
|
loadingText: '解绑中...'
|
|
})
|
|
}
|
|
|
|
// 检查登录状态
|
|
isLoggedIn(): boolean {
|
|
return tokenManager.hasValidToken()
|
|
}
|
|
|
|
// 获取当前用户token
|
|
getToken(): string | null {
|
|
return tokenManager.getAccessToken()
|
|
}
|
|
|
|
// 清除登录状态
|
|
clearAuth(): void {
|
|
tokenManager.clearTokens()
|
|
}
|
|
|
|
// 账号注销
|
|
async deleteAccount(data: {
|
|
password: string
|
|
code: string
|
|
}): Promise<ApiResponse<{ success: boolean }>> {
|
|
const response = await httpService.delete('/auth/account', data, {
|
|
showLoading: true,
|
|
loadingText: '注销中...'
|
|
})
|
|
|
|
// 注销成功后清除本地数据
|
|
if (response.success) {
|
|
this.clearAuth()
|
|
}
|
|
|
|
return response
|
|
}
|
|
}
|
|
|
|
// 导出认证服务实例
|
|
export default new AuthService()
|