requestInterceptors.js 972 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * 请求拦截
  3. * @param {Object} http
  4. */
  5. module.exports = (vm) => {
  6. uni.$u.http.interceptors.request.use((config) => {
  7. // 查询当前有没有网络
  8. uni.getNetworkType({
  9. success: (res) => {
  10. if (res.networkType == 'none') {
  11. uni.showToast({
  12. title: '暂无网络连接,请检查您的网络',
  13. icon: 'none'
  14. })
  15. return;
  16. }
  17. }
  18. });
  19. // 自定义参数
  20. const custom = config?.custom
  21. // 如果都需要loading效果的接口
  22. if (custom.loading !== false) {
  23. uni.showLoading({
  24. title: '加载中'
  25. })
  26. wx.showNavigationBarLoading();
  27. }
  28. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  29. config.data = config.data || {}
  30. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  31. // console.log(vm.$store.state);
  32. return config
  33. }, (config) => // 可使用async await 做异步操作
  34. Promise.reject(config))
  35. }