用工小程序前端代码
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.

30 lines
918 B

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