fix: 修复登录页面401错误处理问题

根本原因:
- request拦截器在收到401错误时立即重定向到登录页
- 这会中断Login.vue的错误处理逻辑
- 导致ElMessage.error()无法执行,错误消息toast无法显示

修复方案:
- 在登录页面时不执行重定向
- 允许Login.vue正常处理错误并显示toast消息
- 改进测试等待策略,确保toast消息出现
This commit is contained in:
张翔
2026-04-04 23:25:36 +08:00
parent 977e283fbd
commit a1497a480b
2 changed files with 11 additions and 4 deletions
@@ -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/);
});
+3 -1
View File
@@ -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)
}