fix(client): 接口结果修正

This commit is contained in:
dayjoy
2025-09-26 16:57:09 +08:00
parent b78a07f192
commit 999a8e2520
2 changed files with 21 additions and 13 deletions

View File

@@ -54,32 +54,38 @@ export const apiRequest = async <T = any>(
// 便捷的HTTP方法封装 // 便捷的HTTP方法封装
export const api = { export const api = {
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> => get: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<T>(endpoint, { ...options, method: 'GET' }), apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'GET' }),
post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> => post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<T>(endpoint, { apiRequest<ApiResponse<T>>(endpoint, {
...options, ...options,
method: 'POST', method: 'POST',
body: data ? JSON.stringify(data) : undefined, body: data ? JSON.stringify(data) : undefined,
}), }),
put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> => put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<T>(endpoint, { apiRequest<ApiResponse<T>>(endpoint, {
...options, ...options,
method: 'PUT', method: 'PUT',
body: data ? JSON.stringify(data) : undefined, body: data ? JSON.stringify(data) : undefined,
}), }),
patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> => patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<T>(endpoint, { apiRequest<ApiResponse<T>>(endpoint, {
...options, ...options,
method: 'PATCH', method: 'PATCH',
body: data ? JSON.stringify(data) : undefined, body: data ? JSON.stringify(data) : undefined,
}), }),
delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> => delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<T>(endpoint, { ...options, method: 'DELETE' }), apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'DELETE' }),
}; };
export type ApiResponse<T> = {
code: number;
message: string;
data: T;
}
export default api; export default api;

View File

@@ -14,13 +14,15 @@ export type UserInfo = {
/** /**
* 获取当前用户的信息 * 获取当前用户的信息
*/ */
export const getUserInfo = async (): Promise<UserInfo> => { export const getUserInfo = async (): Promise<UserInfo | null> => {
return await api.get<UserInfo>('/api/user/info'); const res = await api.get<UserInfo>('/api/user/info');
return res.code === 200 ? res.data : null;
}; };
/** /**
* 获取群组内所有用户的信息 * 获取群组内所有用户的信息
*/ */
export const getGroupUsers = async (): Promise<UserInfo[]> => { export const getGroupUsers = async (): Promise<UserInfo[]> => {
return await api.get<UserInfo[]>('/api/group/members'); const res = await api.get<UserInfo[]>('/api/group/members');
return res.code === 200 ? res.data || [] : [];
}; };