61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import http from '@/utils/http'
|
||
import { convertMenuData } from '@/utils/menuConverter'
|
||
|
||
/**
|
||
* 从新的API获取菜单树形数据,并转换为角色管理所需的格式
|
||
*/
|
||
export function getMenuTreeForRole(query?: { terminalType?: string }) {
|
||
return http.get<any[]>('/menu/tree', query).then((res) => {
|
||
// 假设响应格式为 { data: [...菜单数据], success: true, msg: "", code: 200 }
|
||
const data = res.data || []
|
||
// 转换菜单数据为角色管理组件需要的格式
|
||
const convertedData = convertMenuData(data)
|
||
return convertedData
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 转换菜单数据为带权限的树形结构
|
||
* @param menus 转换后的菜单数据
|
||
* @param selectedMenuIds 已选中的菜单ID集合
|
||
* @returns 带权限标记的菜单树
|
||
*/
|
||
export function transformMenusWithPermissions(menus: any[], selectedMenuIds: string[] = []) {
|
||
// 深拷贝菜单数据,避免修改原始数据
|
||
const result = JSON.parse(JSON.stringify(menus))
|
||
|
||
// 递归处理菜单树,添加权限标记
|
||
const processMenus = (items: any[]) => {
|
||
return items.map((item) => {
|
||
// 设置选中状态
|
||
item.isChecked = selectedMenuIds.includes(item.id.toString())
|
||
|
||
// 如果有子菜单,递归处理
|
||
if (item.children && item.children.length > 0) {
|
||
item.children = processMenus(item.children)
|
||
}
|
||
|
||
return item
|
||
})
|
||
}
|
||
|
||
return processMenus(result)
|
||
}
|
||
|
||
/**
|
||
* 获取角色已分配的菜单ID列表
|
||
*/
|
||
export function getRoleMenuIds(roleId: string) {
|
||
return http.get<string[]>(`/role/get-menus/${roleId}`)
|
||
}
|
||
|
||
/**
|
||
* 为角色分配菜单权限
|
||
*/
|
||
export function assignRoleMenus(roleId: string, menuIds: string[]) {
|
||
return http.post('/role/bind-menu', {
|
||
roleId,
|
||
menuIds,
|
||
})
|
||
}
|