requestInterceptors.js 751 B

1234567891011121314151617181920212223
  1. import Cookies from "js-cookie";
  2. /**
  3. * 请求拦截
  4. * @param {Object} http
  5. */
  6. module.exports = (vm) => {
  7. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  8. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  9. uni.showLoading({
  10. title: '加载中'
  11. });
  12. config.data = config.data || {}
  13. let YxToken = Cookies.get('congress')
  14. if (YxToken) {
  15. config.header.Authorization = `Bearer ${YxToken}`
  16. }
  17. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  18. // console.log(vm.$store.state);
  19. return config
  20. }, (config) => // 可使用async await 做异步操作
  21. Promise.reject(config))
  22. }