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