/** * 响应拦截 * @param {Object} http */ // 日志打印 import reportErr from '@/util/errorReport/log.js'; module.exports = (vm) => { uni.$u.http.interceptors.response.use((response) => { uni.hideLoading(); wx.hideNavigationBarLoading(); // 自定义参数 const custom = response.config?.custom // 响应的数据 const data = response.data if (data.code !== '00000' && data.code !== 0) { // 错误上报 reportErr('请求响应错误信息!!!', response); // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示 if (custom.toast !== false) { uni.$u.toast(data.message) } // 如果需要catch返回 if (custom?.catch) { return data } else { // 否则返回一个pending中的promise return new Promise(() => {}) } } // 如果需要catch返回 if (custom?.catch) { return data } else { return data.data } }, (response) => { switch (response.statusCode) { case 404: // 错误上报 reportErr('接口路径找不到!!!', response); uni.$u.toast('接口路径找不到!!!') break; case 500: // 错误上报 reportErr('服务器出错了!!!', response); uni.$u.toast('服务器出错了!!!') break; default: // 错误上报 reportErr('出错!!!', response) break; } setTimeout(() => { uni.hideLoading(); wx.hideNavigationBarLoading(); }, 500) return Promise.reject(response) }) }