From 999a8e2520d18a790cf97e90582705931928dc0a Mon Sep 17 00:00:00 2001 From: dayjoy Date: Fri, 26 Sep 2025 16:57:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(client):=20=E6=8E=A5=E5=8F=A3=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/client/src/api/index.ts | 26 ++++++++++++++++---------- packages/client/src/api/user.ts | 8 +++++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/client/src/api/index.ts b/packages/client/src/api/index.ts index ece297d..7a9703c 100644 --- a/packages/client/src/api/index.ts +++ b/packages/client/src/api/index.ts @@ -54,32 +54,38 @@ export const apiRequest = async ( // 便捷的HTTP方法封装 export const api = { - get: (endpoint: string, options?: RequestOptions): Promise => - apiRequest(endpoint, { ...options, method: 'GET' }), + get: (endpoint: string, options?: RequestOptions): Promise> => + apiRequest>(endpoint, { ...options, method: 'GET' }), - post: (endpoint: string, data?: any, options?: RequestOptions): Promise => - apiRequest(endpoint, { + post: (endpoint: string, data?: any, options?: RequestOptions): Promise> => + apiRequest>(endpoint, { ...options, method: 'POST', body: data ? JSON.stringify(data) : undefined, }), - put: (endpoint: string, data?: any, options?: RequestOptions): Promise => - apiRequest(endpoint, { + put: (endpoint: string, data?: any, options?: RequestOptions): Promise> => + apiRequest>(endpoint, { ...options, method: 'PUT', body: data ? JSON.stringify(data) : undefined, }), - patch: (endpoint: string, data?: any, options?: RequestOptions): Promise => - apiRequest(endpoint, { + patch: (endpoint: string, data?: any, options?: RequestOptions): Promise> => + apiRequest>(endpoint, { ...options, method: 'PATCH', body: data ? JSON.stringify(data) : undefined, }), - delete: (endpoint: string, options?: RequestOptions): Promise => - apiRequest(endpoint, { ...options, method: 'DELETE' }), + delete: (endpoint: string, options?: RequestOptions): Promise> => + apiRequest>(endpoint, { ...options, method: 'DELETE' }), }; +export type ApiResponse = { + code: number; + message: string; + data: T; +} + export default api; diff --git a/packages/client/src/api/user.ts b/packages/client/src/api/user.ts index 3d022df..da293d6 100644 --- a/packages/client/src/api/user.ts +++ b/packages/client/src/api/user.ts @@ -14,13 +14,15 @@ export type UserInfo = { /** * 获取当前用户的信息 */ -export const getUserInfo = async (): Promise => { - return await api.get('/api/user/info'); +export const getUserInfo = async (): Promise => { + const res = await api.get('/api/user/info'); + return res.code === 200 ? res.data : null; }; /** * 获取群组内所有用户的信息 */ export const getGroupUsers = async (): Promise => { - return await api.get('/api/group/members'); + const res = await api.get('/api/group/members'); + return res.code === 200 ? res.data || [] : []; };