12345678910111213141516171819 |
- /**
- * 请求拦截
- * @param {Object} http
- */
- module.exports = (vm) => {
- uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
- // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
- config.data = config.data || {}
- // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
- // console.log(vm.$store.state);
- // config.header.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6Miwib3BlbmlkIjoiIiwiaGVhZGltZyI6Imh0dHA6XC9cL3hpYW5ncWluLnpob3VzaS5oZGxrZWppLmNvbVwvdXBsb2FkXC9jMDU1NzhhN2FkNGI2YmE4XC8wYjBkYWM1OGEwNWFlNjFlLnBuZyIsIm5pY2tuYW1lIjoiXHU2ZDRiXHU4YmQ1NDEiLCJwaG9uZSI6IjE1NTg4NTAwODkyIiwidXNlcm5hbWUiOiJcdTViNTlcdThkMzVcdTRlMWMiLCJ2aXBfbGV2ZWwiOjEsImlzX2RlbGV0ZWQiOjEsInN0YXR1cyI6Miwic2V4IjoyLCJjb25zdGVsbGF0aW9uIjpudWxsLCJoZWlnaHQiOm51bGwsIndlaWdodCI6bnVsbCwiZWR1Y2F0aW9uIjoxLCJncmFkdWF0aW9uX3NjaG9vbCI6bnVsbCwid29ya2luZyI6MSwibmF0dXJlIjoxLCJwb3NpdGlvbiI6bnVsbCwiaXNfZHUiOjIsImZhX3dvcmsiOm51bGwsIm1hX3dvcmsiOm51bGwsImhvbWUiOm51bGwsImNyZWF0ZV9hdCI6IjIwMjItMDgtMDMgMTY6NTE6NTUiLCJwcm92aW5jZV9pZCI6MSwiY2l0eV9pZCI6MiwiYXJlYV9pZCI6MywiYWdlIjoxOCwidmlwX251bWJlciI6IjEwMDAyIn0.gXYY9kFMXhZ0F_6BRRo2hTVd2foxuBvkPD2sVDNqr3E'
- if (uni.getStorageSync('token')) {
- // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
- config.header.Authorization = uni.getStorageSync('token')
- }
- return config
- }, (config) => // 可使用async await 做异步操作
- Promise.reject(config))
- }
|