responseInterceptors.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * 响应拦截
  3. * @param {Object} http
  4. */
  5. export const responseInterceptors = (vm) => {
  6. if(!uni.$uv.http) return
  7. uni.$uv.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  8. const data = response.data
  9. // 自定义参数
  10. const custom = response.config?.custom
  11. try {
  12. uni.hideLoading();
  13. } catch (error) {
  14. }
  15. if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  16. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  17. if (custom.toast !== false) {
  18. // uni.$uv.toast(data.msg)
  19. setTimeout(() => {
  20. uni.showToast({
  21. title: data.msg,
  22. icon: 'none',
  23. duration: 3000
  24. })
  25. }, 300);
  26. }
  27. // const codeList = [100001, 100003, 100004, 100005, 100007]
  28. // if(codeList.includes(data.code)){
  29. // // 获取当前页面实例
  30. // const pages = getCurrentPages();
  31. // // 获取当前页面的实例
  32. // const currentPage = pages[pages.length - 1];
  33. // // 获取页面路径
  34. // const pagePath = currentPage.route;
  35. // // console.log('当前页面路径为:' + pagePath);
  36. // if(pagePath != 'pages/login/index'){
  37. // uni.setStorageSync('token', '')
  38. // uni.reLaunch({
  39. // url: "/pages/login/index"
  40. // })
  41. // }
  42. // }
  43. // 如果需要catch返回,则进行reject
  44. return Promise.reject(response.data)
  45. // if (custom?.catch) {
  46. // return Promise.reject(response.data)
  47. // } else {
  48. // // 否则返回一个pending中的promise
  49. // return new Promise(() => { })
  50. // }
  51. }
  52. return response.data || {}
  53. }, (response) => { /* 对响应错误做点什么 (statusCode !== 200)*/
  54. try {
  55. uni.hideLoading();
  56. } catch (error) {
  57. }
  58. return Promise.reject(response)
  59. })
  60. }