fetch(modify):修改bug
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 5m20s
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 5m20s
This commit is contained in:
@@ -20,7 +20,7 @@ import { isInWhiteList, hasRoutePermission, isTokenValid, buildLoginRedirect } f
|
||||
const isRouteRegistered = ref(false)
|
||||
|
||||
// 临时开发模式:跳过所有权限验证(开发静态页面时使用)
|
||||
const DEV_MODE_SKIP_AUTH = true
|
||||
const DEV_MODE_SKIP_AUTH = false
|
||||
|
||||
/**
|
||||
* 路由全局前置守卫
|
||||
@@ -232,20 +232,125 @@ async function processFrontendMenu(router: Router): Promise<void> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理后端控制模式的菜单逻辑
|
||||
* 处理后端控制模式的菜单
|
||||
*/
|
||||
async function processBackendMenu(router: Router): Promise<void> {
|
||||
const closeLoading = loadingService.showLoading()
|
||||
|
||||
try {
|
||||
const { menuList } = await menuService.getMenuList()
|
||||
await registerAndStoreMenu(router, menuList, closeLoading)
|
||||
const userStore = useUserStore()
|
||||
const backendMenus = userStore.menus || []
|
||||
const routeMap = buildRouteMap(asyncRoutes)
|
||||
|
||||
const menuList = backendMenus
|
||||
.map((menu) => convertBackendMenuToRoute(menu, routeMap))
|
||||
.filter((route) => route !== null)
|
||||
|
||||
const finalMenuList = mergeDefaultMenus(menuList, routeMap)
|
||||
await registerAndStoreMenu(router, finalMenuList, closeLoading)
|
||||
} catch (error) {
|
||||
closeLoading()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并默认菜单
|
||||
* 将配置的默认菜单合并到后端返回的菜单中
|
||||
*/
|
||||
function mergeDefaultMenus(
|
||||
backendMenus: AppRouteRecord[],
|
||||
routeMap: Map<string, AppRouteRecord>
|
||||
): AppRouteRecord[] {
|
||||
const defaultMenuPaths = ['/dashboard']
|
||||
|
||||
const defaultMenus: AppRouteRecord[] = defaultMenuPaths
|
||||
.map((path) => {
|
||||
const route = routeMap.get(path)
|
||||
return route ? menuDataToRouter(route) : null
|
||||
})
|
||||
.filter((menu): menu is AppRouteRecord => menu !== null)
|
||||
|
||||
const backendPaths = new Set(backendMenus.map((m) => m.path))
|
||||
const filteredDefaultMenus = defaultMenus.filter((m) => !backendPaths.has(m.path))
|
||||
|
||||
return [...filteredDefaultMenus, ...backendMenus]
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 URL 到路由的映射表
|
||||
*/
|
||||
function buildRouteMap(routes: AppRouteRecord[], parentPath = ''): Map<string, AppRouteRecord> {
|
||||
const map = new Map<string, AppRouteRecord>()
|
||||
|
||||
routes.forEach((route) => {
|
||||
// 构建完整路径
|
||||
const fullPath = route.path.startsWith('/')
|
||||
? route.path
|
||||
: parentPath
|
||||
? `${parentPath}/${route.path}`.replace(/\/+/g, '/')
|
||||
: `/${route.path}`
|
||||
|
||||
// 存储路由映射
|
||||
map.set(fullPath, route)
|
||||
|
||||
// 递归处理子路由
|
||||
if (route.children && route.children.length > 0) {
|
||||
const childMap = buildRouteMap(route.children, fullPath)
|
||||
childMap.forEach((childRoute, childPath) => {
|
||||
map.set(childPath, childRoute)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将后端菜单数据转换为路由格式
|
||||
*/
|
||||
function convertBackendMenuToRoute(
|
||||
menu: any,
|
||||
routeMap: Map<string, AppRouteRecord>,
|
||||
parentPath = ''
|
||||
): AppRouteRecord | null {
|
||||
const menuUrl = menu.url || '/'
|
||||
const matchedRoute = routeMap.get(menuUrl)
|
||||
|
||||
if (!matchedRoute) {
|
||||
console.warn(`未找到与菜单 URL "${menuUrl}" 匹配的路由定义: ${menu.name}`)
|
||||
return null
|
||||
}
|
||||
|
||||
const route: AppRouteRecord = {
|
||||
path: menuUrl,
|
||||
name: matchedRoute.name,
|
||||
component: matchedRoute.component,
|
||||
meta: {
|
||||
...matchedRoute.meta,
|
||||
title: menu.name,
|
||||
permission: menu.perm_code,
|
||||
sort: menu.sort || matchedRoute.meta?.sort || 0,
|
||||
icon: menu.icon || matchedRoute.meta?.icon,
|
||||
// 清除前端定义的 roles 和 permissions,使用后端权限控制
|
||||
roles: undefined,
|
||||
permissions: undefined
|
||||
}
|
||||
}
|
||||
|
||||
if (menu.children && menu.children.length > 0) {
|
||||
const children = menu.children
|
||||
.map((child: any) => convertBackendMenuToRoute(child, routeMap, menuUrl))
|
||||
.filter((child: AppRouteRecord | null) => child !== null)
|
||||
|
||||
if (children.length > 0) {
|
||||
route.children = children
|
||||
}
|
||||
}
|
||||
|
||||
return route
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册路由并存储菜单数据
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user