axios.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. const isServer = typeof window === 'undefined'
  9. service.interceptors.request.use(config => {
  10. // is server render, use ${baseUrl} directly rather than ${proxyUrl}
  11. config.url = config.url || '/'
  12. if (isServer) {
  13. if (config.url.indexOf('/inquiry') === 0) {
  14. config.url = process.env.commonUrl + config.url
  15. } else {
  16. config.url = process.env.baseUrl + config.url
  17. }
  18. // config.headers.cookie = store.state.option.cookies + '; ' + store.state.option.sessionId
  19. // config.headers['User-Agent']
  20. } else {
  21. document.getElementById('loading').setAttribute('class', 'loading in')
  22. }
  23. return config
  24. }, error => {
  25. return Promise.reject(error)
  26. })
  27. service.interceptors.response.use(response => {
  28. const cookies = response.headers['set-cookie']
  29. if (cookies && cookies.length) {
  30. for (let i = 0; i < cookies.length; i++) {
  31. if (cookies[i].indexOf('JSESSIONID') > -1) {
  32. const sessionId = cookies[i]
  33. const first = sessionId.indexOf(';')
  34. const second = sessionId.lastIndexOf(';')
  35. const newSessionId = sessionId.replace(sessionId.substring(first, second), '')
  36. store.commit('option/SET_SESSION_ID', newSessionId)
  37. break
  38. }
  39. }
  40. }
  41. if (!isServer) {
  42. document.getElementById('loading').setAttribute('class', 'loading')
  43. }
  44. return response
  45. }, error => {
  46. if (!isServer) {
  47. document.getElementById('loading').setAttribute('class', 'loading')
  48. }
  49. return Promise.reject(error)
  50. })
  51. Vue.prototype.$http = service
  52. export default service