创建了api目录并拆分为10个功能模块文件
建立了index.ts统一导出 精简了原api.ts文件
This commit is contained in:
108
client/mobile/api/user.ts
Normal file
108
client/mobile/api/user.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import axios from 'axios';
|
||||
import type { User } from '../../share/types.ts';
|
||||
|
||||
const API_BASE_URL = '/api';
|
||||
|
||||
// 为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(`${API_BASE_URL}/users`, { params });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取单个用户详情
|
||||
getUser: async (userId: number): Promise<UserResponse> => {
|
||||
try {
|
||||
const response = await axios.get(`${API_BASE_URL}/users/${userId}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 创建用户
|
||||
createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
|
||||
try {
|
||||
const response = await axios.post(`${API_BASE_URL}/users`, userData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 更新用户信息
|
||||
updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
|
||||
try {
|
||||
const response = await axios.put(`${API_BASE_URL}/users/${userId}`, userData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 删除用户
|
||||
deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
|
||||
try {
|
||||
const response = await axios.delete(`${API_BASE_URL}/users/${userId}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取当前用户信息
|
||||
getCurrentUser: async (): Promise<UserResponse> => {
|
||||
try {
|
||||
const response = await axios.get(`${API_BASE_URL}/users/me/profile`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// 更新当前用户信息
|
||||
updateCurrentUser: async (userData: Partial<User>): Promise<UserUpdateResponse> => {
|
||||
try {
|
||||
const response = await axios.put(`${API_BASE_URL}/users/me/profile`, userData);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user