修复登录失败时无错误提示的问题

问题:登录失败时,401 错误会触发自动跳转到登录页,
导致错误提示来不及显示就被页面刷新覆盖。

解决方案:在 fetchWithAuth 函数中,只有当用户不在登录
页面时才执行 401 错误的自动跳转逻辑,让登录页面能够
正常显示错误提示信息。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
燕陇琪 2025-11-07 23:19:52 +08:00
parent 45299b2d7e
commit b082e708ae

View File

@ -113,7 +113,8 @@ export const fetchWithAuth = async (
const response = await fetch(url, fetchOptions) const response = await fetch(url, fetchOptions)
// 统一处理 401 未授权错误 // 统一处理 401 未授权错误
if (response.status === 401) { // 注意:如果已经在登录页面,不要跳转,让登录页面自己处理错误提示
if (response.status === 401 && !window.location.pathname.startsWith('/login')) {
console.error('Token已过期或未授权请重新登录') console.error('Token已过期或未授权请重新登录')
localStorage.removeItem('token') localStorage.removeItem('token')
localStorage.removeItem('user') localStorage.removeItem('user')