主动取消重复请求关闭loading

This commit is contained in:
lzp
2026-04-01 14:04:33 +08:00
parent 4860400a70
commit 883ca6b5cb

View File

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