sparkles: 登录拦截
This commit is contained in:
parent
d1356f167c
commit
f190f0ebea
@ -1,6 +1,6 @@
|
||||
{
|
||||
"index": "首页",
|
||||
"dashboard": "仪表盘",
|
||||
"dashboard": "首页",
|
||||
"acl": {
|
||||
"name": "系统管理",
|
||||
"users": "用户管理",
|
||||
|
@ -2,6 +2,7 @@ import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
|
||||
import UserLayout from '@/layout/user-layout.vue'
|
||||
import AdminLayout from '@/layout/admin-layout.vue'
|
||||
import BlankLayout from '@/layout/blank-layout.vue'
|
||||
import router from '@/router'
|
||||
|
||||
export const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
@ -195,3 +196,26 @@ const filterRoutesByPermission = (routes: RouteRecordRaw[], permissions: string[
|
||||
})
|
||||
.filter((route): route is RouteRecordRaw => route !== undefined) // 过滤掉 undefined 的项并确保类型正确
|
||||
}
|
||||
|
||||
// 白名单
|
||||
const whiteList = ['Index', 'Admin', 'Message', 'LoginPage', 'Login']
|
||||
|
||||
// 路由守卫
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
if (to.name && whiteList.includes(to.name as string)) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
// 其他页面需要登录 用pinia会报错
|
||||
const user = localStorage.getItem('user')
|
||||
if (user) {
|
||||
// 将json字符串转换为对象auth.AuthUser
|
||||
const obj: auth.AuthUser = JSON.parse(user)
|
||||
if (obj.token && obj.token.length > 0) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
}
|
||||
window.console.log('没有登录')
|
||||
next({ name: 'LoginPage' })
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user