user.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * User API module
  3. */
  4. import { http } from '~/utils/request'
  5. import type {
  6. PlaymateInfoVO,
  7. UserInfosVO,
  8. UserInfoVO,
  9. UserLoginVO,
  10. UserThirdLoginDTO,
  11. TokenRenewalVO,
  12. UserInfoEditDTO,
  13. PlaymateLastOrderInfoVO,
  14. UsersOnlineStateVO,
  15. } from '~/types/api'
  16. /**
  17. * Playmate charm rating request payload
  18. */
  19. interface PlaymateCharmStarDTO {
  20. userNo: string
  21. star: number
  22. }
  23. /**
  24. * User API endpoints
  25. */
  26. export const userApi = {
  27. /**
  28. * Get my user information
  29. * @returns User info
  30. */
  31. getMyInfo() {
  32. return http.post<UserInfoVO>('/user/my/info')
  33. },
  34. /**
  35. * Get playmate profile information
  36. * @param id - playmate user id (userNo)
  37. * @returns Playmate profile info
  38. */
  39. getPlaymateInfo(id: string) {
  40. return http.post<PlaymateInfoVO>('/user/playmate/info', { id })
  41. },
  42. /**
  43. * Google login
  44. * @param params - Login params containing Google auth data
  45. * @returns Login response with user info and token
  46. */
  47. loginWithGoogle(params: UserThirdLoginDTO) {
  48. return http.post<UserLoginVO>('/user/login/google/enter', params)
  49. },
  50. /**
  51. * Test environment email login
  52. * @param email - User email for test login
  53. * @returns Login response with user info and token
  54. */
  55. loginWithEmail(email: string) {
  56. return http.post<UserLoginVO>('/user/login/email/enter', { email })
  57. },
  58. /**
  59. * User logout
  60. * @returns Empty response
  61. */
  62. logout() {
  63. return http.post<object>('/user/logout')
  64. },
  65. /**
  66. * Renew token - refresh authentication token
  67. * @returns Token renewal response
  68. */
  69. renewalToken() {
  70. return http.post<TokenRenewalVO>('/user/renewalToken')
  71. },
  72. /**
  73. * Edit my user information
  74. * @param params - User info to update
  75. * @returns Empty response on success
  76. */
  77. editMyInfo(params: UserInfoEditDTO) {
  78. return http.post<object>('/user/my/info/edit', params)
  79. },
  80. /**
  81. * Rate playmate charm star
  82. * @param params - Rating payload (userNo, star)
  83. * @returns Empty response on success
  84. */
  85. ratePlaymateCharm(params: PlaymateCharmStarDTO) {
  86. return http.post<object>('/user/playmate/charmStar', params)
  87. },
  88. /**
  89. * Get my last order info as a playmate
  90. * 对应后端接口:POST /playmate/mylast/orderinfo
  91. * @returns Last order info for current playmate
  92. */
  93. getMyLastPlaymateOrderInfo() {
  94. return http.post<PlaymateLastOrderInfoVO>('/playmate/mylast/orderinfo')
  95. },
  96. /**
  97. * Batch get user infos by userNos (max 20 per request)
  98. * 对应后端接口:POST /user/get/infos
  99. */
  100. getUserInfos(userNos: string[]) {
  101. return http.post<UserInfosVO>('/user/get/infos', { userNos })
  102. },
  103. /**
  104. * Batch get users online state by userNos (max 20 per request)
  105. * 对应后端接口:POST /user/getUsersOnlineState
  106. */
  107. getUsersOnlineState(userNos: string[]) {
  108. return http.post<UsersOnlineStateVO>('/user/getUsersOnlineState', { userNos })
  109. },
  110. }
  111. /**
  112. * Usage examples:
  113. *
  114. * 1. Get user info:
  115. * ```typescript
  116. * import { userApi } from '~/api/user'
  117. *
  118. * const getUserInfo = async () => {
  119. * try {
  120. * const userInfo = await userApi.getMyInfo()
  121. * console.log(userInfo)
  122. * } catch (error) {
  123. * console.error('Failed to get user info:', error)
  124. * }
  125. * }
  126. * ```
  127. *
  128. * 2. Google login:
  129. * ```typescript
  130. * import { userApi } from '~/api/user'
  131. *
  132. * const handleGoogleLogin = async (googleData: string) => {
  133. * try {
  134. * const loginResult = await userApi.loginWithGoogle({ data: googleData })
  135. * console.log('Login success:', loginResult)
  136. * // Save token and user info
  137. * } catch (error) {
  138. * console.error('Login failed:', error)
  139. * }
  140. * }
  141. * ```
  142. *
  143. * 3. Logout:
  144. * ```typescript
  145. * import { userApi } from '~/api/user'
  146. *
  147. * const handleLogout = async () => {
  148. * try {
  149. * await userApi.logout()
  150. * console.log('Logout success')
  151. * // Clear local storage and redirect to login
  152. * } catch (error) {
  153. * console.error('Logout failed:', error)
  154. * }
  155. * }
  156. * ```
  157. *
  158. * 4. Use with useApi composable for reactive state:
  159. * ```typescript
  160. * import { userApi } from '~/api/user'
  161. * import { useApi } from '~/composables/useApi'
  162. *
  163. * const { loading, error, request } = useApi()
  164. *
  165. * const getUserInfo = async () => {
  166. * const result = await request(() => userApi.getMyInfo())
  167. * if (result) {
  168. * console.log('User info:', result)
  169. * }
  170. * }
  171. * ```
  172. */