request.js 5.2 KB

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