|
@@ -1,9 +1,9 @@
|
|
|
// src/utils/request.ts
|
|
// src/utils/request.ts
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
-import type {
|
|
|
|
|
- AxiosInstance,
|
|
|
|
|
- AxiosRequestConfig,
|
|
|
|
|
- AxiosResponse,
|
|
|
|
|
|
|
+import type {
|
|
|
|
|
+ AxiosInstance,
|
|
|
|
|
+ AxiosRequestConfig,
|
|
|
|
|
+ AxiosResponse,
|
|
|
AxiosError,
|
|
AxiosError,
|
|
|
InternalAxiosRequestConfig,
|
|
InternalAxiosRequestConfig,
|
|
|
AxiosRequestHeaders
|
|
AxiosRequestHeaders
|
|
@@ -19,24 +19,24 @@ interface CustomAxiosInstance extends AxiosInstance {
|
|
|
|
|
|
|
|
// 创建API客户端
|
|
// 创建API客户端
|
|
|
export const api5000: CustomAxiosInstance = axios.create({
|
|
export const api5000: CustomAxiosInstance = axios.create({
|
|
|
- baseURL: isDevelopment
|
|
|
|
|
- ? 'http://localhost:5000'
|
|
|
|
|
|
|
+ baseURL: isDevelopment
|
|
|
|
|
+ ? 'http://localhost:5000'
|
|
|
: 'https://www.soilgd.com:5000',
|
|
: 'https://www.soilgd.com:5000',
|
|
|
timeout: 100000,
|
|
timeout: 100000,
|
|
|
withCredentials: false // 关键修改:除非需要cookie,否则设为false
|
|
withCredentials: false // 关键修改:除非需要cookie,否则设为false
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
export const api8000: CustomAxiosInstance = axios.create({
|
|
export const api8000: CustomAxiosInstance = axios.create({
|
|
|
- baseURL: isDevelopment
|
|
|
|
|
- ? 'http://localhost:8000'
|
|
|
|
|
|
|
+ baseURL: isDevelopment
|
|
|
|
|
+ ? 'http://localhost:8000'
|
|
|
: 'https://www.soilgd.com:8000',
|
|
: 'https://www.soilgd.com:8000',
|
|
|
timeout: 100000,
|
|
timeout: 100000,
|
|
|
withCredentials: true
|
|
withCredentials: true
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
export const apiMain: CustomAxiosInstance = axios.create({
|
|
export const apiMain: CustomAxiosInstance = axios.create({
|
|
|
- baseURL: isDevelopment
|
|
|
|
|
- ? 'http://localhost'
|
|
|
|
|
|
|
+ baseURL: isDevelopment
|
|
|
|
|
+ ? 'http://localhost'
|
|
|
: 'https://www.soilgd.com',
|
|
: 'https://www.soilgd.com',
|
|
|
timeout: 100000,
|
|
timeout: 100000,
|
|
|
withCredentials: true
|
|
withCredentials: true
|
|
@@ -48,9 +48,9 @@ function isGeoJSONResponse(response: AxiosResponse): boolean {
|
|
|
const contentType = response.headers['content-type'] || '';
|
|
const contentType = response.headers['content-type'] || '';
|
|
|
return (
|
|
return (
|
|
|
contentType.includes('application/geo+json') ||
|
|
contentType.includes('application/geo+json') ||
|
|
|
- (contentType.includes('application/json') &&
|
|
|
|
|
- (response.data?.type === 'FeatureCollection' ||
|
|
|
|
|
- response.data?.type === 'Feature'))
|
|
|
|
|
|
|
+ (contentType.includes('application/json') &&
|
|
|
|
|
+ (response.data?.type === 'FeatureCollection' ||
|
|
|
|
|
+ response.data?.type === 'Feature'))
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -66,25 +66,25 @@ const setupInterceptors = (instance: CustomAxiosInstance) => {
|
|
|
}
|
|
}
|
|
|
config.headers.set('Authorization', `Bearer ${token}`);
|
|
config.headers.set('Authorization', `Bearer ${token}`);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 为GeoJSON请求设置Accept头
|
|
// 为GeoJSON请求设置Accept头
|
|
|
if (config.url?.match(/\/geojson|\/vector/i)) {
|
|
if (config.url?.match(/\/geojson|\/vector/i)) {
|
|
|
if (!config.headers) {
|
|
if (!config.headers) {
|
|
|
config.headers = new axios.AxiosHeaders();
|
|
config.headers = new axios.AxiosHeaders();
|
|
|
}
|
|
}
|
|
|
config.headers.set('Accept', 'application/geo+json, application/json');
|
|
config.headers.set('Accept', 'application/geo+json, application/json');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 为GeoJSON请求设置更长的超时时间
|
|
// 为GeoJSON请求设置更长的超时时间
|
|
|
if (!config.timeout || config.timeout < 180000) {
|
|
if (!config.timeout || config.timeout < 180000) {
|
|
|
config.timeout = 180000; // 3分钟
|
|
config.timeout = 180000; // 3分钟
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return config;
|
|
return config;
|
|
|
},
|
|
},
|
|
|
(error: AxiosError) => Promise.reject(error)
|
|
(error: AxiosError) => Promise.reject(error)
|
|
|
);
|
|
);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 响应拦截器
|
|
// 响应拦截器
|
|
|
instance.interceptors.response.use(
|
|
instance.interceptors.response.use(
|
|
|
(response: AxiosResponse) => {
|
|
(response: AxiosResponse) => {
|
|
@@ -93,7 +93,7 @@ const setupInterceptors = (instance: CustomAxiosInstance) => {
|
|
|
const isBlob = response.config.responseType === 'blob';
|
|
const isBlob = response.config.responseType === 'blob';
|
|
|
const isImage = contentType.includes('image/');
|
|
const isImage = contentType.includes('image/');
|
|
|
const isJSON = contentType.includes('application/json');
|
|
const isJSON = contentType.includes('application/json');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
console.log('响应处理:', {
|
|
console.log('响应处理:', {
|
|
|
url: response.config.url,
|
|
url: response.config.url,
|
|
|
responseType: response.config.responseType,
|
|
responseType: response.config.responseType,
|
|
@@ -102,23 +102,23 @@ const setupInterceptors = (instance: CustomAxiosInstance) => {
|
|
|
isImage,
|
|
isImage,
|
|
|
isJSON
|
|
isJSON
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 1. 处理二进制响应(图片/文件下载)
|
|
// 1. 处理二进制响应(图片/文件下载)
|
|
|
if (isBlob || isImage) {
|
|
if (isBlob || isImage) {
|
|
|
return response;
|
|
return response;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 2. 处理GeoJSON响应(新增部分)
|
|
// 2. 处理GeoJSON响应(新增部分)
|
|
|
if (isGeoJSONResponse(response)) {
|
|
if (isGeoJSONResponse(response)) {
|
|
|
console.log('检测到GeoJSON响应,返回完整响应对象');
|
|
console.log('检测到GeoJSON响应,返回完整响应对象');
|
|
|
return response;
|
|
return response;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 3. 处理普通JSON响应
|
|
// 3. 处理普通JSON响应
|
|
|
if (isJSON) {
|
|
if (isJSON) {
|
|
|
return response;
|
|
return response;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 4. 其他类型响应
|
|
// 4. 其他类型响应
|
|
|
return response;
|
|
return response;
|
|
|
},
|
|
},
|