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方法封装
export const api = {
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> =>
apiRequest<T>(endpoint, { ...options, method: 'GET' }),
get: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'GET' }),
post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
apiRequest<T>(endpoint, {
post: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<ApiResponse<T>>(endpoint, {
...options,
method: 'POST',
body: data ? JSON.stringify(data) : undefined,
}),
put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
apiRequest<T>(endpoint, {
put: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<ApiResponse<T>>(endpoint, {
...options,
method: 'PUT',
body: data ? JSON.stringify(data) : undefined,
}),
patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<T> =>
apiRequest<T>(endpoint, {
patch: <T = any>(endpoint: string, data?: any, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<ApiResponse<T>>(endpoint, {
...options,
method: 'PATCH',
body: data ? JSON.stringify(data) : undefined,
}),
delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<T> =>
apiRequest<T>(endpoint, { ...options, method: 'DELETE' }),
delete: <T = any>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>> =>
apiRequest<ApiResponse<T>>(endpoint, { ...options, method: 'DELETE' }),
};
export type ApiResponse<T> = {
code: number;
message: string;
data: T;
}
export default api;

View File

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