axios.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. let reqCount = 0 // 请求计数器
  9. service.interceptors.request.use(config => {
  10. // is server render, use ${baseUrl} directly rather than ${proxyUrl}
  11. config.url = config.url || '/'
  12. reqCount++
  13. if (process.server) {
  14. if (config.url.indexOf('/inquiry') === 0) {
  15. config.url = process.env.commonUrl + config.url
  16. } else if (config.url.indexOf('/productsuer') === 0) {
  17. config.url = process.env.materialUrl + config.url
  18. } else if (config.url.indexOf('/messages') === 0) {
  19. config.url = process.env.messageUrl + config.url
  20. } else if (config.url.indexOf('/cmsApi') === 0) {
  21. config.url = process.env.cmsUrl + config.url
  22. } else if (config.url.indexOf('/sso') === 0) {
  23. config.url = process.env.ssoUrl + config.url
  24. } else {
  25. config.url = process.env.baseUrl + config.url
  26. }
  27. // let paramStr = ''
  28. // if (config.params) {
  29. // paramStr += '?'
  30. // for (let s in config.params) {
  31. // paramStr += s + '=' + config.params[s] + '&'
  32. // }
  33. // paramStr.substr(0, paramStr.length - 2)
  34. // }
  35. // console.log(config.url + paramStr)
  36. // config.headers.cookie = store.state.option.cookies + '; ' + store.state.option.sessionId
  37. // config.headers['User-Agent']
  38. } else {
  39. document.getElementById('loading').setAttribute('class', 'loading in')
  40. }
  41. return config
  42. }, error => {
  43. return Promise.reject(error)
  44. })
  45. service.interceptors.response.use(response => {
  46. // const cookie = response.headers['set-cookie']
  47. // if (cookie && cookie.length) {
  48. // let cookies = cookie.split(';')
  49. // for (let i = 0; i < cookies.length; i++) {
  50. // let cookieArr = cookies[i].split('=')
  51. // if (cookieArr.length === 2 && cookieArr[0] === 'JSESSIONID') {
  52. // store.commit('option/SET_SESSION_ID', cookieArr[1])
  53. // break
  54. // }
  55. // }
  56. // }
  57. // console.log('ccc', response.headers['set-cookie'])
  58. if (--reqCount <= 0 && !process.server) {
  59. document.getElementById('loading').setAttribute('class', 'loading')
  60. }
  61. return response
  62. }, error => {
  63. if (!process.server) {
  64. if (--reqCount <= 0) {
  65. document.getElementById('loading').setAttribute('class', 'loading')
  66. }
  67. // 401拦截
  68. if (error.response.status === 401 &&
  69. // 当前账套店铺信息
  70. !/\/store-service\/stores$/g.test(error.response.config.url) &&
  71. // 店铺关注
  72. !/\/trade\/storeFocus\/ifFocus/g.test(error.response.config.url) &&
  73. // 器件收藏
  74. !/\/trade\/collection\/list/g.test(error.response.config.url)) {
  75. window.location.href = `/auth/login?returnUrl=${window.location.href}`
  76. return
  77. }
  78. }
  79. return Promise.reject(error)
  80. })
  81. Vue.prototype.$http = service
  82. export default service