租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
1.1 KiB

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