requestInterceptors.js 1.6 KB

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. // config.header.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6Miwib3BlbmlkIjoiIiwiaGVhZGltZyI6Imh0dHA6XC9cL3hpYW5ncWluLnpob3VzaS5oZGxrZWppLmNvbVwvdXBsb2FkXC9jMDU1NzhhN2FkNGI2YmE4XC8wYjBkYWM1OGEwNWFlNjFlLnBuZyIsIm5pY2tuYW1lIjoiXHU2ZDRiXHU4YmQ1NDEiLCJwaG9uZSI6IjE1NTg4NTAwODkyIiwidXNlcm5hbWUiOiJcdTViNTlcdThkMzVcdTRlMWMiLCJ2aXBfbGV2ZWwiOjEsImlzX2RlbGV0ZWQiOjEsInN0YXR1cyI6Miwic2V4IjoyLCJjb25zdGVsbGF0aW9uIjpudWxsLCJoZWlnaHQiOm51bGwsIndlaWdodCI6bnVsbCwiZWR1Y2F0aW9uIjoxLCJncmFkdWF0aW9uX3NjaG9vbCI6bnVsbCwid29ya2luZyI6MSwibmF0dXJlIjoxLCJwb3NpdGlvbiI6bnVsbCwiaXNfZHUiOjIsImZhX3dvcmsiOm51bGwsIm1hX3dvcmsiOm51bGwsImhvbWUiOm51bGwsImNyZWF0ZV9hdCI6IjIwMjItMDgtMDMgMTY6NTE6NTUiLCJwcm92aW5jZV9pZCI6MSwiY2l0eV9pZCI6MiwiYXJlYV9pZCI6MywiYWdlIjoxOCwidmlwX251bWJlciI6IjEwMDAyIn0.gXYY9kFMXhZ0F_6BRRo2hTVd2foxuBvkPD2sVDNqr3E'
  12. if (uni.getStorageSync('token')) {
  13. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  14. config.header.Authorization = uni.getStorageSync('token')
  15. }
  16. return config
  17. }, (config) => // 可使用async await 做异步操作
  18. Promise.reject(config))
  19. }