requestInterceptors.js 928 B

12345678910111213141516171819202122232425
  1. import store from "@/store/index.js"
  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. config.data = config.data || {}
  10. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  11. // console.log(vm.$store.state);
  12. config.header = Object
  13. .assign({}, { "Access-Control-Allow-Origin": "*", "content-type": "application/json;charset=UTF-8",
  14. cid: "" }, config.header)
  15. if (store.state.token) {
  16. config.header.Authorization = store.state.token
  17. }
  18. if (config.custom.loading != false) {
  19. uni.showLoading();
  20. }
  21. //console.log("=======>", config)
  22. return config
  23. }, (config) => // 可使用async await 做异步操作
  24. Promise.reject(config))
  25. }