axios.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. } else {
  19. document.getElementById('loading').setAttribute('class', 'loading in')
  20. }
  21. return config
  22. }, error => {
  23. return Promise.reject(error)
  24. })
  25. service.interceptors.response.use(response => {
  26. const cookies = response.headers['set-cookie']
  27. if (cookies && cookies.length) {
  28. for (let i = 0; i < cookies.length; i++) {
  29. if (cookies[i].indexOf('JSESSIONID') > -1) {
  30. const sessionId = cookies[i]
  31. const first = sessionId.indexOf(';')
  32. const second = sessionId.lastIndexOf(';')
  33. const newSessionId = sessionId.replace(sessionId.substring(first, second), '')
  34. store.commit('option/SET_SESSION_ID', newSessionId)
  35. break
  36. }
  37. }
  38. }
  39. if (typeof window !== 'undefined') {
  40. document.getElementById('loading').setAttribute('class', 'loading')
  41. }
  42. return response
  43. }, error => {
  44. return Promise.reject(error)
  45. })
  46. Vue.prototype.$http = service
  47. export default service