request.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import notification from 'ant-design-vue/es/notification'
  5. import modal from 'ant-design-vue/es/modal'
  6. import { VueAxios } from './axios'
  7. // 创建 axios 实例
  8. const service = axios.create({
  9. baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  10. timeout: 6000000 // 请求超时时间
  11. })
  12. const err = (error) => {
  13. console.log(error, error.response,error.message,'error')
  14. const config = error.config
  15. // 网络连接出错
  16. if (error.message.indexOf('Network Error') >= 0 || error.message.indexOf('Request failed') >= 0) {
  17. notification.destroy()
  18. notification.error({
  19. message: '提示',
  20. description: '无网络连接,请检查网络是否连接正常'
  21. })
  22. store.commit('SET_loadingStatus', false)
  23. return Promise.reject(err)
  24. }
  25. // 业务错误提示
  26. if (error.response) {
  27. const status = error.response.status
  28. if ((status == 503 || status == 500 || status == 404)) {
  29. notification.destroy()
  30. notification.error({
  31. message: '提示',
  32. description: error.response.data.message
  33. })
  34. }
  35. store.commit('SET_loadingStatus', false)
  36. return Promise.reject(err)
  37. }
  38. // 超时无法访问服务
  39. if (error.message.indexOf('timeout') >= 0) {
  40. notification.destroy()
  41. notification.error({
  42. message: '提示',
  43. description: '网络连接超时,正在重连...'
  44. })
  45. // If config does not exist or the retry option is not set, reject
  46. if (!config || !config.retry) return Promise.reject(err)
  47. // Set the variable for keeping track of the retry count
  48. config.__retryCount = Vue.ls.get(config.url + '__retryCount') || 0
  49. // Check if we've maxed out the total number of retries
  50. if (config.__retryCount >= config.retry) {
  51. Vue.ls.remove(config.url + '__retryCount')
  52. notification.destroy()
  53. notification.error({
  54. message: '提示',
  55. description: '网络重连失败,请检测网络连接是否正常'
  56. })
  57. store.commit('SET_loadingStatus', false)
  58. // Reject with the error
  59. return Promise.reject(err)
  60. }
  61. // Increase the retry count
  62. config.__retryCount += 1
  63. Vue.ls.set(config.url + '__retryCount', config.__retryCount)
  64. // Create new promise to handle exponential backoff
  65. config.url = config.url.replace('/api', '')
  66. const backoff = new Promise(function (resolve) {
  67. setTimeout(function () {
  68. resolve()
  69. }, config.retryDelay || 1)
  70. })
  71. // Return the promise in which recalls axios to retry the request
  72. return backoff.then(function () {
  73. return service(config)
  74. })
  75. }
  76. }
  77. // request interceptor
  78. service.interceptors.request.use(config => {
  79. const token = store.getters.token
  80. if (token) {
  81. config.headers['access-token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  82. }
  83. config.retry = 3
  84. config.retryDelay = 1000
  85. store.commit('SET_loadingStatus', true)
  86. return config
  87. }, err)
  88. // response interceptor
  89. service.interceptors.response.use((response) => {
  90. // console.log(response, 'response.data.status')
  91. const et = sessionStorage.getItem('errorTips')
  92. if ((response.data.status == '1001' || response.data.status == '1099') && !et) {
  93. sessionStorage.setItem('errorTips', 1)
  94. modal.destroyAll()
  95. modal.error({
  96. title: '提示',
  97. content: response.data.message,
  98. onOk () {
  99. sessionStorage.setItem('errorTips', 0)
  100. store.dispatch('Logout').then(() => {
  101. window.location.reload()
  102. })
  103. }
  104. })
  105. }
  106. if ((response.data.status == '401' || response.data.status == '500') && window.location.pathname != '/user/login') {
  107. notification.destroy()
  108. notification.error({
  109. message: '提示',
  110. description: response.data.message
  111. })
  112. }
  113. if (response.data.status == '900' && window.location.pathname != '/user/login') {
  114. notification.destroy()
  115. notification.error({
  116. message: response.data.message,
  117. description: response.data.code
  118. })
  119. }
  120. store.commit('SET_loadingStatus', false)
  121. return response.data
  122. }, err)
  123. const installer = {
  124. vm: {},
  125. install (Vue) {
  126. Vue.use(VueAxios, service)
  127. }
  128. }
  129. export {
  130. installer as VueAxios,
  131. service as axios
  132. }