20260816220132
This commit is contained in:
1 parent
b6f9a4c912
commit
72993ecdcd
179 files changed
+20050
-633
No files matched your search
@@ -1,8 +1,80 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import NProgress from 'nprogress'
|
||||
import 'nprogress/nprogress.css'
|
||||
import DefaultLayout from '@/layouts/DefaultLayout.vue'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { usePermissionStore } from '@/stores/permissions'
|
||||
|
||||
NProgress.configure({ showSpinner: false })
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: [],
|
||||
routes: [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('@/views/login/index.vue'),
|
||||
meta: { title: '登录' },
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: DefaultLayout,
|
||||
redirect: '/dashboard',
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{
|
||||
path: '/dashboard',
|
||||
name: 'dashboard',
|
||||
component: () => import('@/views/dashboard/index.vue'),
|
||||
meta: { title: '工作台' },
|
||||
},
|
||||
// 业务页面后续在此追加,配置 meta.moduleId 后自动走权限校验
|
||||
],
|
||||
},
|
||||
{
|
||||
// 组件库演练场:独立路由,不进入业务布局
|
||||
path: '/demo',
|
||||
name: 'demo',
|
||||
component: () => import('@/views/demo/index.vue'),
|
||||
meta: { title: '组件库' },
|
||||
},
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
redirect: '/dashboard',
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
// 登录守卫:未登录访问受保护页跳 /login?redirect=xxx;已登录访问 /login 跳 /dashboard
|
||||
// 权限守卫:路由配置 meta.moduleId 时校验访问权限,无权限跳 /dashboard
|
||||
router.beforeEach(async (to) => {
|
||||
NProgress.start()
|
||||
const authStore = useAuthStore()
|
||||
|
||||
if (to.path === '/login') {
|
||||
if (authStore.isAuthenticated) return '/dashboard'
|
||||
return
|
||||
}
|
||||
|
||||
if (to.matched.some((r) => r.meta.requiresAuth)) {
|
||||
if (!authStore.isAuthenticated) {
|
||||
return { path: '/login', query: { redirect: to.fullPath } }
|
||||
}
|
||||
|
||||
const moduleId = to.meta.moduleId
|
||||
if (moduleId) {
|
||||
const permissionStore = usePermissionStore()
|
||||
// 权限接口失败时静默降级:canAccess 返回 false,与"菜单退化为仅工作台"策略一致
|
||||
await permissionStore.load().catch(() => {})
|
||||
if (!permissionStore.canAccess(moduleId)) {
|
||||
return '/dashboard'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
router.afterEach(() => {
|
||||
NProgress.done()
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in new issue
Block a user