request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import {
  4. baseURL,
  5. contentType,
  6. debounce,
  7. invalidCode,
  8. noPermissionCode,
  9. requestTimeout,
  10. successCode,
  11. tokenName,
  12. loginInterception,
  13. } from '@/config'
  14. import store from '@/store'
  15. import qs from 'qs'
  16. import router from '@/router'
  17. import { isArray } from '@/utils/validate'
  18. let loadingInstance
  19. /**
  20. * @author https://vue-admin-beautiful.com (不想保留author可删除)
  21. * @description 处理code异常
  22. * @param {*} code
  23. * @param {*} msg
  24. */
  25. const handleCode = (code, msg) => {
  26. switch (code) {
  27. case invalidCode:
  28. Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, 'error')
  29. store.dispatch('user/resetAccessToken').catch(() => {})
  30. if (loginInterception) {
  31. location.reload()
  32. }
  33. break
  34. case noPermissionCode:
  35. router.push({ path: '/401' }).catch(() => {})
  36. break
  37. default:
  38. Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, 'error')
  39. break
  40. }
  41. }
  42. const instance = axios.create({
  43. baseURL,
  44. timeout: requestTimeout,
  45. headers: {
  46. 'Content-Type': contentType,
  47. },
  48. })
  49. instance.interceptors.request.use(
  50. (config) => {
  51. if (store.getters['user/accessToken']) {
  52. config.headers[tokenName] = store.getters['user/accessToken']
  53. }
  54. //这里会过滤所有为空、0、false的key,如果不需要请自行注释
  55. if (config.data)
  56. config.data = Vue.prototype.$baseLodash.pickBy(
  57. config.data,
  58. Vue.prototype.$baseLodash.identity
  59. )
  60. if (
  61. config.data &&
  62. config.headers['Content-Type'] ===
  63. 'application/x-www-form-urlencoded;charset=UTF-8'
  64. )
  65. config.data = qs.stringify(config.data)
  66. if (debounce.some((item) => config.url.includes(item)))
  67. loadingInstance = Vue.prototype.$baseLoading()
  68. return config
  69. },
  70. (error) => {
  71. return Promise.reject(error)
  72. }
  73. )
  74. instance.interceptors.response.use(
  75. (response) => {
  76. if (loadingInstance) loadingInstance.close()
  77. const { data, config } = response
  78. const { code, msg } = data
  79. // 操作正常Code数组
  80. const codeVerificationArray = isArray(successCode)
  81. ? [...successCode]
  82. : [...[successCode]]
  83. // 是否操作正常
  84. if (codeVerificationArray.includes(code)) {
  85. return data
  86. } else {
  87. handleCode(code, msg)
  88. return Promise.reject(
  89. 'vue-admin-beautiful请求异常拦截:' +
  90. JSON.stringify({ url: config.url, code, msg }) || 'Error'
  91. )
  92. }
  93. },
  94. (error) => {
  95. if (loadingInstance) loadingInstance.close()
  96. const { response, message } = error
  97. if (error.response && error.response.data) {
  98. const { status, data } = response
  99. handleCode(status, data.msg || message)
  100. return Promise.reject(error)
  101. } else {
  102. let { message } = error
  103. if (message === 'Network Error') {
  104. message = '后端接口连接异常'
  105. }
  106. if (message.includes('timeout')) {
  107. message = '后端接口请求超时'
  108. }
  109. if (message.includes('Request failed with status code')) {
  110. const code = message.substr(message.length - 3)
  111. message = '后端接口' + code + '异常'
  112. }
  113. Vue.prototype.$baseMessage(message || `后端接口未知异常`, 'error')
  114. return Promise.reject(error)
  115. }
  116. }
  117. )
  118. export default instance