feat: 完成情绪博物馆项目重构和功能增强 - 新增日记评论和帖子功能 - 重构前端架构,优化用户体验 - 完善WebSocket通信机制 - 更新项目文档和部署配置
This commit is contained in:
+198
-91
@@ -1,114 +1,221 @@
|
||||
import request from '@/utils/request'
|
||||
/**
|
||||
* 认证相关API服务
|
||||
*/
|
||||
|
||||
import { http } from '@/utils/request'
|
||||
import type {
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
RegisterRequest,
|
||||
AuthResponse,
|
||||
UserInfo,
|
||||
CaptchaResponse,
|
||||
ApiResponse,
|
||||
RefreshTokenRequest,
|
||||
ChangePasswordRequest,
|
||||
ForgotPasswordRequest,
|
||||
ResetPasswordRequest,
|
||||
UserInfo
|
||||
SendCodeRequest,
|
||||
VerifyCodeRequest,
|
||||
OAuthLoginRequest,
|
||||
BindOAuthRequest,
|
||||
UnbindOAuthRequest,
|
||||
OAuthInfo,
|
||||
LoginHistory,
|
||||
OnlineUser
|
||||
} from '@/types/auth'
|
||||
|
||||
export const authService = {
|
||||
// 获取验证码
|
||||
async getCaptcha(): Promise<CaptchaResponse> {
|
||||
return await request.get('/auth/captcha')
|
||||
},
|
||||
|
||||
// 用户登录
|
||||
async login(data: LoginRequest): Promise<LoginResponse> {
|
||||
return await request.post('/auth/login', data)
|
||||
},
|
||||
|
||||
// 用户注册
|
||||
async register(data: RegisterRequest): Promise<LoginResponse> {
|
||||
return await request.post('/auth/register', data)
|
||||
},
|
||||
|
||||
// 刷新token
|
||||
async refreshToken(data: RefreshTokenRequest): Promise<ApiResponse<LoginResponse>> {
|
||||
const response = await request.post('/auth/refresh-token', data)
|
||||
/**
|
||||
* 认证API服务类
|
||||
*/
|
||||
export class AuthService {
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
static async login(data: LoginRequest): Promise<AuthResponse> {
|
||||
const response = await http.post<AuthResponse>('/auth/login', data)
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
// 用户登出
|
||||
async logout(): Promise<ApiResponse<void>> {
|
||||
const response = await request.post('/auth/logout')
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
static async register(data: RegisterRequest): Promise<AuthResponse> {
|
||||
const response = await http.post<AuthResponse>('/auth/register', data)
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
async getUserInfo(): Promise<ApiResponse<UserInfo>> {
|
||||
const response = await request.get('/auth/user-info')
|
||||
/**
|
||||
* 获取验证码
|
||||
*/
|
||||
static async getCaptcha(): Promise<CaptchaResponse> {
|
||||
const response = await http.get<CaptchaResponse>('/auth/captcha')
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
async changePassword(data: ChangePasswordRequest): Promise<ApiResponse<void>> {
|
||||
const response = await request.post('/auth/change-password', data)
|
||||
/**
|
||||
* 用户登出
|
||||
*/
|
||||
static async logout(): Promise<void> {
|
||||
const response = await http.post<void>('/auth/logout')
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
// 忘记密码
|
||||
async forgotPassword(data: ForgotPasswordRequest): Promise<ApiResponse<void>> {
|
||||
return await request.post('/forgot-password', data)
|
||||
},
|
||||
/**
|
||||
* 刷新Token
|
||||
*/
|
||||
static async refreshToken(data: RefreshTokenRequest): Promise<AuthResponse> {
|
||||
const response = await http.post<AuthResponse>('/auth/refresh-token', data)
|
||||
return response.data
|
||||
}
|
||||
|
||||
// 重置密码
|
||||
async resetPassword(data: ResetPasswordRequest): Promise<ApiResponse<void>> {
|
||||
return await request.post('/reset-password', data)
|
||||
},
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
static async getCurrentUserInfo(): Promise<UserInfo> {
|
||||
const response = await http.get<UserInfo>('/auth/user/info')
|
||||
return response.data
|
||||
}
|
||||
|
||||
// 验证token有效性
|
||||
async validateToken(): Promise<ApiResponse<boolean>> {
|
||||
return await request.get('/validate-token')
|
||||
},
|
||||
/**
|
||||
* 验证Token是否有效
|
||||
*/
|
||||
static async validateToken(): Promise<boolean> {
|
||||
const response = await http.get<boolean>('/auth/validate-token')
|
||||
return response.data
|
||||
}
|
||||
|
||||
// 检查账号是否存在
|
||||
async checkAccount(account: string): Promise<ApiResponse<boolean>> {
|
||||
return await request.get(`/check-account?account=${account}`)
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
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/reset-password', data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*/
|
||||
static async sendCode(data: SendCodeRequest): Promise<void> {
|
||||
return http.post<void>('/auth/send-code', data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证验证码
|
||||
*/
|
||||
static async verifyCode(data: VerifyCodeRequest): Promise<boolean> {
|
||||
return http.post<boolean>('/auth/verify-code', data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三方登录
|
||||
*/
|
||||
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 const authUtils = {
|
||||
// 获取token
|
||||
getToken(): string | null {
|
||||
return localStorage.getItem('token')
|
||||
},
|
||||
|
||||
// 设置token
|
||||
setToken(token: string): void {
|
||||
localStorage.setItem('token', token)
|
||||
},
|
||||
|
||||
// 移除token
|
||||
removeToken(): void {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('userInfo')
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo(): UserInfo | null {
|
||||
const userInfo = localStorage.getItem('userInfo')
|
||||
return userInfo ? JSON.parse(userInfo) : null
|
||||
},
|
||||
|
||||
// 设置用户信息
|
||||
setUserInfo(userInfo: UserInfo): void {
|
||||
localStorage.setItem('userInfo', JSON.stringify(userInfo))
|
||||
},
|
||||
|
||||
// 检查是否已登录
|
||||
isLoggedIn(): boolean {
|
||||
return !!this.getToken()
|
||||
},
|
||||
|
||||
// 清除所有认证信息
|
||||
clearAuth(): void {
|
||||
this.removeToken()
|
||||
}
|
||||
}
|
||||
// 导出默认实例
|
||||
export default AuthService
|
||||
|
||||
Reference in New Issue
Block a user