123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import { AxiosResponse, InternalAxiosRequestConfig } from './types'
- import { ElMessage } from 'element-plus'
- import qs from 'qs'
- import { SUCCESS_CODE, EXPIRE_CODE, TRANSFORM_REQUEST_DATA } from '../constants'
- // @ts-ignore
- import { API_URL_MESSAGE_TIP } from '@/utils/messageApi'
- import { API_URL_MESSAGE_TIP_COMMON } from '../api/messageApi'
- // import { useUserStoreWithOut } from '@common/src/store/modules/user'
- import { objToFormData } from '../utils'
- import { useUserStoreWithOut } from '../store/modules/user'
- import { usePermissionStoreWithOut } from '../store/modules/permission'
- const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
- if (
- config.method === 'post' &&
- config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
- ) {
- config.data = qs.stringify(config.data)
- } else if (
- TRANSFORM_REQUEST_DATA &&
- config.method === 'post' &&
- config.headers['Content-Type'] === 'multipart/form-data' &&
- !(config.data instanceof FormData)
- ) {
- config.data = objToFormData(config.data)
- }
- if (config.method === 'get' && config.params) {
- let url = config.url as string
- url += '?'
- const keys = Object.keys(config.params)
- for (const key of keys) {
- if (config.params[key] !== void 0 && config.params[key] !== null) {
- url += `${key}=${encodeURIComponent(config.params[key])}&`
- }
- }
- url = url.substring(0, url.length - 1)
- config.params = {}
- config.url = url
- }
- return config
- }
- const defaultResponseInterceptors = (response: AxiosResponse) => {
- if (response.data.code === EXPIRE_CODE) {
- ElMessage.warning('权限认证已过期,即将离开本页面')
- setTimeout(() => {
- const userStore = useUserStoreWithOut()
- if (sessionStorage.getItem('flag')) {
- const usePermissionStore = usePermissionStoreWithOut()
- userStore.setToken('')
- userStore.setUserInfo(undefined)
- userStore.setRoleRouters([])
- usePermissionStore.resetAddRouters()
- window.location.href = response?.data?.attachments?.loginUrl
- } else {
- userStore.reset()
- }
- }, 3000)
- return
- }
- if (response.data.success) {
- const url: any = response?.config?.url?.split('?')[0]
- // 统一处理需要展示提示信息的接口
- if (API_URL_MESSAGE_TIP.includes(url) || API_URL_MESSAGE_TIP_COMMON.includes(url)) {
- ElMessage.success(response?.data?.message)
- }
- }
- // console.log(response)
- if (response?.config?.responseType === 'blob') {
- // 如果是文件流,直接过
- // 当前业务处理形式 如果是文件流,直接过
- return {
- data: response.data,
- name: response?.headers?.['content-disposition'] || '导出文件.xlsx'
- }
- } else if (response.data.success || response.data.code === SUCCESS_CODE) {
- return response.data
- } else {
- ElMessage.error(response?.data?.message || 'Error Response')
- }
- }
- export { defaultResponseInterceptors, defaultRequestInterceptors }
|