Merge branch 'main' of https://git.apinb.com/ops/front
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import axios, {
|
||||
AxiosInstance,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
InternalAxiosRequestConfig
|
||||
} from "axios";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import SafeStorage, { AppStorageKey } from "@/utils/safeStorage";
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import SafeStorage, { AppStorageKey } from '@/utils/safeStorage'
|
||||
|
||||
console.log('import.meta.env.VITE_API_BASE_UR:', import.meta.env.VITE_API_BASE_URL)
|
||||
// 1. 创建axios实例
|
||||
@@ -35,28 +30,8 @@ import axios, {
|
||||
// 3. 响应拦截器
|
||||
instance.interceptors.response.use(
|
||||
(response: AxiosResponse) => {
|
||||
const cfg = response.config as RequestConfig
|
||||
if (cfg.rawResponse) {
|
||||
return response
|
||||
}
|
||||
// 二进制流:只返回 data,且勿对 Blob 访问 .status
|
||||
if (cfg.responseType === 'blob' || cfg.responseType === 'arraybuffer') {
|
||||
const body = response.data as unknown
|
||||
if (body instanceof Blob) return body
|
||||
if (typeof body === 'string') {
|
||||
return new Blob([body], {
|
||||
type: (response.headers['content-type'] as string) || 'application/octet-stream',
|
||||
})
|
||||
}
|
||||
if (body instanceof ArrayBuffer) {
|
||||
return new Blob([body], {
|
||||
type: (response.headers['content-type'] as string) || 'application/octet-stream',
|
||||
})
|
||||
}
|
||||
return body
|
||||
}
|
||||
// 统一处理响应数据格式[2](@ref)
|
||||
if (response.data?.status === 401) {
|
||||
if (response.data.status === 401) {
|
||||
// token过期处理
|
||||
SafeStorage.clearAppStorage();
|
||||
window.location.href = "/auth/login";
|
||||
@@ -79,44 +54,42 @@ import axios, {
|
||||
interface RequestConfig extends AxiosRequestConfig {
|
||||
data?: unknown;
|
||||
needWorkspace?: boolean;
|
||||
/** 为 true 时响应拦截器返回完整 AxiosResponse(用于 blob 等需自行取 data 的场景) */
|
||||
rawResponse?: boolean;
|
||||
}
|
||||
|
||||
export const request = {
|
||||
get<T = any>(url: string, config?: RequestConfig): Promise<T> {
|
||||
return instance.get(url, config);
|
||||
return instance.get(url, config)
|
||||
},
|
||||
post<T = any>(url: string, data = {}, config?: RequestConfig): Promise<T> {
|
||||
let params: any
|
||||
if (config?.needWorkspace) {
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data };
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data }
|
||||
} else {
|
||||
params = data;
|
||||
params = data
|
||||
}
|
||||
return instance.post(url, params, config);
|
||||
return instance.post(url, params, config)
|
||||
},
|
||||
put<T = any>(url: string, data = {}, config?: RequestConfig): Promise<T> {
|
||||
let params: any
|
||||
if (config?.needWorkspace) {
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data };
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data }
|
||||
} else {
|
||||
params = data;
|
||||
params = data
|
||||
}
|
||||
return instance.put(url, params, config);
|
||||
return instance.put(url, params, config)
|
||||
},
|
||||
delete<T = any>(url: string, config?: RequestConfig): Promise<T> {
|
||||
return instance.delete(url, config);
|
||||
return instance.delete(url, config)
|
||||
},
|
||||
patch<T = any>(url: string, data = {}, config?: RequestConfig): Promise<T> {
|
||||
let params: any
|
||||
if (config?.needWorkspace) {
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data };
|
||||
params = { workspace: import.meta.env.VITE_APP_WORKSPACE, ...data }
|
||||
} else {
|
||||
params = data;
|
||||
params = data
|
||||
}
|
||||
return instance.patch(url, params, config);
|
||||
}
|
||||
};
|
||||
|
||||
export default instance;
|
||||
return instance.patch(url, params, config)
|
||||
},
|
||||
}
|
||||
|
||||
export default instance
|
||||
|
||||
Reference in New Issue
Block a user