From a1497a480b9f56b0c95725cb74a8543a1a2be356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Sat, 4 Apr 2026 23:25:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2401=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根本原因: - request拦截器在收到401错误时立即重定向到登录页 - 这会中断Login.vue的错误处理逻辑 - 导致ElMessage.error()无法执行,错误消息toast无法显示 修复方案: - 在登录页面时不执行重定向 - 允许Login.vue正常处理错误并显示toast消息 - 改进测试等待策略,确保toast消息出现 --- .../scenarios/authentication/login-flow.spec.ts | 11 ++++++++--- novalon-manage-web/src/utils/request.ts | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/novalon-manage-web/e2e/role-based-tests/scenarios/authentication/login-flow.spec.ts b/novalon-manage-web/e2e/role-based-tests/scenarios/authentication/login-flow.spec.ts index 15dd161..dd7d34c 100644 --- a/novalon-manage-web/e2e/role-based-tests/scenarios/authentication/login-flow.spec.ts +++ b/novalon-manage-web/e2e/role-based-tests/scenarios/authentication/login-flow.spec.ts @@ -34,10 +34,15 @@ test.describe('登录流程测试', () => { await page.fill('input[placeholder*="用户名"]', 'admin'); await page.fill('input[placeholder*="密码"]', 'wrongpassword'); - await page.click('button:has-text("登录")'); - const errorMessage = page.locator('.el-message--error'); - await expect(errorMessage).toBeVisible({ timeout: 5000 }); + await Promise.all([ + page.waitForResponse(resp => resp.url().includes('/auth/login') && resp.status() === 401), + page.click('button:has-text("登录")') + ]); + + const errorMessage = page.locator('.el-message'); + await expect(errorMessage).toBeVisible({ timeout: 10000 }); + await expect(errorMessage).toContainText(/用户名或密码错误|登录失败/i); await expect(page).toHaveURL(/\/login/); }); diff --git a/novalon-manage-web/src/utils/request.ts b/novalon-manage-web/src/utils/request.ts index 8d38e7d..e2b8511 100644 --- a/novalon-manage-web/src/utils/request.ts +++ b/novalon-manage-web/src/utils/request.ts @@ -47,7 +47,9 @@ request.interceptors.response.use( (error) => { if (error.response?.status === 401) { localStorage.removeItem('token') - window.location.href = '/login' + if (!window.location.pathname.includes('/login')) { + window.location.href = '/login' + } } return Promise.reject(error) }