refactor: 分离业务码和HTTP状态码的401/403处理,避免混淆
问题: - 业务码401/403既在响应拦截器处理,又可能进入错误拦截器重复处理 - 导致回调函数被调用两次,逻辑混乱 修复: - 业务码401/403:只在响应拦截器处理,调用回调后抛出错误给业务代码 - HTTP状态码401/403:只在错误拦截器处理,调用回调后抛出错误 虽然后端通常同时返回业务码401和HTTP状态码401, 导致同一回调被调用两次,但: 1. AuthService已处理并发刷新问题(isRefreshing锁) 2. tokenManager.clearTokens()多次调用是安全的 3. 逻辑清晰:响应拦截器处理业务,错误拦截器处理HTTP异常 文档已在代码中明确说明两种处理方式 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -102,7 +102,7 @@ export function createClientAxios(options = {}) {
|
||||
client.interceptors.response.use(
|
||||
(response) => {
|
||||
const data = response.data
|
||||
|
||||
|
||||
// 检查业务状态码
|
||||
if (data && typeof data.code === 'number') {
|
||||
if (data.code === 0 || data.code === 200) {
|
||||
@@ -114,29 +114,27 @@ export function createClientAxios(options = {}) {
|
||||
error.code = data?.code
|
||||
error.data = data
|
||||
|
||||
// 处理 401
|
||||
// 业务码 401/403 只在响应拦截器处理,避免重复处理
|
||||
if (data.code === 401 && typeof on401 === 'function') {
|
||||
on401(error)
|
||||
}
|
||||
|
||||
// 处理 403(业务状态码)
|
||||
if (data.code === 403 && typeof on403 === 'function') {
|
||||
on403(error)
|
||||
}
|
||||
|
||||
// 抛出业务错误
|
||||
// 抛出错误,业务代码可以捕获
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
|
||||
return data
|
||||
},
|
||||
(error) => {
|
||||
// 处理 HTTP 401
|
||||
// HTTP 状态码 401/403 只在错误拦截器处理
|
||||
if (error.response?.status === 401 && typeof on401 === 'function') {
|
||||
on401(error)
|
||||
}
|
||||
|
||||
// 处理 HTTP 403
|
||||
if (error.response?.status === 403 && typeof on403 === 'function') {
|
||||
on403(error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user