requestInterceptors.js 732 B

1234567891011121314151617181920212223242526272829
  1. import Cache from '../cache'
  2. import { getToken } from '../auth'
  3. /**
  4. * 请求拦截
  5. * @param {Object} http
  6. */
  7. module.exports = vm => {
  8. uni.$u.http.interceptors.request.use(
  9. config => { // 可使用async await 做异步操作
  10. config.data = config.data || {}
  11. const brandId = Cache.get('brand_id') ? Cache.get('brand_id') : 18
  12. if (brandId) {
  13. config.header['Brand-Id'] = brandId
  14. }
  15. if (Cache.get('store_id')) {
  16. config.header['Store-Id'] = Cache.get('store_id')
  17. }
  18. if (getToken()) {
  19. config.header['Authorization'] = getToken()
  20. }
  21. return config
  22. }, (config) => { // 可使用async await 做异步操作
  23. return Promise.reject(config)
  24. })
  25. }