2025-04-20 00:49:14 +08:00
|
|
|
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(
|
2025-06-10 14:48:52 +08:00
|
|
|
(config) => {
|
|
|
|
try {
|
|
|
|
const token =
|
|
|
|
JSON.parse(localStorage.getItem('VWED_AMR调度系统__PRODUCTION__3.7.1__COMMON__LOCAL__KEY__') || '{}')?.value
|
|
|
|
.TOKEN__.value || '';
|
|
|
|
config.headers['x-access-token'] = token;
|
|
|
|
const tenantId =
|
|
|
|
JSON.parse(localStorage.getItem('VWED_AMR调度系统__PRODUCTION__3.7.1__COMMON__LOCAL__KEY__') || '{}')?.value
|
|
|
|
.TENANT_ID.value || '';
|
|
|
|
config.headers['x-tenant-id'] = tenantId;
|
|
|
|
console.log(config);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
2025-04-20 00:49:14 +08:00
|
|
|
(error) => Promise.reject(error.message),
|
|
|
|
);
|
|
|
|
|
|
|
|
// 添加响应拦截器
|
|
|
|
http.interceptors.response.use(
|
|
|
|
(response) => {
|
|
|
|
const res = <CommonRes>response.data;
|
2025-06-06 22:36:16 +08:00
|
|
|
if (res?.success) return <never>res.result;
|
2025-04-20 00:49:14 +08:00
|
|
|
const hint = res?.message ?? '未知异常';
|
|
|
|
message.error(hint);
|
|
|
|
return Promise.reject(hint);
|
|
|
|
},
|
2025-05-13 22:37:34 +08:00
|
|
|
(error) => {
|
|
|
|
const hint = error.response.data?.message || error.message;
|
|
|
|
message.error(hint ?? '未知异常');
|
|
|
|
return Promise.reject(hint);
|
|
|
|
},
|
2025-04-20 00:49:14 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
type HttpInstance = Omit<AxiosInstance, 'get' | 'post'> & {
|
2025-04-30 00:17:09 +08:00
|
|
|
get: <D = void>(url: string, config?: AxiosRequestConfig) => Promise<D | undefined>;
|
|
|
|
post: <D = void, B = unknown>(url: string, body?: B, config?: AxiosRequestConfig<B>) => Promise<D | undefined>;
|
2025-04-20 00:49:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
type CommonRes<T = void> = {
|
|
|
|
code: number;
|
|
|
|
success: boolean;
|
2025-06-06 22:36:16 +08:00
|
|
|
result: T | undefined;
|
2025-04-20 00:49:14 +08:00
|
|
|
message: string;
|
|
|
|
};
|