Skip to content

Commit 3210178

Browse files
committed
路由 meta.auth 保持元信息默认合并行为,路由访问改为通过 route.matched 进行逐级鉴权
1 parent afe7b2e commit 3210178

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/App.vue

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import eventBus from './utils/eventBus'
66
import Provider from './ui-provider/index.vue'
77
import useSettingsStore from '@/store/modules/settings'
88
9+
const route = useRoute()
10+
911
const settingsStore = useSettingsStore()
1012
const { auth } = useAuth()
1113
14+
const isAuth = computed(() => {
15+
return route.matched.every((item) => {
16+
return auth(item.meta.auth ?? '')
17+
})
18+
})
19+
1220
// 侧边栏主导航当前实际宽度
1321
const mainSidebarActualWidth = computed(() => {
1422
let actualWidth = Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--g-main-sidebar-width'))
@@ -61,13 +69,13 @@ import.meta.env.VITE_APP_DEBUG_TOOL === 'vconsole' && new VConsole()
6169
<template>
6270
<Provider>
6371
<RouterView
64-
v-slot="{ Component, route }"
72+
v-slot="{ Component }"
6573
:style="{
6674
'--g-main-sidebar-actual-width': mainSidebarActualWidth,
6775
'--g-sub-sidebar-actual-width': subSidebarActualWidth,
6876
}"
6977
>
70-
<component :is="Component" v-if="auth(route.meta.auth ?? '')" />
78+
<component :is="Component" v-if="isAuth" />
7179
<NotAllowed v-else />
7280
</RouterView>
7381
<SystemInfo />

src/store/modules/route.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cloneDeep } from 'lodash-es'
2-
import type { RouteMeta, RouteRecordRaw } from 'vue-router'
2+
import type { RouteRecordRaw } from 'vue-router'
33
import useSettingsStore from './settings'
44
import { resolveRoutePath } from '@/utils'
55
import { systemRoutes } from '@/router/routes'
@@ -25,16 +25,15 @@ const useRouteStore = defineStore(
2525
title: route.meta?.title,
2626
icon: route.meta?.icon,
2727
hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
28-
}], route.path, route.meta?.auth)
28+
}], route.path)
2929
}
3030
return route
3131
}
32-
function flatAsyncRoutesRecursive(routes: RouteRecordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = '', baseAuth: RouteMeta['auth']): RouteRecordRaw[] {
32+
function flatAsyncRoutesRecursive(routes: RouteRecordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = ''): RouteRecordRaw[] {
3333
const res: RouteRecordRaw[] = []
3434
routes.forEach((route) => {
3535
if (route.children) {
3636
const childrenBaseUrl = resolveRoutePath(baseUrl, route.path)
37-
const childrenBaseAuth = !baseAuth || baseAuth === '' || baseAuth?.length === 0 ? route.meta?.auth : baseAuth
3837
const tmpBreadcrumb = cloneDeep(breadcrumb)
3938
tmpBreadcrumb.push({
4039
path: childrenBaseUrl,
@@ -47,11 +46,10 @@ const useRouteStore = defineStore(
4746
if (!tmpRoute.meta) {
4847
tmpRoute.meta = {}
4948
}
50-
tmpRoute.meta.auth = childrenBaseAuth
5149
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
5250
delete tmpRoute.children
5351
res.push(tmpRoute)
54-
const childrenRoutes = flatAsyncRoutesRecursive(route.children, tmpBreadcrumb, childrenBaseUrl, childrenBaseAuth)
52+
const childrenRoutes = flatAsyncRoutesRecursive(route.children, tmpBreadcrumb, childrenBaseUrl)
5553
childrenRoutes.forEach((item) => {
5654
// 如果 path 一样则覆盖,因为子路由的 path 可能设置为空,导致和父路由一样,直接注册会提示路由重复
5755
if (res.some(v => v.path === item.path)) {
@@ -80,7 +78,6 @@ const useRouteStore = defineStore(
8078
if (!tmpRoute.meta) {
8179
tmpRoute.meta = {}
8280
}
83-
tmpRoute.meta.auth = !baseAuth || baseAuth === '' || baseAuth?.length === 0 ? tmpRoute.meta?.auth : baseAuth
8481
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
8582
res.push(tmpRoute)
8683
}

0 commit comments

Comments
 (0)