request.js 4.0 KB

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