1,问题描述:在请求接口时我们封装在全局的接口请求httpPost里在哪里对错误进行弹窗提示处理:代码如下
return new Promise((resolve,reject)=>{ uni.request({ url: ajax.apiUrl + url, data: data, method: 'POST', timeout: timeout,//超时时间,单位 ms header: { 'Content-Type': contentType, 'user_session_key': login.getValue('token') }, success: (res) => { if (200 !== res.statusCode) { uni.showToast({ title: "系统繁忙,请稍后再试", icon: 'none', duration: 2000 }) reject(res); } else { if (res.data.errorCode) { if(res.data.errorCode === '10009'){ uni.navigateTo({ url:'/sub_intake/pages/login/login' }) } else { if(res.data.errorDetails === '余额不足请充值') { uni.showModal({ title: '提示', content: res.data.errorDetails, success: function (res) { if (res.confirm) { console.log('用户点击确定'); uni.navigateTo({ url:'/sub_rechargeWithdrawal/pages/recharge/recharge' }) } else if (res.cancel) { console.log('用户点击取消'); } } }); }else { uni.showToast({ title: res.data.errorDetails, icon: 'none', duration: 2000 }) } reject(res); } throw new Error('Server Error'); } else { resolve(res); } } }, fail: (res) => { //接口调用失败的回调 console.log('请求的数据',res) uni.showToast({ title: res.errMsg, icon: 'none', duration: 3000 }) reject(res) }, complete: (res)=> { alert.hideLoading() } });
2,问题原因:
代码中,complete
回调中调用了alert.hideLoading()
,这可能导致在请求完成后立即隐藏加载提示,因此造成uni.showToast
弹窗提示闪一下就消失。complete
回调会在成功、失败和不论请求是否成功都会执行,因此如果你在请求完成后立即隐藏加载提示,可能导致在 fail
或 success
中的提示无法稳定显示。
3,问题解决:尝试将 alert.hideLoading()
移动到 success
和 fail
回调中,这样就可以确保在请求处理完成后再隐藏加载提示