mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2025-04-06 03:57:49 +08:00
add router permission
This commit is contained in:
parent
59aaa82e0d
commit
94ae564008
@ -18,6 +18,8 @@
|
||||
* 如果需要添加公共路由, 不希望添加复杂 meta 配置, 则可以在此添加路由名称即可
|
||||
*
|
||||
* 该配置会覆盖 meta 配置
|
||||
*
|
||||
* 默认预设 admin 作为超级管理员角色, 可根据自身需求修改
|
||||
*/
|
||||
|
||||
import { useSignin } from '@/store'
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { createRouter, createWebHashHistory } from 'vue-router'
|
||||
import { constantRoutes } from './routes'
|
||||
import { getCache, setCache } from '@/utils/cache'
|
||||
|
||||
import { permissionRouter as _permissionRouter } from './permission'
|
||||
|
||||
|
@ -23,27 +23,38 @@
|
||||
*/
|
||||
|
||||
import { getCache, setCache } from '@/utils/cache'
|
||||
import { useSignin } from '@/store'
|
||||
|
||||
import type { Router } from 'vue-router'
|
||||
import type { Router, NavigationGuardNext } from 'vue-router'
|
||||
|
||||
export const permissionRouter = (router: Router) => {
|
||||
const { beforeEach } = router
|
||||
|
||||
const redirectToDashboard = (next: NavigationGuardNext) => {
|
||||
next('/dashboard')
|
||||
|
||||
setCache('menuKey', '/dashboard')
|
||||
}
|
||||
|
||||
beforeEach((to, from, next) => {
|
||||
const token = getCache('token')
|
||||
const route = getCache('menuKey')
|
||||
const { role } = storeToRefs(useSignin())
|
||||
const { meta } = to
|
||||
|
||||
if (token !== 'no') {
|
||||
if (to.path === '/' || from.path === '/login') {
|
||||
if (route !== 'no') {
|
||||
next(route)
|
||||
if (meta?.role?.includes(role.value)) {
|
||||
if (to.path === '/' || from.path === '/login') {
|
||||
if (route !== 'no') {
|
||||
next(route)
|
||||
} else {
|
||||
redirectToDashboard(next)
|
||||
}
|
||||
} else {
|
||||
next('/dashboard')
|
||||
|
||||
setCache('menuKey', '/dashboard')
|
||||
next()
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
redirectToDashboard(next)
|
||||
}
|
||||
} else {
|
||||
if (to.path === '/' || from.path === '/login') {
|
||||
|
Loading…
x
Reference in New Issue
Block a user