| | import axios, { type AxiosInstance, type AxiosError } from 'axios' |
| |
|
| | |
| | export const apiClient: AxiosInstance = axios.create({ |
| | baseURL: import.meta.env.VITE_API_URL || '', |
| | timeout: 30000, |
| | withCredentials: true, |
| | }) |
| |
|
| | |
| | apiClient.interceptors.request.use( |
| | (config) => { |
| | |
| | return config |
| | }, |
| | (error) => { |
| | return Promise.reject(error) |
| | } |
| | ) |
| |
|
| | |
| | apiClient.interceptors.response.use( |
| | (response) => { |
| | return response.data |
| | }, |
| | async (error: AxiosError) => { |
| | |
| | if (error.response?.status === 401) { |
| | const { useAuthStore } = await import('@/stores/auth') |
| | const authStore = useAuthStore() |
| | authStore.isLoggedIn = false |
| |
|
| | const router = await import('@/router') |
| | router.default.push('/login') |
| | } |
| |
|
| | const errorMessage = error.response?.data |
| | ? (error.response.data as any).detail || (error.response.data as any).message |
| | : error.message |
| |
|
| | return Promise.reject(new Error(errorMessage || '请求失败')) |
| | } |
| | ) |
| |
|
| | export default apiClient |
| |
|