requestInterceptors.js 765 B

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