From 376981cf5ea36d928001d0082496d866a1ebdfc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E7=9A=93?= <1736436516@qq.com> Date: Mon, 30 Sep 2024 15:43:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 13a0e8e..fb3be9e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import router from './router'; import ElementPlus from 'element-plus'; import 'element-plus/dist/index.css'; import zhCn from 'element-plus/es/locale/lang/zh-cn'; +// import axiosInstance from '@/utils/axios-config'; import axios from 'axios'; import '@/assets/global.css' @@ -11,7 +12,29 @@ import '@/assets/global.css' const app = createApp(App) -app.provide('axios', axios); +// app.provide('axios', axiosInstance); app.use(ElementPlus, { locale: zhCn }); app.use (router) + +// 导航守卫,检查登录状态 +router.beforeEach((to, from, next) => { + const token = localStorage.getItem('alertToken'); // 检查 token 是否存在,作为是否登录的依据 + if (to.matched.some(record => record.meta.requiresAuth)) { + // 该路由需要认证 + if (!token) { + // 如果没有 token,重定向到登录页面 + next({ + path: '/login', + query: { redirect: to.fullPath } // 将当前路径传递给登录页面,登录后可以重定向回来 + }); + } else { + // 已登录,继续访问 + next(); + } + } else { + // 不需要认证,继续访问 + next(); + } +}); + app.mount('#app')