fetch(add): 新增
Some checks failed
构建并部署前端到测试环境 / build-and-deploy (push) Failing after 6s

This commit is contained in:
sexygoat
2026-01-27 09:18:45 +08:00
parent 0eed8244e5
commit 5c6312c407
33 changed files with 4897 additions and 374 deletions

View File

@@ -8,7 +8,16 @@ const axiosInstance = axios.create({
timeout: 15000, // 请求超时时间(毫秒)
baseURL: import.meta.env.MODE === 'development' ? '' : import.meta.env.VITE_API_URL, // API地址开发环境使用代理生产环境使用完整URL
withCredentials: true, // 异步请求携带cookie
transformRequest: [(data) => JSON.stringify(data)], // 请求数据转换为 JSON 字符串
transformRequest: [
(data, headers) => {
// 如果是 FormData,不进行转换
if (data instanceof FormData) {
return data
}
// 其他数据转换为 JSON 字符串
return JSON.stringify(data)
}
],
validateStatus: (status) => status >= 200 && status < 300, // 只接受 2xx 的状态码
headers: {
get: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
@@ -36,10 +45,17 @@ axiosInstance.interceptors.request.use(
// 如果 token 存在,则设置请求头
if (accessToken) {
request.headers.set({
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
})
// 如果是 FormData,不要覆盖 Content-Type (让浏览器自动设置 boundary)
if (request.data instanceof FormData) {
request.headers.set('Authorization', `Bearer ${accessToken}`)
// 删除 Content-Type,让浏览器自动设置
delete request.headers['Content-Type']
} else {
request.headers.set({
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
})
}
}
return request // 返回修改后的配置