Files
invest-mind-store/apps/web/src/services/api.ts

96 lines
3.0 KiB
TypeScript

import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { envConfig } from '../config/env';
// 创建 axios 实例
const apiClient: AxiosInstance = axios.create({
baseURL: envConfig.apiBaseUrl,
timeout: 30000, // 30秒超时
headers: {
'Content-Type': 'application/json',
},
});
// 请求拦截器
apiClient.interceptors.request.use(
(config) => {
// 可以在这里添加 token 等认证信息
// const token = localStorage.getItem('token')
// if (token) {
// config.headers.Authorization = `Bearer ${token}`
// }
if (envConfig.debug) {
console.log('请求:', config.method?.toUpperCase(), config.url);
}
return config;
},
(error) => {
if (envConfig.debug) {
console.error('请求错误:', error);
}
return Promise.reject(error);
}
);
// 响应拦截器
apiClient.interceptors.response.use(
(response: AxiosResponse) => {
if (envConfig.debug) {
console.log('响应:', response.config.url, response.data);
}
return response;
},
(error) => {
if (envConfig.debug) {
console.error('响应错误:', error.response?.data || error.message);
}
// 处理常见错误
if (error.response) {
switch (error.response.status) {
case 401:
// 未授权,可以跳转到登录页
// window.location.href = '/login'
break;
case 403:
console.error('没有权限访问该资源');
break;
case 404:
console.error('请求的资源不存在');
break;
case 500:
console.error('服务器内部错误');
break;
default:
console.error('请求失败:', error.response.data?.message || error.message);
}
} else if (error.request) {
console.error('网络错误,请检查网络连接');
}
return Promise.reject(error);
}
);
// 导出常用的请求方法
export const api = {
get: <T = any>(url: string, config?: AxiosRequestConfig) =>
apiClient.get<T>(url, config).then((res) => res.data),
post: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
apiClient.post<T>(url, data, config).then((res) => res.data),
put: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
apiClient.put<T>(url, data, config).then((res) => res.data),
patch: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
apiClient.patch<T>(url, data, config).then((res) => res.data),
delete: <T = any>(url: string, config?: AxiosRequestConfig) =>
apiClient.delete<T>(url, config).then((res) => res.data),
};
// 导出 axios 实例(用于特殊需求)
export default apiClient;