2025-06-16 14:42:26 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Author: 田鑫
|
|
|
|
|
|
* @Date: 2023-02-17 11:58:44
|
|
|
|
|
|
* @LastEditors: 田鑫
|
|
|
|
|
|
* @LastEditTime: 2023-03-05 19:17:29
|
|
|
|
|
|
* @Description:
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
|
|
|
|
//* 导出Request类,可以用来自定义传递配置来创建实例
|
|
|
|
|
|
export class Request {
|
|
|
|
|
|
//* axios 实例
|
|
|
|
|
|
private instance: AxiosInstance;
|
|
|
|
|
|
//* 基础配置
|
|
|
|
|
|
private baseConfig: AxiosRequestConfig = { baseURL: import.meta.env.EO_API_URL, timeout: 60000 };
|
|
|
|
|
|
|
|
|
|
|
|
public constructor(config: AxiosRequestConfig) {
|
|
|
|
|
|
this.instance = axios.create(Object.assign(this.baseConfig, config));
|
|
|
|
|
|
|
|
|
|
|
|
this.instance.interceptors.request.use(
|
|
|
|
|
|
(config: AxiosRequestConfig) => {
|
|
|
|
|
|
const token = localStorage.getItem('token') as string;
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
config.headers!.Authorization = token;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
config.headers!.satoken = '123';
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: any) => {
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
this.instance.interceptors.response.use(
|
|
|
|
|
|
(res: AxiosResponse) => {
|
|
|
|
|
|
//* http请求成功
|
|
|
|
|
|
//* 存入通用response header
|
|
|
|
|
|
let tenanttype = localStorage.getItem('tenanttype') as string;
|
|
|
|
|
|
if (!tenanttype) {
|
|
|
|
|
|
tenanttype = res.headers!.tenanttype;
|
|
|
|
|
|
localStorage.setItem('tenanttype', tenanttype);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (res.data.code === 200) {
|
|
|
|
|
|
return res.data.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
AMessage.error(res.data!.msg);
|
|
|
|
|
|
return Promise.reject(res.data);
|
|
|
|
|
|
},
|
|
|
|
|
|
(err: any) => {
|
|
|
|
|
|
// AMessage.error(err.message);
|
|
|
|
|
|
// 这里用来处理http常见错误,进行全局提示
|
|
|
|
|
|
return Promise.reject(err.response);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//* 定义请求方法
|
|
|
|
|
|
public request<T = any>(config: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.request(config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.get(url, { params, ...config });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.post(url, data, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.put(url, data, config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.delete(url, config);
|
|
|
|
|
|
}
|
2025-06-18 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
public patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
|
return this.instance.patch(url, data, config);
|
|
|
|
|
|
}
|
2025-06-16 14:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//* 默认导出Request实例
|
|
|
|
|
|
export default new Request({});
|