requestInterceptors.js 680 B

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