responseInterceptors.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if (!getApp().globalData.isLogin) {
  28. getApp().globalData.isLogin = true
  29. uni.reLaunch({
  30. url: '/pages/login'
  31. })
  32. }
  33. return Promise.resolve()
  34. }
  35. }
  36. uni.showModal({
  37. title: '提示',
  38. content: error.data.message,
  39. showCancel: false
  40. })
  41. return Promise.reject(error)
  42. })
  43. }