user.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * Account cancellation / destroy
  67. * 对应后端接口:POST /user/destroy
  68. * 按后端要求使用 application/x-www-form-urlencoded
  69. * 注意:暂不传递额外的 body 参数(仅前端展示)
  70. */
  71. destroy() {
  72. return http.post<object>('/user/destroy', undefined, {
  73. headers: {
  74. 'Content-Type': 'application/x-www-form-urlencoded',
  75. },
  76. })
  77. },
  78. /**
  79. * Renew token - refresh authentication token
  80. * @returns Token renewal response
  81. */
  82. renewalToken() {
  83. return http.post<TokenRenewalVO>('/user/renewalToken')
  84. },
  85. /**
  86. * Edit my user information
  87. * @param params - User info to update
  88. * @returns Empty response on success
  89. */
  90. editMyInfo(params: UserInfoEditDTO) {
  91. return http.post<object>('/user/my/info/edit', params)
  92. },
  93. /**
  94. * Rate playmate charm star
  95. * @param params - Rating payload (userNo, star)
  96. * @returns Empty response on success
  97. */
  98. ratePlaymateCharm(params: PlaymateCharmStarDTO) {
  99. return http.post<object>('/user/playmate/charmStar', params)
  100. },
  101. /**
  102. * Get my last order info as a playmate
  103. * 对应后端接口:POST /playmate/mylast/orderinfo
  104. * @returns Last order info for current playmate
  105. */
  106. getMyLastPlaymateOrderInfo() {
  107. return http.post<PlaymateLastOrderInfoVO>('/playmate/mylast/orderinfo')
  108. },
  109. /**
  110. * Batch get user infos by userNos (max 20 per request)
  111. * 对应后端接口:POST /user/get/infos
  112. */
  113. getUserInfos(userNos: string[]) {
  114. return http.post<UserInfosVO>('/user/get/infos', { userNos })
  115. },
  116. /**
  117. * Batch get users online state by userNos (max 20 per request)
  118. * 对应后端接口:POST /user/getUsersOnlineState
  119. */
  120. getUsersOnlineState(userNos: string[]) {
  121. return http.post<UsersOnlineStateVO>('/user/getUsersOnlineState', { userNos })
  122. },
  123. }
  124. /**
  125. * Usage examples:
  126. *
  127. * 1. Get user info:
  128. * ```typescript
  129. * import { userApi } from '~/api/user'
  130. *
  131. * const getUserInfo = async () => {
  132. * try {
  133. * const userInfo = await userApi.getMyInfo()
  134. * console.log(userInfo)
  135. * } catch (error) {
  136. * console.error('Failed to get user info:', error)
  137. * }
  138. * }
  139. * ```
  140. *
  141. * 2. Google login:
  142. * ```typescript
  143. * import { userApi } from '~/api/user'
  144. *
  145. * const handleGoogleLogin = async (googleData: string) => {
  146. * try {
  147. * const loginResult = await userApi.loginWithGoogle({ data: googleData })
  148. * console.log('Login success:', loginResult)
  149. * // Save token and user info
  150. * } catch (error) {
  151. * console.error('Login failed:', error)
  152. * }
  153. * }
  154. * ```
  155. *
  156. * 3. Logout:
  157. * ```typescript
  158. * import { userApi } from '~/api/user'
  159. *
  160. * const handleLogout = async () => {
  161. * try {
  162. * await userApi.logout()
  163. * console.log('Logout success')
  164. * // Clear local storage and redirect to login
  165. * } catch (error) {
  166. * console.error('Logout failed:', error)
  167. * }
  168. * }
  169. * ```
  170. *
  171. * 4. Use with useApi composable for reactive state:
  172. * ```typescript
  173. * import { userApi } from '~/api/user'
  174. * import { useApi } from '~/composables/useApi'
  175. *
  176. * const { loading, error, request } = useApi()
  177. *
  178. * const getUserInfo = async () => {
  179. * const result = await request(() => userApi.getMyInfo())
  180. * if (result) {
  181. * console.log('User info:', result)
  182. * }
  183. * }
  184. * ```
  185. */