axios.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '~store'
  4. const service = axios.create({
  5. withCredentials: true,
  6. baseURL: '/'
  7. })
  8. service.interceptors.request.use(config => {
  9. // is server render, use ${baseUrl} directly rather than ${proxyUrl}
  10. if (typeof window === 'undefined') {
  11. if (config.url.indexOf('/inquiry') === 0) {
  12. config.url = process.env.commonUrl + config.url
  13. } else {
  14. config.url = process.env.baseUrl + config.url
  15. }
  16. config.headers.cookie = store.state.option.cookies + '; ' + store.state.option.sessionId
  17. config.headers['User-Agent'] = store.state.option.userAgent
  18. }
  19. return config
  20. }, error => {
  21. return Promise.reject(error)
  22. })
  23. service.interceptors.response.use(response => {
  24. const cookies = response.headers['set-cookie']
  25. if (cookies && cookies.length) {
  26. for (let i = 0; i < cookies.length; i++) {
  27. if (cookies[i].indexOf('JSESSIONID') > -1) {
  28. const sessionId = cookies[i]
  29. const first = sessionId.indexOf(';')
  30. const second = sessionId.lastIndexOf(';')
  31. const newSessionId = sessionId.replace(sessionId.substring(first, second), '')
  32. store.commit('option/SET_SESSION_ID', newSessionId)
  33. break
  34. }
  35. }
  36. }
  37. return response
  38. }, error => {
  39. return Promise.reject(error)
  40. })
  41. Vue.prototype.$http = service
  42. export default service