/** * 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('/user/my/info') }, /** * Get playmate profile information * @param id - playmate user id (userNo) * @returns Playmate profile info */ getPlaymateInfo(id: string) { return http.post('/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('/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('/user/login/email/enter', { email }) }, /** * User logout * @returns Empty response */ logout() { return http.post('/user/logout') }, /** * Account cancellation / destroy * 对应后端接口:POST /user/destroy * 按后端要求使用 application/x-www-form-urlencoded * 注意:暂不传递额外的 body 参数(仅前端展示) */ destroy() { return http.post('/user/destroy', undefined, { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, }) }, /** * Renew token - refresh authentication token * @returns Token renewal response */ renewalToken() { return http.post('/user/renewalToken') }, /** * Edit my user information * @param params - User info to update * @returns Empty response on success */ editMyInfo(params: UserInfoEditDTO) { return http.post('/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('/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('/playmate/mylast/orderinfo') }, /** * Batch get user infos by userNos (max 20 per request) * 对应后端接口:POST /user/get/infos */ getUserInfos(userNos: string[]) { return http.post('/user/get/infos', { userNos }) }, /** * Batch get users online state by userNos (max 20 per request) * 对应后端接口:POST /user/getUsersOnlineState */ getUsersOnlineState(userNos: string[]) { return http.post('/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) * } * } * ``` */