http.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import axios from 'axios'
  2. import store from '@/store'
  3. import router from '../router'
  4. import Vue from 'vue'
  5. let vue = new Vue()
  6. // import qs from 'qs'
  7. const exceptUrls = ['/sso/user/logout', '/sso/user/sendUserInfo', '/user/order/queryOrderStatus', '/user/checkToken']
  8. axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? 'https://www.4dkankan.com/api' : '/api'
  9. // axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? 'http://192.168.0.207:8087/api' : '/api'
  10. // 请求超时时限
  11. axios.defaults.timeout = 1500000
  12. // 请求次数
  13. axios.defaults.retry = 2
  14. // 请求的间隙
  15. axios.defaults.retryDelay = 1000
  16. // 拦截请求,做登陆,或head处理
  17. axios.interceptors.request.use(function (config) {
  18. if (config.method === 'post') {
  19. config.data = {
  20. ...config.data,
  21. rnd: Math.random()
  22. }
  23. // config.data = qs.stringify(config.data)
  24. } else if (config.method === 'get') {
  25. config.params = {
  26. rnd: Math.random(),
  27. ...config.params
  28. }
  29. // config.params = qs.stringify(config.params)
  30. }
  31. for (let i = 0; i < exceptUrls.length; i++) {
  32. let url = exceptUrls[i]
  33. if (config.url === url) {
  34. return config
  35. }
  36. }
  37. // vue.$toast.showLoading()
  38. return config
  39. }, function (error) {
  40. // 对请求错误做些什么
  41. return Promise.reject(error)
  42. })
  43. // // 拦截返回,做错误统一处理
  44. axios.interceptors.response.use(
  45. response => {
  46. vue.$toast.hideLoading()
  47. let code = Number(response.data.code)
  48. for (let i = 0; i < exceptUrls.length; i++) {
  49. let url = response.config.baseURL + exceptUrls[i]
  50. if (response.config.url === url) {
  51. return response
  52. }
  53. }
  54. switch (code) {
  55. case 3004:
  56. let msg = localStorage.getItem('language') === 'en' ? 'Please re-log in.' : '请重新登录'
  57. store.state.user.token && vue.$toast.show('warn', msg, async () => {
  58. await store.dispatch('logout')
  59. router.push({name: 'home'})
  60. })
  61. break
  62. default:
  63. break
  64. }
  65. return response
  66. },
  67. error => {
  68. // 请求超时的之后,抛出 error.code = ECONNABORTED的错误..错误信息是 timeout of xxx ms exceeded
  69. let err = localStorage.getItem('language') === 'en' ? 'Network request failed' : '网络请求失败'
  70. if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
  71. var config = error.config
  72. config.__retryCount = config.__retryCount || 0
  73. if (config.__retryCount >= config.retry) {
  74. // Reject with the error
  75. // window.location.reload();
  76. vue.$toast.show('error', err, function () {
  77. vue.$toast.hideLoading()
  78. })
  79. return Promise.reject(error)
  80. }
  81. // Increase the retry count
  82. config.__retryCount += 1
  83. // Create new promise to handle exponential backoff
  84. var backoff = new Promise(function (resolve) {
  85. setTimeout(function () {
  86. resolve()
  87. }, config.retryDelay || 1)
  88. })
  89. return backoff.then(function () {
  90. return axios(config)
  91. })
  92. } else {
  93. vue.$toast.show('error', err, function () {
  94. vue.$toast.hideLoading()
  95. })
  96. return Promise.reject(error)
  97. }
  98. }
  99. )
  100. export default axios