统一使用index.ts中的全局axios配置 移除了所有重复的API_BASE_URL定义 简化了所有API调用路径格式 提高了代码一致性和可维护性 确保所有API功能保持正常
106 lines
2.4 KiB
TypeScript
106 lines
2.4 KiB
TypeScript
import axios from 'axios';
|
|
import type { User } from '../../share/types.ts';
|
|
|
|
// 为UserAPI添加的接口响应类型
|
|
interface UsersResponse {
|
|
data: User[];
|
|
pagination: {
|
|
total: number;
|
|
current: number;
|
|
pageSize: number;
|
|
totalPages: number;
|
|
};
|
|
}
|
|
|
|
export interface UserResponse {
|
|
data: User;
|
|
message?: string;
|
|
}
|
|
|
|
interface UserCreateResponse {
|
|
message: string;
|
|
data: User;
|
|
}
|
|
|
|
interface UserUpdateResponse {
|
|
message: string;
|
|
data: User;
|
|
}
|
|
|
|
interface UserDeleteResponse {
|
|
message: string;
|
|
id: number;
|
|
}
|
|
|
|
// 用户管理API
|
|
export const UserAPI = {
|
|
// 获取用户列表
|
|
getUsers: async (params?: { page?: number, limit?: number, search?: string }): Promise<UsersResponse> => {
|
|
try {
|
|
const response = await axios.get('/users', { params });
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 获取单个用户详情
|
|
getUser: async (userId: number): Promise<UserResponse> => {
|
|
try {
|
|
const response = await axios.get(`/users/${userId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 创建用户
|
|
createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
|
|
try {
|
|
const response = await axios.post('/users', userData);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 更新用户信息
|
|
updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
|
|
try {
|
|
const response = await axios.put(`/users/${userId}`, userData);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 删除用户
|
|
deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
|
|
try {
|
|
const response = await axios.delete(`/users/${userId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 获取当前用户信息
|
|
getCurrentUser: async (): Promise<UserResponse> => {
|
|
try {
|
|
const response = await axios.get('/users/me/profile');
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 更新当前用户信息
|
|
updateCurrentUser: async (userData: Partial<User>): Promise<UserUpdateResponse> => {
|
|
try {
|
|
const response = await axios.put('/users/me/profile', userData);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}; |