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
*
* admin ,
*/
import { useSignin } from '@/store'

View File

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

View File

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