responseInterceptors.js 1.1 KB

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