Skip to content

Commit 34c7e41

Browse files
committed
对未设置 redirect 的父路由,访问时将自动重定向到允许访问的子路由
1 parent d572e6d commit 34c7e41

File tree

21 files changed

+438
-234
lines changed

21 files changed

+438
-234
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"zod": "^3.24.3"
7474
},
7575
"devDependencies": {
76-
"@antfu/eslint-config": "4.3.0",
76+
"@antfu/eslint-config": "^4.12.0",
7777
"@faker-js/faker": "^9.7.0",
7878
"@iconify/json": "^2.2.333",
7979
"@iconify/vue": "^4.3.0",

pnpm-lock.yaml

Lines changed: 406 additions & 194 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layouts/components/Topbar/Toolbar/NavSearch/search.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useFocus } from '@vueuse/core'
1010
import hotkeys from 'hotkeys-js'
1111
1212
defineOptions({
13-
name: 'Search',
13+
name: 'NavSearchModal',
1414
})
1515
1616
const isShow = defineModel<boolean>({

src/mock/app.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default defineFakeRoute([
1818
{
1919
path: '/multilevel_menu_example',
2020
component: 'Layout',
21-
redirect: '/multilevel_menu_example/page',
2221
name: 'multilevelMenuExample',
2322
meta: {
2423
title: '多级导航',
@@ -36,7 +35,6 @@ export default defineFakeRoute([
3635
{
3736
path: 'level2',
3837
name: 'multilevelMenuExample2',
39-
redirect: '/multilevel_menu_example/level2/page',
4038
meta: {
4139
title: '导航2',
4240
},
@@ -52,7 +50,6 @@ export default defineFakeRoute([
5250
{
5351
path: 'level3',
5452
name: 'multilevelMenuExample2-2',
55-
redirect: '/multilevel_menu_example/level2/level3/page1',
5653
meta: {
5754
title: '导航2-2',
5855
},
@@ -82,7 +79,6 @@ export default defineFakeRoute([
8279
{
8380
path: '/permission_example',
8481
component: 'Layout',
85-
redirect: '/permission_example/index',
8682
name: 'permissionExample',
8783
meta: {
8884
title: '权限验证',

src/router/guards.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ function setupRoutes(router: Router) {
112112
})
113113
}
114114

115+
// 当父级路由未配置重定向时,自动重定向到有访问权限的子路由
116+
function setupRedirectAuthChildendRoute(router: Router) {
117+
router.beforeEach((to, _from, next) => {
118+
const { auth } = useAuth()
119+
const currentRoute = router.getRoutes().find(route => route.path === (to.matched.at(-1)?.path ?? ''))
120+
if (!currentRoute?.redirect) {
121+
const findAuthRoute = currentRoute?.children?.find(route => route.meta?.menu !== false && auth(route.meta?.auth ?? ''))
122+
if (findAuthRoute) {
123+
next(findAuthRoute)
124+
}
125+
else {
126+
next()
127+
}
128+
}
129+
else {
130+
next()
131+
}
132+
})
133+
}
134+
115135
// 进度条
116136
function setupProgress(router: Router) {
117137
const { isLoading } = useNProgress()
@@ -208,6 +228,7 @@ function setupOther(router: Router) {
208228

209229
export default function setupGuards(router: Router) {
210230
setupRoutes(router)
231+
setupRedirectAuthChildendRoute(router)
211232
setupProgress(router)
212233
setupTitle(router)
213234
setupKeepAlive(router)

src/router/modules/breadcrumb.example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/breadcrumb_example',
99
component: Layout,
10-
redirect: '/breadcrumb_example/list1',
1110
name: 'breadcrumbExample',
1211
meta: {
1312
title: '面包屑导航',
@@ -34,7 +33,6 @@ const routes: RouteRecordRaw = {
3433
},
3534
{
3635
path: 'list2',
37-
redirect: '/breadcrumb_example/list2',
3836
meta: {
3937
title: '列表2(层级模式)',
4038
},

src/router/modules/component.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ const routes: RouteRecordRaw = {
242242
},
243243
{
244244
path: 'extend',
245-
redirect: 'pageheader',
246245
name: 'componentExampleExtend',
247246
meta: {
248247
title: '扩展组件',

src/router/modules/ecology.example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw[] = [
88
{
99
path: '/official',
10-
redirect: '/official/fantastic-startkit',
1110
component: Layout,
1211
meta: {
1312
title: '官方周边',
@@ -48,7 +47,6 @@ const routes: RouteRecordRaw[] = [
4847
},
4948
{
5049
path: '/recommand',
51-
redirect: '/recommand/vform',
5250
component: Layout,
5351
meta: {
5452
title: '友情推荐',

src/router/modules/external.link.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/link',
99
component: Layout,
10-
redirect: '/link/gitee',
1110
name: 'externalLinkExample',
1211
meta: {
1312
title: '外链',

src/router/modules/feature.example.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function Layout() {
77
const routes: RouteRecordRaw = {
88
path: '/feature_example',
99
component: Layout,
10-
redirect: '/feature_example/clipboard',
1110
name: 'featureExample',
1211
meta: {
1312
title: '功能',

0 commit comments

Comments
 (0)