123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /**
- * 响应拦截
- * @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)
- })
- }
|