41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
![]() |
import { message } from 'ant-design-vue';
|
||
|
import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';
|
||
|
|
||
|
// 创建HTTP实例
|
||
|
const http: HttpInstance = axios.create({
|
||
|
baseURL: import.meta.env.ENV_HTTP_BASE,
|
||
|
adapter: 'fetch',
|
||
|
timeout: 30_000,
|
||
|
});
|
||
|
export default http;
|
||
|
|
||
|
// 添加请求拦截器
|
||
|
http.interceptors.request.use(
|
||
|
(config) => config,
|
||
|
(error) => Promise.reject(error.message),
|
||
|
);
|
||
|
|
||
|
// 添加响应拦截器
|
||
|
http.interceptors.response.use(
|
||
|
(response) => {
|
||
|
const res = <CommonRes>response.data;
|
||
|
if (res?.success) return <never>res.data;
|
||
|
const hint = res?.message ?? '未知异常';
|
||
|
message.error(hint);
|
||
|
return Promise.reject(hint);
|
||
|
},
|
||
|
(error) => Promise.reject(error.message),
|
||
|
);
|
||
|
|
||
|
type HttpInstance = Omit<AxiosInstance, 'get' | 'post'> & {
|
||
|
get: <R = void>(url: string, config?: AxiosRequestConfig) => Promise<R | undefined>;
|
||
|
post: <R = void, D = unknown>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R | undefined>;
|
||
|
};
|
||
|
|
||
|
type CommonRes<T = void> = {
|
||
|
code: number;
|
||
|
success: boolean;
|
||
|
data: T | undefined;
|
||
|
message: string;
|
||
|
};
|