mirror of
https://github.com/zclzone/vue-naive-admin.git
synced 2026-01-22 23:50:22 +08:00
init
This commit is contained in:
37
src/router/guards/permission-guard.js
Normal file
37
src/router/guards/permission-guard.js
Normal file
@@ -0,0 +1,37 @@
|
||||
/**********************************
|
||||
* @Author: Ronnie Zhang
|
||||
* @LastEditor: Ronnie Zhang
|
||||
* @LastEditTime: 2023/12/05 21:25:07
|
||||
* @Email: zclzone@outlook.com
|
||||
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
||||
**********************************/
|
||||
|
||||
import { useAuthStore } from '@/store'
|
||||
import api from '@/api'
|
||||
|
||||
const WHITE_LIST = ['/login', '/404', '/role-select']
|
||||
export function createPermissionGuard(router) {
|
||||
router.beforeEach(async (to) => {
|
||||
const authStore = useAuthStore()
|
||||
const token = authStore.accessToken
|
||||
|
||||
/** 没有token */
|
||||
if (!token) {
|
||||
if (WHITE_LIST.includes(to.path)) return true
|
||||
return { path: 'login', query: { ...to.query, redirect: to.path } }
|
||||
}
|
||||
|
||||
// 有token的情况
|
||||
if (to.path === '/login') return { path: '/' }
|
||||
if (WHITE_LIST.includes(to.path)) return true
|
||||
|
||||
const routes = router.getRoutes()
|
||||
if (routes.find((route) => route.name === to.name)) return true
|
||||
|
||||
// 判断是无权限还是404
|
||||
const { data: hasMenu } = await api.validateMenuPath(to.path)
|
||||
return hasMenu
|
||||
? { name: '403', query: { path: to.fullPath }, state: { from: 'permission-guard' } }
|
||||
: { name: '404', query: { path: to.fullPath } }
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user