2023-09-27 15:39:39 +08:00
|
|
|
|
import axios, { AxiosError } from 'axios'
|
2023-11-30 11:34:39 +08:00
|
|
|
|
import { defaultRequestInterceptors, defaultResponseInterceptors } from './config'
|
2023-04-28 17:50:58 +08:00
|
|
|
|
|
2023-06-08 09:47:18 +08:00
|
|
|
|
import { AxiosInstance, InternalAxiosRequestConfig, RequestConfig, AxiosResponse } from './types'
|
2023-09-27 15:39:39 +08:00
|
|
|
|
import { ElMessage } from 'element-plus'
|
2023-11-30 11:34:39 +08:00
|
|
|
|
import { REQUEST_TIMEOUT } from '@/constants'
|
2023-04-28 17:50:58 +08:00
|
|
|
|
|
2023-10-27 13:54:01 +08:00
|
|
|
|
export const PATH_URL = import.meta.env.VITE_API_BASE_PATH
|
2023-04-28 17:50:58 +08:00
|
|
|
|
|
|
|
|
|
const abortControllerMap: Map<string, AbortController> = new Map()
|
|
|
|
|
|
2023-05-04 14:20:10 +08:00
|
|
|
|
const axiosInstance: AxiosInstance = axios.create({
|
2023-11-30 11:34:39 +08:00
|
|
|
|
timeout: REQUEST_TIMEOUT,
|
2023-05-04 14:20:10 +08:00
|
|
|
|
baseURL: PATH_URL
|
|
|
|
|
})
|
2023-04-28 17:50:58 +08:00
|
|
|
|
|
|
|
|
|
axiosInstance.interceptors.request.use((res: InternalAxiosRequestConfig) => {
|
|
|
|
|
const controller = new AbortController()
|
|
|
|
|
const url = res.url || ''
|
|
|
|
|
res.signal = controller.signal
|
2024-02-01 16:43:16 +08:00
|
|
|
|
abortControllerMap.set(import.meta.env.VITE_USE_MOCK === 'true' ? url.replace('/mock', '') : url, controller)
|
2023-04-28 17:50:58 +08:00
|
|
|
|
return res
|
2022-08-13 09:32:13 +08:00
|
|
|
|
})
|
|
|
|
|
|
2023-04-28 17:50:58 +08:00
|
|
|
|
axiosInstance.interceptors.response.use(
|
|
|
|
|
(res: AxiosResponse) => {
|
|
|
|
|
const url = res.config.url || ''
|
|
|
|
|
abortControllerMap.delete(url)
|
2023-09-18 16:34:08 +08:00
|
|
|
|
// 这里不能做任何处理,否则后面的 interceptors 拿不到完整的上下文了
|
|
|
|
|
return res
|
2022-08-13 09:32:13 +08:00
|
|
|
|
},
|
2023-09-27 15:39:39 +08:00
|
|
|
|
(error: AxiosError) => {
|
2023-10-07 16:43:03 +08:00
|
|
|
|
console.log('err: ' + error) // for debug
|
2023-09-27 15:39:39 +08:00
|
|
|
|
ElMessage.error(error.message)
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
2022-08-13 09:32:13 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-11-30 11:34:39 +08:00
|
|
|
|
axiosInstance.interceptors.request.use(defaultRequestInterceptors)
|
|
|
|
|
axiosInstance.interceptors.response.use(defaultResponseInterceptors)
|
2023-05-04 14:20:10 +08:00
|
|
|
|
|
|
|
|
|
const service = {
|
|
|
|
|
request: (config: RequestConfig) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (config.interceptors?.requestInterceptors) {
|
|
|
|
|
config = config.interceptors.requestInterceptors(config as any)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
axiosInstance
|
|
|
|
|
.request(config)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
resolve(res)
|
|
|
|
|
})
|
|
|
|
|
.catch((err: any) => {
|
|
|
|
|
reject(err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
cancelRequest: (url: string | string[]) => {
|
|
|
|
|
const urlList = Array.isArray(url) ? url : [url]
|
|
|
|
|
for (const _url of urlList) {
|
|
|
|
|
abortControllerMap.get(_url)?.abort()
|
|
|
|
|
abortControllerMap.delete(_url)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
cancelAllRequest() {
|
|
|
|
|
for (const [_, controller] of abortControllerMap) {
|
|
|
|
|
controller.abort()
|
|
|
|
|
}
|
|
|
|
|
abortControllerMap.clear()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default service
|