mirror of
https://github.com/zclzone/vue-naive-admin.git
synced 2026-01-23 08:00:22 +08:00
init
This commit is contained in:
105
src/router/index.js
Normal file
105
src/router/index.js
Normal file
@@ -0,0 +1,105 @@
|
||||
/**********************************
|
||||
* @Author: Ronnie Zhang
|
||||
* @LastEditor: Ronnie Zhang
|
||||
* @LastEditTime: 2023/12/05 21:25:23
|
||||
* @Email: zclzone@outlook.com
|
||||
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
||||
**********************************/
|
||||
|
||||
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
|
||||
import { setupRouterGuards } from './guards'
|
||||
import { useAuthStore, usePermissionStore, useUserStore } from '@/store'
|
||||
|
||||
export const basicRoutes = [
|
||||
{
|
||||
name: 'Login',
|
||||
path: '/login',
|
||||
component: () => import('@/views/login/index.vue'),
|
||||
meta: {
|
||||
title: '登录页',
|
||||
layout: 'empty',
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: 'Home',
|
||||
path: '/',
|
||||
component: () => import('@/views/home/index.vue'),
|
||||
meta: {
|
||||
title: '首页',
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: '404',
|
||||
path: '/404',
|
||||
component: () => import('@/views/error-page/404.vue'),
|
||||
meta: {
|
||||
title: '页面飞走了',
|
||||
layout: 'empty',
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: '403',
|
||||
path: '/403',
|
||||
component: () => import('@/views/error-page/403.vue'),
|
||||
meta: {
|
||||
title: '没有权限',
|
||||
layout: 'empty',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export const router = createRouter({
|
||||
history:
|
||||
import.meta.env.VITE_USE_HASH === 'true' ? createWebHashHistory('/') : createWebHistory('/'),
|
||||
routes: basicRoutes,
|
||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||
})
|
||||
|
||||
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) {
|
||||
authStore.toLogin()
|
||||
return
|
||||
}
|
||||
await Promise.all([userStore.getUserInfo(), permissionStore.initPermissions()])
|
||||
permissionStore.accessRoutes.forEach((route) => {
|
||||
!router.hasRoute(route.name) && router.addRoute(route)
|
||||
})
|
||||
}
|
||||
|
||||
export async function resetRouter() {
|
||||
const basicRouteNames = getRouteNames(basicRoutes)
|
||||
router.getRoutes().forEach((route) => {
|
||||
const name = route.name
|
||||
if (!basicRouteNames.includes(name)) {
|
||||
router.removeRoute(name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function getRouteNames(routes) {
|
||||
const names = []
|
||||
for (const route of routes) {
|
||||
names.push(route.name)
|
||||
if (route.children?.length) {
|
||||
names.push(...getRouteNames(route.children))
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
Reference in New Issue
Block a user