request.js 6.5 KB

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