2025-04-10 11:54:21 +08:00
|
|
|
|
import { apiRequest, ApiResponse } from './api-utils';
|
|
|
|
|
|
|
2025-04-09 17:21:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 菜单项接口
|
|
|
|
|
|
*/
|
|
|
|
|
|
export interface MenuItem {
|
|
|
|
|
|
id: number;
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
path: string;
|
|
|
|
|
|
icon?: string;
|
|
|
|
|
|
parent_id: number;
|
|
|
|
|
|
status: number;
|
|
|
|
|
|
sort: number;
|
|
|
|
|
|
children?: MenuItem[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* 获取菜单树
|
2025-04-09 17:21:29 +08:00
|
|
|
|
* @param onlyEnabled 是否只获取启用的菜单
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* @returns 菜单树
|
2025-04-09 17:21:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export async function getMenus(onlyEnabled: boolean = true): Promise<MenuItem[]> {
|
|
|
|
|
|
try {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
// 构建查询参数
|
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
params.append('only_enabled', onlyEnabled ? '1' : '0');
|
2025-04-09 17:21:29 +08:00
|
|
|
|
|
2025-04-10 11:54:21 +08:00
|
|
|
|
const response = await apiRequest<MenuItem[]>(`/menu/tree?${params.toString()}`);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
|
2025-04-10 11:54:21 +08:00
|
|
|
|
return response.data || [];
|
2025-04-09 17:21:29 +08:00
|
|
|
|
} catch (error) {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
console.error('获取菜单树失败:', error);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* 获取菜单列表
|
|
|
|
|
|
* @param page 页码
|
|
|
|
|
|
* @param limit 每页数量
|
|
|
|
|
|
* @returns 菜单列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function getMenuList(
|
|
|
|
|
|
page: number = 1,
|
|
|
|
|
|
limit: number = 20
|
|
|
|
|
|
): Promise<ApiResponse<{
|
|
|
|
|
|
list: MenuItem[];
|
|
|
|
|
|
total: number;
|
|
|
|
|
|
page: number;
|
|
|
|
|
|
limit: number;
|
|
|
|
|
|
}>> {
|
|
|
|
|
|
// 构建查询参数
|
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
params.append('page', page.toString());
|
|
|
|
|
|
params.append('limit', limit.toString());
|
|
|
|
|
|
|
|
|
|
|
|
return apiRequest(`/menu/list?${params.toString()}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 保存菜单(新增或更新)
|
2025-04-09 17:21:29 +08:00
|
|
|
|
* @param menuData 菜单数据
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* @returns 保存结果
|
2025-04-09 17:21:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export async function saveMenu(menuData: Partial<MenuItem>): Promise<boolean> {
|
|
|
|
|
|
try {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
const response = await apiRequest('/menu/save', 'POST', menuData);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
|
2025-04-10 11:54:21 +08:00
|
|
|
|
return response.code === 200;
|
2025-04-09 17:21:29 +08:00
|
|
|
|
} catch (error) {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
console.error('保存菜单失败:', error);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除菜单
|
|
|
|
|
|
* @param id 菜单ID
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* @returns 删除结果
|
2025-04-09 17:21:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export async function deleteMenu(id: number): Promise<boolean> {
|
|
|
|
|
|
try {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
const response = await apiRequest(`/menu/delete/${id}`, 'DELETE');
|
2025-04-09 17:21:29 +08:00
|
|
|
|
|
2025-04-10 11:54:21 +08:00
|
|
|
|
return response.code === 200;
|
2025-04-09 17:21:29 +08:00
|
|
|
|
} catch (error) {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
console.error('删除菜单失败:', error);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新菜单状态
|
|
|
|
|
|
* @param id 菜单ID
|
2025-04-10 11:54:21 +08:00
|
|
|
|
* @param status 状态:1启用,0禁用
|
|
|
|
|
|
* @returns 更新结果
|
2025-04-09 17:21:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export async function updateMenuStatus(id: number, status: 0 | 1): Promise<boolean> {
|
|
|
|
|
|
try {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
const response = await apiRequest('/menu/status', 'POST', {
|
|
|
|
|
|
id,
|
|
|
|
|
|
status
|
2025-04-09 17:21:29 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-04-10 11:54:21 +08:00
|
|
|
|
return response.code === 200;
|
2025-04-09 17:21:29 +08:00
|
|
|
|
} catch (error) {
|
2025-04-10 11:54:21 +08:00
|
|
|
|
console.error('更新菜单状态失败:', error);
|
2025-04-09 17:21:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|