100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
|
|
import axios, { AxiosError, type AxiosInstance, type InternalAxiosRequestConfig, type AxiosResponse } from 'axios'
|
||
|
|
import { API_BASE_URL, API_TIMEOUT } from '@/config'
|
||
|
|
|
||
|
|
// 请求响应接口
|
||
|
|
export interface ApiResponse<T = any> {
|
||
|
|
code: number
|
||
|
|
message: string
|
||
|
|
data: T
|
||
|
|
}
|
||
|
|
|
||
|
|
// 请求配置接口
|
||
|
|
export interface RequestConfig extends InternalAxiosRequestConfig {
|
||
|
|
skipAuth?: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建 axios 实例
|
||
|
|
const apiClient: AxiosInstance = axios.create({
|
||
|
|
baseURL: API_BASE_URL,
|
||
|
|
timeout: API_TIMEOUT,
|
||
|
|
headers: {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
},
|
||
|
|
})
|
||
|
|
|
||
|
|
// 请求拦截器
|
||
|
|
apiClient.interceptors.request.use(
|
||
|
|
(config: RequestConfig) => {
|
||
|
|
// 从 localStorage 获取 token
|
||
|
|
const token = localStorage.getItem('access_token')
|
||
|
|
if (token && !config.skipAuth) {
|
||
|
|
config.headers.Authorization = `Bearer ${token}`
|
||
|
|
}
|
||
|
|
return config
|
||
|
|
},
|
||
|
|
(error: AxiosError) => {
|
||
|
|
return Promise.reject(error)
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
// 响应拦截器
|
||
|
|
apiClient.interceptors.response.use(
|
||
|
|
(response: AxiosResponse<ApiResponse>) => {
|
||
|
|
const { data } = response
|
||
|
|
if (data.code === 0) {
|
||
|
|
return response
|
||
|
|
}
|
||
|
|
// 处理业务错误
|
||
|
|
return Promise.reject(new Error(data.message || 'Request failed'))
|
||
|
|
},
|
||
|
|
(error: AxiosError<ApiResponse>) => {
|
||
|
|
if (error.response) {
|
||
|
|
const { status, data } = error.response
|
||
|
|
switch (status) {
|
||
|
|
case 401:
|
||
|
|
// 未授权,跳转登录
|
||
|
|
localStorage.removeItem('access_token')
|
||
|
|
window.location.href = '/login'
|
||
|
|
break
|
||
|
|
case 403:
|
||
|
|
console.error('Forbidden:', data?.message)
|
||
|
|
break
|
||
|
|
case 404:
|
||
|
|
console.error('Not Found:', data?.message)
|
||
|
|
break
|
||
|
|
case 500:
|
||
|
|
console.error('Server Error:', data?.message)
|
||
|
|
break
|
||
|
|
default:
|
||
|
|
console.error('HTTP Error:', status, data?.message)
|
||
|
|
}
|
||
|
|
} else if (error.request) {
|
||
|
|
console.error('Network Error:', error.message)
|
||
|
|
} else {
|
||
|
|
console.error('Error:', error.message)
|
||
|
|
}
|
||
|
|
return Promise.reject(error)
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
// API 方法封装
|
||
|
|
export const api = {
|
||
|
|
get: <T = any>(url: string, config?: RequestConfig): Promise<ApiResponse<T>> => {
|
||
|
|
return apiClient.get(url, config).then(res => res.data)
|
||
|
|
},
|
||
|
|
|
||
|
|
post: <T = any>(url: string, data?: any, config?: RequestConfig): Promise<ApiResponse<T>> => {
|
||
|
|
return apiClient.post(url, data, config).then(res => res.data)
|
||
|
|
},
|
||
|
|
|
||
|
|
put: <T = any>(url: string, data?: any, config?: RequestConfig): Promise<ApiResponse<T>> => {
|
||
|
|
return apiClient.put(url, data, config).then(res => res.data)
|
||
|
|
},
|
||
|
|
|
||
|
|
delete: <T = any>(url: string, config?: RequestConfig): Promise<ApiResponse<T>> => {
|
||
|
|
return apiClient.delete(url, config).then(res => res.data)
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
export default apiClient
|