request.js 6.4 KB

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