request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. console.log(error.response,error.response.status)
  25. // 业务错误提示
  26. if (error.response) {
  27. const status = error.response.status.toString()
  28. if (status.indexOf('5')>=0||status.indexOf('4')>=0) {
  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. const changeOrg = store.getters.changeOrg
  81. if (token) {
  82. config.headers['access-token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  83. config.headers['access-org'] = changeOrg // 当前要切换的账号 sn
  84. }
  85. config.retry = 3
  86. config.retryDelay = 1000
  87. store.commit('SET_loadingStatus', true)
  88. return config
  89. }, err)
  90. // response interceptor
  91. service.interceptors.response.use((response) => {
  92. console.log(response)
  93. const et = sessionStorage.getItem('errorTips')
  94. // 无权限或登录超时
  95. if ((response.data.status == '1001' || response.data.status == '1099') && !et) {
  96. sessionStorage.setItem('errorTips', 1)
  97. modal.destroyAll()
  98. modal.error({
  99. title: '提示',
  100. content: response.data.message,
  101. onOk () {
  102. sessionStorage.setItem('errorTips', 0)
  103. if(response.data.status == '1099'){
  104. store.dispatch('Logout').then(() => {
  105. window.location.reload()
  106. })
  107. }
  108. }
  109. })
  110. } else if (response.data.status != 200 && response.data.status && response.data.errCode != 'VALIDATE_STOCK_LACK') {
  111. // 单据不存在
  112. if(response.data.message.indexOf('不存在')>=0 || response.data.message.indexOf('单据已删除')>=0){
  113. modal.destroyAll()
  114. modal.error({
  115. title: '提示',
  116. content: response.data.message,
  117. onOk () {
  118. window.history.go(-1)
  119. }
  120. })
  121. }else{
  122. // 其它业务错误提示
  123. notification.destroy()
  124. notification.error({
  125. message: '提示',
  126. description: response.data.message
  127. })
  128. }
  129. store.commit('SET_loadingStatus', false)
  130. }
  131. return response.data
  132. }, err)
  133. const installer = {
  134. vm: {},
  135. install (Vue) {
  136. Vue.use(VueAxios, service)
  137. }
  138. }
  139. export {
  140. installer as VueAxios,
  141. service as axios
  142. }