responseInterceptors.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * 响应拦截
  3. * @param {Object} http
  4. */
  5. module.exports = (vm) => {
  6. uni.$u.http.interceptors.response.use(
  7. (response) => {
  8. /* 对响应成功做点什么 可使用async await 做异步操作*/
  9. const data = response.data;
  10. // 自定义参数
  11. const custom = response.config?.custom;
  12. if(custom?.isAppApi){
  13. // if(response.code!=200) {
  14. // return Promise.reject(data);
  15. // }
  16. return data.data || {};
  17. }
  18. if (data.errCode !== 0 && data.code !== 0) {
  19. // 服务端返回的状态码不等于200,则reject()
  20. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  21. // if (custom.toast !== false) {
  22. // uni.$u.toast(data.message)
  23. // }
  24. // 如果需要catch返回,则进行reject
  25. // if (custom?.catch) {
  26. //console.error('http catch rejected', data)
  27. return Promise.reject(data);
  28. // } else {
  29. // // 否则返回一个pending中的promise
  30. // return new Promise(() => { })
  31. // }
  32. }
  33. return data.data || {};
  34. },
  35. (response) => {
  36. /* 对响应错误做点什么 (statusCode !== 200)*/
  37. return Promise.reject(response);
  38. },
  39. );
  40. };