From f190f0ebea0aa07ece1ab18805e884cf02b44ddd Mon Sep 17 00:00:00 2001 From: my_ong <429426262@qq.com> Date: Wed, 18 Dec 2024 15:47:56 +0800 Subject: [PATCH] =?UTF-8?q?sparkles:=20=E7=99=BB=E5=BD=95=E6=8B=A6?= =?UTF-8?q?=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/locales/zh/menus.json | 2 +- src/router/index.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/locales/zh/menus.json b/src/locales/zh/menus.json index 31688b8..63c102b 100644 --- a/src/locales/zh/menus.json +++ b/src/locales/zh/menus.json @@ -1,6 +1,6 @@ { "index": "首页", - "dashboard": "仪表盘", + "dashboard": "首页", "acl": { "name": "系统管理", "users": "用户管理", diff --git a/src/router/index.ts b/src/router/index.ts index 235e1d8..06528c3 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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 = [ { @@ -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' }) +})