|
@@ -0,0 +1,155 @@
|
|
|
+import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
|
|
|
+
|
|
|
+const windowConfig = (window as any).AppGlobalConfig;
|
|
|
+
|
|
|
+// 创建 axios 实例
|
|
|
+const http: AxiosInstance = axios.create({
|
|
|
+ baseURL: windowConfig.server,
|
|
|
+ timeout: 30000, // 请求超时时间
|
|
|
+});
|
|
|
+
|
|
|
+const Authorization = windowConfig?.authToken ?? "Authorization";
|
|
|
+// 请求拦截器
|
|
|
+http.interceptors.request.use(
|
|
|
+ (config: any) => {
|
|
|
+ const token = localStorage.getItem("token");
|
|
|
+ if (token) {
|
|
|
+ config.headers = {
|
|
|
+ ...config.headers,
|
|
|
+ [Authorization]: token,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return config;
|
|
|
+ },
|
|
|
+ (error) => Promise.reject(error)
|
|
|
+);
|
|
|
+
|
|
|
+// 响应拦截器
|
|
|
+http.interceptors.response.use(
|
|
|
+ (response: AxiosResponse) => {
|
|
|
+ // 检查响应类型,如果是文件类型,直接返回
|
|
|
+ const contentType = response.headers["content-type"];
|
|
|
+ if (
|
|
|
+ (contentType && contentType.includes("application/octet-stream")) ||
|
|
|
+ response.config.responseType === "blob"
|
|
|
+ ) {
|
|
|
+ return response; // 直接返回文件数据
|
|
|
+ }
|
|
|
+
|
|
|
+ // 大模型类接口请求体,直接返回全部内容
|
|
|
+ if (response.status && response.status == 200) {
|
|
|
+ return response.data; // 直接返回有效数据
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理非文件类型的响应
|
|
|
+ if (response.data.code !== 200) {
|
|
|
+ console.error(`Error: ${response.data.message}`);
|
|
|
+ return Promise.reject(new Error(response.data.message));
|
|
|
+ }
|
|
|
+
|
|
|
+ return response.data;
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ console.error(error.response?.data || error.message);
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+// 封装实例方法
|
|
|
+class HttpRequest {
|
|
|
+ private instance: AxiosInstance;
|
|
|
+
|
|
|
+ constructor(instance: AxiosInstance) {
|
|
|
+ this.instance = instance;
|
|
|
+ }
|
|
|
+ get<T = any>(
|
|
|
+ url: string,
|
|
|
+ params?: Record<string, any>,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<T> {
|
|
|
+ if (config?.baseURL) {
|
|
|
+ this.instance.baseURL = config.baseURL;
|
|
|
+ }
|
|
|
+ return this.instance
|
|
|
+ .get<T>(url, { ...config, params })
|
|
|
+ .then((response) => response as T);
|
|
|
+ }
|
|
|
+
|
|
|
+ post<T = any>(
|
|
|
+ url: string,
|
|
|
+ data?: Record<string, any>,
|
|
|
+ config?: AxiosRequestConfig
|
|
|
+ ): Promise<T> {
|
|
|
+ return this.instance
|
|
|
+ .post<T>(url, data, config)
|
|
|
+ .then((response) => response as T);
|
|
|
+ }
|
|
|
+
|
|
|
+ delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
|
+ return this.instance
|
|
|
+ .delete<T>(url, config)
|
|
|
+ .then((response) => response as T);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 下载文件方法,支持 GET 和 POST
|
|
|
+ download(
|
|
|
+ method: "GET" | "POST",
|
|
|
+ url: string,
|
|
|
+ options: {
|
|
|
+ params?: Record<string, any>; // get 请求参数
|
|
|
+ data?: Record<string, any>; // post 请求参数
|
|
|
+ config?: AxiosRequestConfig; // Axios 配置
|
|
|
+ } = {}
|
|
|
+ ): Promise<void> {
|
|
|
+ const { params, data, config } = options;
|
|
|
+
|
|
|
+ // 设置响应类型为 blob
|
|
|
+ const axiosConfig: AxiosRequestConfig = {
|
|
|
+ ...config,
|
|
|
+ responseType: "blob",
|
|
|
+ };
|
|
|
+
|
|
|
+ // 选择请求方法
|
|
|
+ const request =
|
|
|
+ method === "GET"
|
|
|
+ ? this.instance.get(url, { ...axiosConfig, params })
|
|
|
+ : this.instance.post(url, data, axiosConfig);
|
|
|
+
|
|
|
+ return request
|
|
|
+ .then((response) => {
|
|
|
+ console.log("response", response);
|
|
|
+ const blob = new Blob([response.data]);
|
|
|
+ const contentDisposition = response.headers["content-disposition"];
|
|
|
+ const defaultFileName = "downloaded-file.zip"; // 默认文件名
|
|
|
+ let fileName = defaultFileName;
|
|
|
+
|
|
|
+ // 从 Content-Disposition 获取文件名
|
|
|
+ if (contentDisposition) {
|
|
|
+ const match = contentDisposition.match(/filename="?(.+)"?/);
|
|
|
+ if (match && match[1]) {
|
|
|
+ fileName = decodeURIComponent(match[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建下载链接
|
|
|
+ const downloadUrl = window.URL.createObjectURL(blob);
|
|
|
+ const a = document.createElement("a");
|
|
|
+ a.href = downloadUrl;
|
|
|
+ a.download = fileName;
|
|
|
+ a.style.display = "none";
|
|
|
+ document.body.appendChild(a);
|
|
|
+ a.click();
|
|
|
+ window.URL.revokeObjectURL(downloadUrl); // 释放 URL
|
|
|
+ document.body.removeChild(a);
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ console.error("Download failed:", error);
|
|
|
+ return Promise.reject(error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 实例化封装
|
|
|
+const api = new HttpRequest(http);
|
|
|
+
|
|
|
+export default api;
|