add router permission

This commit is contained in:
ray_wuhao 2023-02-01 16:40:56 +08:00
parent 59aaa82e0d
commit 94ae564008
3 changed files with 21 additions and 9 deletions

View File

@ -18,6 +18,8 @@
* , meta , * , meta ,
* *
* meta * meta
*
* admin ,
*/ */
import { useSignin } from '@/store' import { useSignin } from '@/store'

View File

@ -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'

View File

@ -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()