user.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import request from './request'
  2. import type { ApiResponse } from '@/types/auth'
  3. export interface UserProfile {
  4. id: string
  5. username: string
  6. email: string
  7. phone?: string
  8. real_name?: string
  9. company?: string
  10. department?: string
  11. position?: string
  12. avatar_url?: string
  13. is_active: boolean
  14. is_superuser: boolean
  15. created_at: string
  16. updated_at: string
  17. last_login_at?: string
  18. roles?: string[]
  19. }
  20. export interface UpdateProfileRequest {
  21. email: string
  22. phone?: string
  23. real_name?: string
  24. company?: string
  25. department?: string
  26. position?: string
  27. }
  28. export interface ChangePasswordRequest {
  29. old_password: string
  30. new_password: string
  31. }
  32. export interface LoginLog {
  33. id: string
  34. username: string
  35. login_type: string
  36. ip_address: string
  37. user_agent: string
  38. success: boolean
  39. failure_reason?: string
  40. login_at: string
  41. }
  42. export interface UserToken {
  43. id: string
  44. app_name: string
  45. token_type: string
  46. scope: string
  47. expires_at: string
  48. created_at: string
  49. last_used_at?: string
  50. }
  51. export const userApi = {
  52. // 获取用户信息 - 使用auth模块的userinfo端点
  53. getProfile(): Promise<ApiResponse<UserProfile>> {
  54. return request.get('/api/v1/auth/userinfo')
  55. },
  56. // 更新用户信息
  57. updateProfile(data: UpdateProfileRequest): Promise<ApiResponse> {
  58. return request.put('/api/v1/system/users/profile', data)
  59. },
  60. // 修改密码
  61. changePassword(data: ChangePasswordRequest): Promise<ApiResponse> {
  62. return request.put('/api/v1/system/users/password', data)
  63. },
  64. // 上传头像
  65. uploadAvatar(file: File): Promise<ApiResponse<{ url: string }>> {
  66. const formData = new FormData()
  67. formData.append('avatar', file)
  68. return request.post('/api/v1/system/users/avatar', formData, {
  69. headers: {
  70. 'Content-Type': 'multipart/form-data'
  71. }
  72. })
  73. },
  74. // 获取登录日志
  75. getLoginLogs(params?: {
  76. page?: number
  77. page_size?: number
  78. start_time?: string
  79. end_time?: string
  80. }): Promise<ApiResponse<{
  81. items: LoginLog[]
  82. total: number
  83. page: number
  84. page_size: number
  85. }>> {
  86. return request.get('/api/v1/system/users/login-logs', { params })
  87. },
  88. // 获取用户令牌
  89. getTokens(params?: {
  90. page?: number
  91. page_size?: number
  92. }): Promise<ApiResponse<{
  93. items: UserToken[]
  94. total: number
  95. page: number
  96. page_size: number
  97. }>> {
  98. return request.get('/api/v1/system/users/tokens', { params })
  99. },
  100. // 撤销令牌
  101. revokeToken(tokenId: string): Promise<ApiResponse> {
  102. return request.delete(`/api/v1/system/users/tokens/${tokenId}`)
  103. },
  104. // 强制下线(撤销所有令牌)
  105. forceLogout(): Promise<ApiResponse> {
  106. return request.post('/api/v1/system/users/force-logout')
  107. }
  108. }