responseInterceptors.js 970 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * 响应拦截
  3. * @param {Object} http
  4. */
  5. module.exports = vm => {
  6. uni.$u.http.interceptors.response.use(
  7. response => {
  8. const data = response.data
  9. // 刷新token
  10. const authorization = response.headers?.authorization
  11. if (authorization) {
  12. vm.$store.dispatch('user/token', authorization)
  13. }
  14. if (data.code !== 0) {
  15. uni.showModal({
  16. title: '提示',
  17. content: data.msg,
  18. showCancel: false
  19. })
  20. return Promise.reject(data.msg)
  21. }
  22. return data
  23. }, (error) => {
  24. // 401 登录超时 402 需要登录
  25. if (typeof error.data.status_code !== 'undefined') {
  26. if (error.data.status_code === 401 || error.data.status_code === 402) {
  27. return Promise.resolve()
  28. }
  29. }
  30. uni.showModal({
  31. title: '提示',
  32. content: error.data.message,
  33. showCancel: false
  34. })
  35. return Promise.reject(error)
  36. })
  37. }