mirror of
https://github.com/zclzone/vue-naive-admin.git
synced 2026-01-23 08:00:22 +08:00
chore: 重构权限路由守卫
This commit is contained in:
@@ -8,8 +8,6 @@
|
||||
|
||||
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
|
||||
import { setupRouterGuards } from './guards'
|
||||
import { useAuthStore, usePermissionStore, useUserStore } from '@/store'
|
||||
import { getPermissions, getUserInfo } from '@/store/helper'
|
||||
import { basicRoutes } from './basic-routes'
|
||||
|
||||
export const router = createRouter({
|
||||
@@ -22,36 +20,6 @@ export const router = createRouter({
|
||||
})
|
||||
|
||||
export async function setupRouter(app) {
|
||||
try {
|
||||
await initUserAndPermissions()
|
||||
} catch (error) {
|
||||
console.error('🚀 初始化失败', error)
|
||||
}
|
||||
setupRouterGuards(router)
|
||||
app.use(router)
|
||||
}
|
||||
|
||||
export async function initUserAndPermissions() {
|
||||
const permissionStore = usePermissionStore()
|
||||
const userStore = useUserStore()
|
||||
const authStore = useAuthStore()
|
||||
|
||||
if (!authStore.accessToken) {
|
||||
const route = unref(router.currentRoute)
|
||||
if (route.path !== '/login') {
|
||||
router.replace({
|
||||
path: '/login',
|
||||
query: route.query,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
const [user, permissions] = await Promise.all([getUserInfo(), getPermissions()])
|
||||
userStore.setUser(user)
|
||||
permissionStore.setPermissions(permissions)
|
||||
const routeComponents = import.meta.glob('@/views/**/*.vue')
|
||||
permissionStore.accessRoutes.forEach((route) => {
|
||||
route.component = routeComponents[route.component] || undefined
|
||||
!router.hasRoute(route.name) && router.addRoute(route)
|
||||
})
|
||||
setupRouterGuards(router)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user