From 4d1003e46771e994e0131195a4f7c48f876105db Mon Sep 17 00:00:00 2001 From: gaoxq <376340421@qq.com> Date: Fri, 3 Apr 2026 17:41:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E9=9C=80=E8=AE=A4=E8=AF=81=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=89=8D=E9=AA=8C=E8=AF=81=E5=90=8E=E7=AB=AF=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-vue/src/router/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web-vue/src/router/index.js b/web-vue/src/router/index.js index dadef08..3253342 100644 --- a/web-vue/src/router/index.js +++ b/web-vue/src/router/index.js @@ -44,13 +44,29 @@ const router = createRouter({ routes }) -router.beforeEach((to, from, next) => { +router.beforeEach(async (to, from, next) => { const token = localStorage.getItem('token') if (to.meta.requiresAuth && !token) { next('/login') } else if (to.path === '/login' && token) { next('/desktop') + } else if (to.meta.requiresAuth && token) { + // 有 token 且要进需认证页面,先验证后端是否可用 + try { + await fetch('/api/files/test') + next() + } catch { + // 后端不可用,清除登录状态跳转登录页 + localStorage.removeItem('token') + localStorage.removeItem('username') + localStorage.removeItem('userId') + localStorage.removeItem('nickname') + localStorage.removeItem('avatar') + localStorage.removeItem('storageUsed') + localStorage.removeItem('storageLimit') + window.location.href = '/login' + } } else { next() }