config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. AxiosConfig,
  3. AxiosResponse,
  4. AxiosRequestHeaders,
  5. AxiosError,
  6. InternalAxiosRequestConfig
  7. } from './types'
  8. import { ElMessage, ElMessageBox } from 'element-plus'
  9. import qs from 'qs'
  10. import { useStorage } from '@/hooks/web/useStorage'
  11. import { useTagsViewStore } from '@/store/modules/tagsView'
  12. import { resetRouter } from '@/router'
  13. import router from '@/router'
  14. const { getStorage, clear } = useStorage()
  15. const tagsViewStore = useTagsViewStore()
  16. const config: AxiosConfig = {
  17. /**
  18. * api请求基础路径
  19. */
  20. baseUrl: {
  21. // 开发环境接口前缀
  22. base: '',
  23. // 打包开发环境接口前缀
  24. dev: '',
  25. // 打包生产环境接口前缀
  26. pro: '',
  27. // 打包测试环境接口前缀
  28. test: ''
  29. },
  30. /**
  31. * 接口成功返回状态码
  32. */
  33. code: '200',
  34. /**
  35. * 接口请求超时时间
  36. */
  37. timeout: 60000,
  38. /**
  39. * 默认接口请求类型
  40. * 可选值:application/x-www-form-urlencoded multipart/form-data
  41. */
  42. defaultHeaders: 'application/x-www-form-urlencoded',
  43. interceptors: {
  44. //请求拦截
  45. // requestInterceptors: (config) => {
  46. // return config
  47. // },
  48. // 响应拦截器
  49. // responseInterceptors: (result: AxiosResponse) => {
  50. // return result
  51. // }
  52. }
  53. }
  54. const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
  55. if (
  56. config.method === 'post' &&
  57. (config.headers as AxiosRequestHeaders)['Content-Type'] === 'application/x-www-form-urlencoded'
  58. ) {
  59. config.data = qs.stringify(config.data)
  60. }
  61. if (config.method === 'get' && config.params) {
  62. let url = config.url as string
  63. url += '?'
  64. const keys = Object.keys(config.params)
  65. for (const key of keys) {
  66. if (config.params[key] !== void 0 && config.params[key] !== null) {
  67. url += `${key}=${encodeURIComponent(config.params[key])}&`
  68. }
  69. }
  70. url = url.substring(0, url.length - 1)
  71. config.params = {}
  72. config.url = url
  73. }
  74. if (getStorage('token')) {
  75. config.headers.token = getStorage('token')
  76. }
  77. return config
  78. }
  79. ;(error: AxiosError) => {
  80. Promise.reject(error)
  81. }
  82. const defaultResponseInterceptors = (response: AxiosResponse<any>) => {
  83. if (response?.config?.responseType === 'blob') {
  84. // 如果是文件流,直接过
  85. return response
  86. } else if (response.data.code === config.code) {
  87. return response.data
  88. } else if (response.data.code === 'A0000A') {
  89. ElMessageBox.alert(response.data.message, '提示', {
  90. showClose: false,
  91. callback: () => {
  92. clear()
  93. tagsViewStore.delAllViews()
  94. resetRouter() // 重置静态路由表
  95. router.replace('/login')
  96. }
  97. })
  98. } else {
  99. ElMessage.error(response.data.message)
  100. }
  101. }
  102. ;(error: AxiosError) => {
  103. console.log('err' + error) // for debug
  104. ElMessage.error(error.message)
  105. return Promise.reject(error)
  106. }
  107. export { defaultResponseInterceptors, defaultRequestInterceptors }
  108. export default config