| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /**
- * User API module
- */
- import { http } from '~/utils/request'
- import type {
- PlaymateInfoVO,
- UserInfosVO,
- UserInfoVO,
- UserLoginVO,
- UserThirdLoginDTO,
- TokenRenewalVO,
- UserInfoEditDTO,
- PlaymateLastOrderInfoVO,
- UsersOnlineStateVO,
- } from '~/types/api'
- /**
- * Playmate charm rating request payload
- */
- interface PlaymateCharmStarDTO {
- userNo: string
- star: number
- }
- /**
- * User API endpoints
- */
- export const userApi = {
- /**
- * Get my user information
- * @returns User info
- */
- getMyInfo() {
- return http.post<UserInfoVO>('/user/my/info')
- },
- /**
- * Get playmate profile information
- * @param id - playmate user id (userNo)
- * @returns Playmate profile info
- */
- getPlaymateInfo(id: string) {
- return http.post<PlaymateInfoVO>('/user/playmate/info', { id })
- },
- /**
- * Google login
- * @param params - Login params containing Google auth data
- * @returns Login response with user info and token
- */
- loginWithGoogle(params: UserThirdLoginDTO) {
- return http.post<UserLoginVO>('/user/login/google/enter', params)
- },
- /**
- * Test environment email login
- * @param email - User email for test login
- * @returns Login response with user info and token
- */
- loginWithEmail(email: string) {
- return http.post<UserLoginVO>('/user/login/email/enter', { email })
- },
- /**
- * User logout
- * @returns Empty response
- */
- logout() {
- return http.post<object>('/user/logout')
- },
- /**
- * Account cancellation / destroy
- * 对应后端接口:POST /user/destroy
- * 按后端要求使用 application/x-www-form-urlencoded
- * 注意:暂不传递额外的 body 参数(仅前端展示)
- */
- destroy() {
- return http.post<object>('/user/destroy', undefined, {
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- },
- })
- },
- /**
- * Renew token - refresh authentication token
- * @returns Token renewal response
- */
- renewalToken() {
- return http.post<TokenRenewalVO>('/user/renewalToken')
- },
- /**
- * Edit my user information
- * @param params - User info to update
- * @returns Empty response on success
- */
- editMyInfo(params: UserInfoEditDTO) {
- return http.post<object>('/user/my/info/edit', params)
- },
- /**
- * Rate playmate charm star
- * @param params - Rating payload (userNo, star)
- * @returns Empty response on success
- */
- ratePlaymateCharm(params: PlaymateCharmStarDTO) {
- return http.post<object>('/user/playmate/charmStar', params)
- },
- /**
- * Get my last order info as a playmate
- * 对应后端接口:POST /playmate/mylast/orderinfo
- * @returns Last order info for current playmate
- */
- getMyLastPlaymateOrderInfo() {
- return http.post<PlaymateLastOrderInfoVO>('/playmate/mylast/orderinfo')
- },
- /**
- * Batch get user infos by userNos (max 20 per request)
- * 对应后端接口:POST /user/get/infos
- */
- getUserInfos(userNos: string[]) {
- return http.post<UserInfosVO>('/user/get/infos', { userNos })
- },
- /**
- * Batch get users online state by userNos (max 20 per request)
- * 对应后端接口:POST /user/getUsersOnlineState
- */
- getUsersOnlineState(userNos: string[]) {
- return http.post<UsersOnlineStateVO>('/user/getUsersOnlineState', { userNos })
- },
- }
- /**
- * Usage examples:
- *
- * 1. Get user info:
- * ```typescript
- * import { userApi } from '~/api/user'
- *
- * const getUserInfo = async () => {
- * try {
- * const userInfo = await userApi.getMyInfo()
- * console.log(userInfo)
- * } catch (error) {
- * console.error('Failed to get user info:', error)
- * }
- * }
- * ```
- *
- * 2. Google login:
- * ```typescript
- * import { userApi } from '~/api/user'
- *
- * const handleGoogleLogin = async (googleData: string) => {
- * try {
- * const loginResult = await userApi.loginWithGoogle({ data: googleData })
- * console.log('Login success:', loginResult)
- * // Save token and user info
- * } catch (error) {
- * console.error('Login failed:', error)
- * }
- * }
- * ```
- *
- * 3. Logout:
- * ```typescript
- * import { userApi } from '~/api/user'
- *
- * const handleLogout = async () => {
- * try {
- * await userApi.logout()
- * console.log('Logout success')
- * // Clear local storage and redirect to login
- * } catch (error) {
- * console.error('Logout failed:', error)
- * }
- * }
- * ```
- *
- * 4. Use with useApi composable for reactive state:
- * ```typescript
- * import { userApi } from '~/api/user'
- * import { useApi } from '~/composables/useApi'
- *
- * const { loading, error, request } = useApi()
- *
- * const getUserInfo = async () => {
- * const result = await request(() => userApi.getMyInfo())
- * if (result) {
- * console.log('User info:', result)
- * }
- * }
- * ```
- */
|