init
This commit is contained in:
48
web/src/utils/request.ts
Normal file
48
web/src/utils/request.ts
Normal 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
|
||||
Reference in New Issue
Block a user