1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// src/router/types.ts
export interface RouteMeta {
title?: string;
requiresAuth?: boolean;
requiresAdmin?: boolean;
permissions?: string[];
breadcrumb?: string[];
icon?: string;
hidden?: boolean;
}
// 为 Vue Router 添加类型扩展
declare module 'vue-router' {
interface RouteMeta extends RouteMeta {}
// 扩展路由参数类型
interface RouteParams {
id?: string;
userId?: string;
productId?: string;
categoryId?: string;
}
}
// src/router/index.ts
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { useUserStore } from '@/stores/userStore';
// 路由配置
const routes: RouteRecordRaw[] = [
{
path: '/',
name: 'Layout',
component: () => import('@/layout/index.vue'),
meta: { requiresAuth: true },
children: [
{
path: '',
name: 'Dashboard',
component: () => import('@/views/Dashboard.vue'),
meta: {
title: '仪表盘',
icon: 'dashboard',
breadcrumb: ['首页']
}
},
{
path: 'users',
name: 'Users',
component: () => import('@/views/users/List.vue'),
meta: {
title: '用户管理',
icon: 'users',
permissions: ['user:read'],
breadcrumb: ['首页', '用户管理']
}
},
{
path: 'users/:id',
name: 'UserDetail',
component: () => import('@/views/users/Detail.vue'),
props: true,
meta: {
title: '用户详情',
permissions: ['user:read'],
hidden: true,
breadcrumb: ['首页', '用户管理', '用户详情']
}
}
]
},
{
path: '/auth',
name: 'Auth',
component: () => import('@/views/auth/AuthLayout.vue'),
meta: { requiresAuth: false },
children: [
{
path: 'login',
name: 'Login',
component: () => import('@/views/auth/Login.vue'),
meta: { title: '登录' }
},
{
path: 'register',
name: 'Register',
component: () => import('@/views/auth/Register.vue'),
meta: { title: '注册' }
}
]
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => import('@/views/NotFound.vue'),
meta: { title: '404 - 页面未找到' }
}
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
});
// 导航守卫
router.beforeEach(async (to, from, next) => {
const userStore = useUserStore();
// 初始化用户状态
if (!userStore.isInitialized) {
userStore.initialize();
userStore.isInitialized = true;
}
// 设置页面标题
document.title = to.meta.title
? `${to.meta.title} - 企业管理系统`
: '企业管理系统';
// 权限检查
if (to.meta.requiresAuth !== false && !userStore.isAuthenticated) {
return next({ name: 'Login', query: { redirect: to.fullPath } });
}
// 已登录用户访问登录页
if (['Login', 'Register'].includes(to.name as string) && userStore.isAuthenticated) {
return next({ name: 'Dashboard' });
}
// 管理员权限检查
if (to.meta.requiresAdmin && !userStore.isAdmin) {
return next({ name: 'NotFound' });
}
// 权限列表检查
if (to.meta.permissions && to.meta.permissions.length > 0) {
const hasPermission = to.meta.permissions.some(permission =>
userStore.hasPermission(permission)
);
if (!hasPermission) {
return next({ name: 'NotFound' });
}
}
next();
});
export default router;
|