request.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // 获取当前时间撮
  112. const interFaceList = store.getters.interFaceList
  113. const curPath = config.url
  114. if(interFaceList){
  115. const hasPath = interFaceList.find(item => {
  116. const hasSp = item.b.find(k => curPath.indexOf(k)>=0)
  117. return hasSp
  118. })
  119. console.log(hasPath)
  120. if(hasPath){
  121. config.headers['biz-updatedate'] = hasPath.timeStr
  122. }
  123. }
  124. // 当前页面路径
  125. const currentPage = router.history.current
  126. if(currentPage&&currentPage.meta&&config.headers['module']){
  127. config.headers['module'] = encodeURIComponent(currentPage.meta.title||'') + ',' +config.headers['module']
  128. }
  129. console.log(decodeURIComponent(config.headers['module']))
  130. config.headers['auth-price'] = authPrice.join(',') // 价格权限
  131. config.retry = 3
  132. config.retryDelay = 1000
  133. store.commit('SET_loadingStatus', true)
  134. return config
  135. }, err)
  136. // response interceptor
  137. service.interceptors.response.use((response) => {
  138. // console.log(response, 'response.data.status')
  139. const et = sessionStorage.getItem('errorTips')
  140. if ((response.data.status == '1001' || response.data.status == '1099') && !et) {
  141. sessionStorage.setItem('errorTips', 1)
  142. modal.destroyAll()
  143. modal.error({
  144. title: '提示',
  145. content: response.data.message,
  146. onOk () {
  147. sessionStorage.setItem('errorTips', 0)
  148. store.state.app.curActionPermission = ''
  149. store.dispatch('Logout').then(() => {
  150. window.location.reload()
  151. })
  152. }
  153. })
  154. }
  155. if ((response.data.status == '401' || response.data.status == '500') && window.location.pathname != '/user/login') {
  156. store.state.app.curActionPermission = ''
  157. notification.destroy()
  158. notification.error({
  159. message: '提示',
  160. description: response.data.message
  161. })
  162. }
  163. if (response.data.status == '900' && window.location.pathname != '/user/login') {
  164. store.state.app.curActionPermission = ''
  165. notification.destroy()
  166. notification.error({
  167. message: response.data.message,
  168. description: response.data.code
  169. })
  170. }
  171. if(response.data.status!='200'){
  172. store.state.app.curActionPermission = ''
  173. store.commit('SET_loadingStatus', false)
  174. }
  175. // 防重复提交时间戳
  176. const interFaceList = store.getters.interFaceList
  177. const curPath = response.config.url
  178. if(interFaceList){
  179. const hasPath = interFaceList.find(item => curPath.indexOf(item.a)>=0)
  180. if(hasPath){
  181. hasPath.timeStr = response.data.data.updateDate
  182. }
  183. }
  184. return response.data
  185. }, err)
  186. const installer = {
  187. vm: {},
  188. install (Vue) {
  189. Vue.use(VueAxios, service)
  190. }
  191. }
  192. export {
  193. installer as VueAxios,
  194. service as axios,
  195. GETNOBASE
  196. }