This commit is contained in:
Connor
2026-01-12 13:17:11 +08:00
parent 95851f8e69
commit 9600fc542c
132 changed files with 35734 additions and 5 deletions

48
web/src/utils/request.ts Normal file
View File

@@ -0,0 +1,48 @@
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