代码实现
思路:记录请求数量,开始请求+1,完成请求回调-1;记录是否正在加载中,如果正加载中不重新开启动画(避免闪烁)
let loadingCount = 0 // 记录当前正在请求的数量
let isLoading = false // 是否存着loading
return new Promise(function (res, rej) {
if (loadingCount === 0 && !isLoading) {
wx.showLoading({
title: '加载中',
})
isLoading = true
}
loadingCount += 1;
wx.request({
url: obj.url ? obj.url : baseApi,
data: obj.data ? obj.data : {},
method: obj.method ? obj.method : 'GET',
header: header,
success: function (response) {
res(response.data);
},
fail: function (err) {
rej(err);
},
complete: function(){
if (loadingCount <= 0) {
return;
}
console.log(loadingCount)
loadingCount -= 1;
if (loadingCount === 0 && isLoading) {
wx.hideLoading()
isLoading = false
}
}
})
})