config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { AxiosResponse, InternalAxiosRequestConfig } from './types'
  2. import { ElMessage } from 'element-plus'
  3. import qs from 'qs'
  4. import { SUCCESS_CODE, EXPIRE_CODE, TRANSFORM_REQUEST_DATA } from '../constants'
  5. // @ts-ignore
  6. import { API_URL_MESSAGE_TIP } from '@/utils/messageApi'
  7. import { API_URL_MESSAGE_TIP_COMMON } from '../api/messageApi'
  8. // import { useUserStoreWithOut } from '@common/src/store/modules/user'
  9. import { objToFormData } from '../utils'
  10. import { useUserStoreWithOut } from '../store/modules/user'
  11. import { usePermissionStoreWithOut } from '../store/modules/permission'
  12. const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
  13. if (
  14. config.method === 'post' &&
  15. config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
  16. ) {
  17. config.data = qs.stringify(config.data)
  18. } else if (
  19. TRANSFORM_REQUEST_DATA &&
  20. config.method === 'post' &&
  21. config.headers['Content-Type'] === 'multipart/form-data' &&
  22. !(config.data instanceof FormData)
  23. ) {
  24. config.data = objToFormData(config.data)
  25. }
  26. if (config.method === 'get' && config.params) {
  27. let url = config.url as string
  28. url += '?'
  29. const keys = Object.keys(config.params)
  30. for (const key of keys) {
  31. if (config.params[key] !== void 0 && config.params[key] !== null) {
  32. url += `${key}=${encodeURIComponent(config.params[key])}&`
  33. }
  34. }
  35. url = url.substring(0, url.length - 1)
  36. config.params = {}
  37. config.url = url
  38. }
  39. return config
  40. }
  41. const defaultResponseInterceptors = (response: AxiosResponse) => {
  42. if (response.data.code === EXPIRE_CODE) {
  43. ElMessage.warning('权限认证已过期,即将离开本页面')
  44. setTimeout(() => {
  45. const userStore = useUserStoreWithOut()
  46. if (sessionStorage.getItem('flag')) {
  47. const usePermissionStore = usePermissionStoreWithOut()
  48. userStore.setToken('')
  49. userStore.setUserInfo(undefined)
  50. userStore.setRoleRouters([])
  51. usePermissionStore.resetAddRouters()
  52. window.location.href = response?.data?.attachments?.loginUrl
  53. } else {
  54. userStore.reset()
  55. }
  56. }, 3000)
  57. return
  58. }
  59. if (response.data.success) {
  60. const url: any = response?.config?.url?.split('?')[0]
  61. // 统一处理需要展示提示信息的接口
  62. if (API_URL_MESSAGE_TIP.includes(url) || API_URL_MESSAGE_TIP_COMMON.includes(url)) {
  63. ElMessage.success(response?.data?.message)
  64. }
  65. }
  66. // console.log(response)
  67. if (response?.config?.responseType === 'blob') {
  68. // 如果是文件流,直接过
  69. // 当前业务处理形式 如果是文件流,直接过
  70. return {
  71. data: response.data,
  72. name: response?.headers?.['content-disposition'] || '导出文件.xlsx'
  73. }
  74. } else if (response.data.success || response.data.code === SUCCESS_CODE) {
  75. return response.data
  76. } else {
  77. ElMessage.error(response?.data?.message || 'Error Response')
  78. }
  79. }
  80. export { defaultResponseInterceptors, defaultRequestInterceptors }