requestInterceptors.js 659 B

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