responseInterceptors.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * 响应拦截
  3. * @param {Object} http
  4. */
  5. // 日志打印
  6. import reportErr from '@/util/errorReport/log.js';
  7. module.exports = (vm) => {
  8. uni.$u.http.interceptors.response.use((response) => {
  9. uni.hideLoading();
  10. wx.hideNavigationBarLoading();
  11. // 自定义参数
  12. const custom = response.config?.custom
  13. // 响应的数据
  14. const data = response.data
  15. if (data.code !== '00000' && data.code !== 0) {
  16. // 错误上报
  17. reportErr('请求响应错误信息!!!', response);
  18. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  19. if (custom.toast !== false) {
  20. uni.$u.toast(data.message)
  21. }
  22. // 如果需要catch返回
  23. if (custom?.catch) {
  24. return data
  25. } else {
  26. // 否则返回一个pending中的promise
  27. return new Promise(() => {})
  28. }
  29. }
  30. // 如果需要catch返回
  31. if (custom?.catch) {
  32. return data
  33. } else {
  34. return data.data
  35. }
  36. }, (response) => {
  37. switch (response.statusCode) {
  38. case 404:
  39. // 错误上报
  40. reportErr('接口路径找不到!!!', response);
  41. uni.$u.toast('接口路径找不到!!!')
  42. break;
  43. case 500:
  44. // 错误上报
  45. reportErr('服务器出错了!!!', response);
  46. uni.$u.toast('服务器出错了!!!')
  47. break;
  48. default:
  49. // 错误上报
  50. reportErr('出错!!!', response)
  51. break;
  52. }
  53. setTimeout(() => {
  54. uni.hideLoading();
  55. wx.hideNavigationBarLoading();
  56. }, 500)
  57. return Promise.reject(response)
  58. })
  59. }