|
|
|
@ -35,6 +35,27 @@ http.interceptors.response.use(response => { |
|
|
|
} |
|
|
|
return response |
|
|
|
}, error => { |
|
|
|
// 处理HTTP错误响应
|
|
|
|
if (error.response) { |
|
|
|
// 服务器返回了错误状态码
|
|
|
|
if (error.response.status === 401) { |
|
|
|
// HTTP 401 未授权
|
|
|
|
clearLoginInfo() |
|
|
|
router.push({ name: 'login' }) |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
|
|
|
|
// 检查响应体中的code字段
|
|
|
|
if (error.response.data && error.response.data.code === 401) { |
|
|
|
clearLoginInfo() |
|
|
|
router.push({ name: 'login' }) |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
} else if (error.request) { |
|
|
|
// 请求已发出但没有收到响应(网络错误)
|
|
|
|
console.error('网络请求失败:', error.message) |
|
|
|
} |
|
|
|
|
|
|
|
return Promise.reject(error) |
|
|
|
}) |
|
|
|
|
|
|
|
@ -108,6 +129,27 @@ instance.interceptors.response.use(response => { |
|
|
|
} |
|
|
|
return response |
|
|
|
}, error => { |
|
|
|
// 处理HTTP错误响应
|
|
|
|
if (error.response) { |
|
|
|
// 服务器返回了错误状态码
|
|
|
|
if (error.response.status === 401) { |
|
|
|
// HTTP 401 未授权
|
|
|
|
clearLoginInfo() |
|
|
|
router.push({ name: 'login' }) |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
|
|
|
|
// 检查响应体中的code字段
|
|
|
|
if (error.response.data && error.response.data.code === 401) { |
|
|
|
clearLoginInfo() |
|
|
|
router.push({ name: 'login' }) |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
} else if (error.request) { |
|
|
|
// 请求已发出但没有收到响应(网络错误)
|
|
|
|
console.error('网络请求失败:', error.message) |
|
|
|
} |
|
|
|
|
|
|
|
return Promise.reject(error) |
|
|
|
}) |
|
|
|
|
|
|
|
@ -143,6 +185,40 @@ instance2.interceptors.response.use(response => { |
|
|
|
} |
|
|
|
return response |
|
|
|
}, error => { |
|
|
|
// 处理HTTP错误响应
|
|
|
|
if (error.response) { |
|
|
|
// 服务器返回了错误状态码
|
|
|
|
if (error.response.status === 401) { |
|
|
|
// HTTP 401 未授权
|
|
|
|
clearLoginInfo() |
|
|
|
router.push({name: 'login'}) |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
|
|
|
|
// 检查响应体中的code字段(blob响应需要特殊处理)
|
|
|
|
if (error.response.data) { |
|
|
|
// 尝试读取blob中的JSON数据
|
|
|
|
const reader = new FileReader() |
|
|
|
reader.onload = () => { |
|
|
|
try { |
|
|
|
const data = JSON.parse(reader.result) |
|
|
|
if (data.code === 401) { |
|
|
|
clearLoginInfo() |
|
|
|
router.push({name: 'login'}) |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
// 不是JSON格式,忽略
|
|
|
|
} |
|
|
|
} |
|
|
|
if (error.response.data instanceof Blob) { |
|
|
|
reader.readAsText(error.response.data) |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (error.request) { |
|
|
|
// 请求已发出但没有收到响应(网络错误)
|
|
|
|
console.error('网络请求失败:', error.message) |
|
|
|
} |
|
|
|
|
|
|
|
return Promise.reject(error) |
|
|
|
}) |
|
|
|
|
|
|
|
|