requestInterceptors.js 990 B

123456789101112131415161718192021222324252627282930313233343536
  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. mask: true,
  26. })
  27. wx.showNavigationBarLoading();
  28. }
  29. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  30. config.data = config.data || {}
  31. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  32. // console.log(vm.$store.state);
  33. return config
  34. }, (config) => // 可使用async await 做异步操作
  35. Promise.reject(config))
  36. }