主动取消重复请求关闭loading
This commit is contained in:
@@ -32,16 +32,11 @@ axios.defaults.withCredentials = true //跨域携带cookie
|
|||||||
// request拦截器
|
// request拦截器
|
||||||
service.interceptors.request.use(
|
service.interceptors.request.use(
|
||||||
(config: any) => {
|
(config: any) => {
|
||||||
removePending(config)
|
if (removePending(config) && config.loading) closeLoading()
|
||||||
// 如果repeatRequest不配置,那么默认该请求就取消重复接口请求
|
// 如果repeatRequest不配置,那么默认该请求就取消重复接口请求
|
||||||
!config.repeatRequest && addPending(config)
|
!config.repeatRequest && addPending(config)
|
||||||
// 打开loading
|
// 打开loading
|
||||||
if (config.loading) {
|
if (config.loading) openLoading(config.loadingDom)
|
||||||
LoadingInstance._count++
|
|
||||||
if (LoadingInstance._count === 1) {
|
|
||||||
openLoading(config.loadingDom)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 如果登录了,有token,则请求携带token
|
// 如果登录了,有token,则请求携带token
|
||||||
// Do something before request is sent
|
// Do something before request is sent
|
||||||
const token = useUserInfoStore().state.token
|
const token = useUserInfoStore().state.token
|
||||||
@@ -165,6 +160,7 @@ function removePending(config: any) {
|
|||||||
const cancelToken = pendingMap.get(pendingKey)
|
const cancelToken = pendingMap.get(pendingKey)
|
||||||
cancelToken(pendingKey)
|
cancelToken(pendingKey)
|
||||||
pendingMap.delete(pendingKey)
|
pendingMap.delete(pendingKey)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ----------------------------------loading的函数-------------------------------
|
// ----------------------------------loading的函数-------------------------------
|
||||||
@@ -172,7 +168,10 @@ const LoadingInstance: { _count: number } = {
|
|||||||
_count: 0
|
_count: 0
|
||||||
}
|
}
|
||||||
function openLoading(loadingDom: any) {
|
function openLoading(loadingDom: any) {
|
||||||
|
LoadingInstance._count++
|
||||||
|
// if(LoadingInstance._count === 1) {
|
||||||
useGlobalStore().setLoading(true)
|
useGlobalStore().setLoading(true)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
function closeLoading() {
|
function closeLoading() {
|
||||||
if (LoadingInstance._count > 0) LoadingInstance._count--
|
if (LoadingInstance._count > 0) LoadingInstance._count--
|
||||||
|
|||||||
Reference in New Issue
Block a user