Files
huobao-drama/web/src/utils/request.ts
Connor 9600fc542c init
2026-01-12 13:17:11 +08:00

49 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { AxiosError, AxiosInstance, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios'
import axios from 'axios'
import { ElMessage } from 'element-plus'
interface CustomAxiosInstance extends Omit<AxiosInstance, 'get' | 'post' | 'put' | 'patch' | 'delete'> {
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>
}
const request = axios.create({
baseURL: '/api/v1',
timeout: 600000, // 10分钟超时匹配后端AI生成接口
headers: {
'Content-Type': 'application/json'
}
}) as CustomAxiosInstance
// 开源版本 - 无需认证token
request.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
return config
},
(error: AxiosError) => {
return Promise.reject(error)
}
)
request.interceptors.response.use(
(response) => {
const res = response.data
if (res.success) {
return res.data
} else {
// 不在这里显示错误提示,让业务代码自行处理
return Promise.reject(new Error(res.error?.message || '请求失败'))
}
},
(error: AxiosError<any>) => {
// 不在拦截器中自动显示错误提示,让业务代码根据具体情况处理
// 只抛出错误供调用者捕获
return Promise.reject(error)
}
)
export default request