request.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 { VueAxios } from './axios'
  8. import { getUserOsInfo, getBrowserType} from './util'
  9. // 创建 axios 实例
  10. const service = axios.create({
  11. baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  12. timeout: 1000000 // 请求超时时间
  13. })
  14. const err = (error) => {
  15. const config = error.config
  16. // 网络连接出错
  17. if (error.message.indexOf('Network Error') >= 0) {
  18. notification.destroy()
  19. notification.error({
  20. message: '提示',
  21. description: '无网络连接,请检查网络是否连接正常'
  22. })
  23. store.commit('SET_loadingStatus', false)
  24. return Promise.reject(err)
  25. }
  26. console.log(error.response,error.response.status)
  27. // 业务错误提示
  28. if (error.response) {
  29. const status = error.response.status.toString()
  30. if (status.indexOf('5')>=0||status.indexOf('4')>=0) {
  31. notification.destroy()
  32. notification.error({
  33. message: '提示',
  34. description: error.response.data.message
  35. })
  36. }
  37. store.commit('SET_loadingStatus', false)
  38. return Promise.reject(err)
  39. }
  40. // 超时无法访问服务
  41. if (error.message.indexOf('timeout') >= 0) {
  42. notification.destroy()
  43. notification.error({
  44. message: '提示',
  45. description: '网络连接超时,正在重连...'
  46. })
  47. // If config does not exist or the retry option is not set, reject
  48. if (!config || !config.retry) return Promise.reject(err)
  49. // Set the variable for keeping track of the retry count
  50. config.__retryCount = Vue.ls.get(config.url + '__retryCount') || 0
  51. // Check if we've maxed out the total number of retries
  52. if (config.__retryCount >= config.retry) {
  53. Vue.ls.remove(config.url + '__retryCount')
  54. notification.destroy()
  55. notification.error({
  56. message: '提示',
  57. description: '网络重连失败,请检测网络连接是否正常'
  58. })
  59. store.commit('SET_loadingStatus', false)
  60. // Reject with the error
  61. return Promise.reject(err)
  62. }
  63. // Increase the retry count
  64. config.__retryCount += 1
  65. Vue.ls.set(config.url + '__retryCount', config.__retryCount)
  66. // Create new promise to handle exponential backoff
  67. config.url = config.url.replace('/api', '')
  68. const backoff = new Promise(function (resolve) {
  69. setTimeout(function () {
  70. resolve()
  71. }, config.retryDelay || 1)
  72. })
  73. // Return the promise in which recalls axios to retry the request
  74. return backoff.then(function () {
  75. return service(config)
  76. })
  77. }
  78. }
  79. // request interceptor
  80. service.interceptors.request.use(config => {
  81. const token = store.getters.token
  82. const changeOrg = store.getters.changeOrg
  83. if (token) {
  84. config.headers['access-token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  85. config.headers['access-org'] = changeOrg // 当前要切换的账号 sn
  86. }
  87. // 操作系统、浏览器信息
  88. config.headers['os'] = getUserOsInfo()
  89. config.headers['bws'] = getBrowserType()
  90. // 分辨率
  91. config.headers['sdr'] = window.screen.width+'X'+window.screen.height
  92. // 当前页面路径
  93. const currentPage = router.history.current
  94. if(currentPage&&currentPage.meta&&config.headers['module']){
  95. config.headers['module'] = encodeURIComponent(currentPage.meta.title||'') + ',' +config.headers['module']
  96. }
  97. // console.log(config.url)
  98. // console.log(decodeURIComponent(config.headers['module']))
  99. config.retry = 3
  100. config.retryDelay = 1000
  101. store.commit('SET_loadingStatus', true)
  102. return config
  103. }, err)
  104. // response interceptor
  105. service.interceptors.response.use((response) => {
  106. console.log(response)
  107. const et = sessionStorage.getItem('errorTips')
  108. // 无权限或登录超时
  109. if ((response.data.status == '1001' || response.data.status == '1099') && !et) {
  110. sessionStorage.setItem('errorTips', 1)
  111. modal.destroyAll()
  112. modal.error({
  113. title: '提示',
  114. content: response.data.message,
  115. onOk () {
  116. sessionStorage.setItem('errorTips', 0)
  117. if(response.data.status == '1099'){
  118. store.dispatch('Logout').then(() => {
  119. window.location.reload()
  120. })
  121. }
  122. }
  123. })
  124. } else if (response.data.status != 200 && response.data.status && response.data.errCode != 'VALIDATE_STOCK_LACK') {
  125. // 单据不存在
  126. if(response.data.message.indexOf('不存在')>=0 || response.data.message.indexOf('单据已删除')>=0){
  127. modal.destroyAll()
  128. modal.error({
  129. title: '提示',
  130. content: response.data.message,
  131. onOk () {
  132. // window.history.go(-1)
  133. }
  134. })
  135. }else{
  136. // 其它业务错误提示
  137. notification.destroy()
  138. notification.error({
  139. message: '提示',
  140. description: response.data.message
  141. })
  142. }
  143. store.commit('SET_loadingStatus', false)
  144. }
  145. // 检测版本号是否变更
  146. if(response.config.url.indexOf('/logout')<0){
  147. if(!store.state.app.appVersion){
  148. if(response.data.version){
  149. store.commit('SET_appVersion', response.data.version)
  150. }
  151. }else{
  152. if(store.state.app.appVersion != response.data.version){
  153. store.dispatch('getAllLookUp')
  154. store.commit('SET_appVersion', response.data.version)
  155. }
  156. }
  157. }
  158. return response.data
  159. }, err)
  160. const installer = {
  161. vm: {},
  162. install (Vue) {
  163. Vue.use(VueAxios, service)
  164. }
  165. }
  166. export {
  167. installer as VueAxios,
  168. service as axios
  169. }