responseInterceptors.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * 响应拦截
  3. * @param {Object} http
  4. */
  5. module.exports = (vm) => {
  6. uni.$u.http.interceptors.response.use((response) => {
  7. /* 对响应成功做点什么 可使用async await 做异步操作*/
  8. // uni.hideLoading()
  9. const data = response.data
  10. // 自定义参数
  11. const custom = response.config?.custom
  12. // if (data.code == 0) {
  13. // return data || {}
  14. // } else { // 服务端返回的状态码不等于200,则reject()
  15. // // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  16. // if (custom.toast !== false) {
  17. // uni.$u.toast(data.msg)
  18. // }
  19. // // 如果需要catch返回,则进行reject
  20. // if (custom?.catch) {
  21. // return Promise.reject(data)
  22. // } else {
  23. // // 否则返回一个pending中的promise
  24. // return new Promise(() => {})
  25. // }
  26. // }
  27. return data || {}
  28. }, (response) => {
  29. /* 对响应错误做点什么 (statusCode !== 200)*/
  30. let statusCode = response.statusCode
  31. if (statusCode == 401) {
  32. uni.hideLoading()
  33. // uni.clearStorageSync()
  34. uni.removeStorageSync('token')
  35. uni.removeStorageSync('priceConfig')
  36. uni.setStorageSync('switchs', [1, 0, 0])
  37. uni.$u.toast('请登录后操作')
  38. setTimeout(() => {
  39. uni.reLaunch({
  40. url: '/pages/my'
  41. })
  42. }, 1500)
  43. } else if (statusCode == 402) {
  44. uni.hideLoading()
  45. // uni.clearStorageSync()
  46. uni.removeStorageSync('token')
  47. uni.removeStorageSync('priceConfig')
  48. uni.setStorageSync('switchs', [1, 0, 0])
  49. uni.$u.toast('您已被限制登录,请联系管理员')
  50. setTimeout(() => {
  51. uni.reLaunch({
  52. url: '/pages/my'
  53. })
  54. }, 1500)
  55. } else if (statusCode == 500) {
  56. uni.hideLoading()
  57. uni.$u.toast('接口报错')
  58. }
  59. return Promise.reject(response)
  60. })
  61. }