request.js 4.7 KB

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