responseInterceptors.js 1.1 KB

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