环卫车小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.2 KiB

7 months ago
  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. // return config
  12. // }, (config) => // 可使用async await 做异步操作
  13. // Promise.reject(config))
  14. // 请求拦截
  15. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  16. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  17. config.data = config.data || {}
  18. // 根据custom参数中配置的是否需要token,添加对应的请求头
  19. if(config?.custom?.auth) {
  20. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  21. config.header.token = vm.$store.state.user.token
  22. }
  23. return config
  24. }, config => { // 可使用async await 做异步操作
  25. return Promise.reject(config)
  26. })
  27. }