修复分类问题

This commit is contained in:
2026-08-27 23:02:17 +08:00
parent d6bba226d0
commit ae6b060c25
5 changed files with 54 additions and 25 deletions
+13 -5
View File
@@ -3,10 +3,10 @@ import axios from 'axios';
// 获取API基础URL,添加调试信息
const apiBaseURL = import.meta.env.VITE_API_BASE_URL;
// 创建axios实例
// 创建axios实例(普通接口 5min;大文件上传在 api/file.js 单独设置更长 timeout)
const service = axios.create({
baseURL: apiBaseURL,
timeout: 10000,
timeout: 300000,
withCredentials: false // JWT 不需要 Cookie
});
@@ -18,9 +18,13 @@ service.interceptors.request.use(
config.headers['Authorization'] = `Bearer ${token}`;
}
// 对于有 body 的请求(POST、PUT、PATCH),确保设置 Content-Type
// 对于有 body 的请求(POST、PUT、PATCH),默认 JSON;
// FormData 必须由浏览器自行设置 Content-Type,否则缺少 multipart boundary,服务端无法解析
if (config.data && ['post', 'put', 'patch'].includes(config.method?.toLowerCase())) {
if (!config.headers['Content-Type'] && !config.headers['content-type']) {
if (typeof FormData !== 'undefined' && config.data instanceof FormData) {
delete config.headers['Content-Type'];
delete config.headers['content-type'];
} else if (!config.headers['Content-Type'] && !config.headers['content-type']) {
config.headers['Content-Type'] = 'application/json';
}
}
@@ -54,7 +58,11 @@ service.interceptors.response.use(
console.error('请求失败,请稍后再试');
}
} else if (error.request) {
console.error('请求失败,请检查网络连接');
if (error.code === 'ECONNABORTED') {
console.error('请求超时,请稍后重试');
} else {
console.error('请求失败,请检查网络连接');
}
} else {
console.error('请求配置错误');
}