84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import axios from 'axios';
|
||
import type { AxiosInstance, AxiosResponse } from 'axios';
|
||
|
||
// API 配置
|
||
const API_CONFIG = {
|
||
// 内网地址(HTTP)
|
||
INTERNAL_URL: 'http://10.1.5.118:8077/platform-api',
|
||
// 外网地址(HTTPS)
|
||
EXTERNAL_URL: 'https://apihis.circleharmonyhospital.cn:8982/platform-api',
|
||
// 默认使用外网地址,可根据环境变量切换
|
||
// 开发环境使用内网,生产环境使用外网
|
||
BASE_URL: import.meta.env.NODE_ENV === 'development'
|
||
? 'http://10.1.5.118:8077/platform-api'
|
||
: 'http://apihis.circleharmonyhospital.cn:8982/platform-api',
|
||
// 请求超时时间(30秒)
|
||
TIMEOUT: 30000,
|
||
};
|
||
|
||
// 创建 axios 实例
|
||
const request: AxiosInstance = axios.create({
|
||
baseURL: API_CONFIG.BASE_URL,
|
||
timeout: API_CONFIG.TIMEOUT,
|
||
headers: {
|
||
'Content-Type': 'application/json;charset=UTF-8',
|
||
},
|
||
});
|
||
|
||
// 请求拦截器
|
||
request.interceptors.request.use(
|
||
(config) => {
|
||
// 可以在这里添加 token 等认证信息
|
||
// const token = localStorage.getItem('token');
|
||
// if (token && config.headers) {
|
||
// config.headers.Authorization = `Bearer ${token}`;
|
||
// }
|
||
return config;
|
||
},
|
||
(error) => {
|
||
return Promise.reject(error);
|
||
}
|
||
);
|
||
|
||
// 响应拦截器
|
||
request.interceptors.response.use(
|
||
(response: AxiosResponse) => {
|
||
return response;
|
||
},
|
||
(error) => {
|
||
// 统一错误处理
|
||
if (error.response) {
|
||
const { status, data } = error.response;
|
||
switch (status) {
|
||
case 400:
|
||
console.error('请求参数错误', data);
|
||
break;
|
||
case 401:
|
||
console.error('未授权,请重新登录', data);
|
||
// 可以在这里处理登录跳转
|
||
break;
|
||
case 403:
|
||
console.error('拒绝访问', data);
|
||
break;
|
||
case 404:
|
||
console.error('请求地址不存在', data);
|
||
break;
|
||
case 500:
|
||
console.error('服务器内部错误', data);
|
||
break;
|
||
default:
|
||
console.error('请求失败', data);
|
||
}
|
||
} else if (error.request) {
|
||
console.error('请求超时或网络错误');
|
||
} else {
|
||
console.error('请求配置错误', error.message);
|
||
}
|
||
return Promise.reject(error);
|
||
}
|
||
);
|
||
|
||
export default request;
|
||
export { API_CONFIG };
|
||
|