Skip to content

Commit cee3b54

Browse files
author
piexlMax(奇淼
committed
fix(loading): 修复loading状态管理问题并添加超时处理
1 parent ff7e002 commit cee3b54

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

web/src/utils/request.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,88 @@ const service = axios.create({
1414
let activeAxios = 0
1515
let timer
1616
let loadingInstance
17+
let isLoadingVisible = false
18+
let forceCloseTimer
19+
1720
const showLoading = (
1821
option = {
1922
target: null
2023
}
2124
) => {
2225
const loadDom = document.getElementById('gva-base-load-dom')
2326
activeAxios++
27+
28+
// 清除之前的定时器
2429
if (timer) {
2530
clearTimeout(timer)
2631
}
32+
33+
// 清除强制关闭定时器
34+
if (forceCloseTimer) {
35+
clearTimeout(forceCloseTimer)
36+
}
37+
2738
timer = setTimeout(() => {
28-
if (activeAxios > 0) {
39+
// 再次检查activeAxios状态,防止竞态条件
40+
if (activeAxios > 0 && !isLoadingVisible) {
2941
if (!option.target) option.target = loadDom
3042
loadingInstance = ElLoading.service(option)
43+
isLoadingVisible = true
44+
45+
// 设置强制关闭定时器,防止loading永远不关闭(30秒超时)
46+
forceCloseTimer = setTimeout(() => {
47+
if (isLoadingVisible && loadingInstance) {
48+
console.warn('Loading强制关闭:超时30秒')
49+
loadingInstance.close()
50+
isLoadingVisible = false
51+
activeAxios = 0 // 重置计数器
52+
}
53+
}, 30000)
3154
}
3255
}, 400)
3356
}
3457

3558
const closeLoading = () => {
3659
activeAxios--
3760
if (activeAxios <= 0) {
61+
activeAxios = 0 // 确保不会变成负数
3862
clearTimeout(timer)
39-
loadingInstance && loadingInstance.close()
63+
64+
if (forceCloseTimer) {
65+
clearTimeout(forceCloseTimer)
66+
forceCloseTimer = null
67+
}
68+
69+
if (isLoadingVisible && loadingInstance) {
70+
loadingInstance.close()
71+
isLoadingVisible = false
72+
}
73+
loadingInstance = null
74+
}
75+
}
76+
77+
// 全局重置loading状态的函数,用于异常情况
78+
const resetLoading = () => {
79+
activeAxios = 0
80+
isLoadingVisible = false
81+
82+
if (timer) {
83+
clearTimeout(timer)
84+
timer = null
85+
}
86+
87+
if (forceCloseTimer) {
88+
clearTimeout(forceCloseTimer)
89+
forceCloseTimer = null
90+
}
91+
92+
if (loadingInstance) {
93+
try {
94+
loadingInstance.close()
95+
} catch (e) {
96+
console.warn('关闭loading时出错:', e)
97+
}
98+
loadingInstance = null
4099
}
41100
}
42101
// http request 拦截器
@@ -105,6 +164,8 @@ service.interceptors.response.use(
105164
}
106165

107166
if (!error.response) {
167+
// 网络错误时重置loading状态
168+
resetLoading()
108169
errorBoxVisible = true
109170
ElMessageBox.confirm(
110171
`
@@ -196,4 +257,12 @@ service.interceptors.response.use(
196257
return error
197258
}
198259
)
260+
// 监听页面卸载事件,确保loading被正确清理
261+
if (typeof window !== 'undefined') {
262+
window.addEventListener('beforeunload', resetLoading)
263+
window.addEventListener('unload', resetLoading)
264+
}
265+
266+
// 导出service和resetLoading函数
267+
export { resetLoading }
199268
export default service

0 commit comments

Comments
 (0)