2021-12-14 15:53:30 +08:00
|
|
|
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
2022-05-21 17:31:01 +08:00
|
|
|
import { ResultEnum, RequestHttpHeaderEnum } from "@/enums/httpEnum"
|
|
|
|
import { PageEnum, ErrorPageNameMap } from "@/enums/pageEnum"
|
|
|
|
import { StorageEnum } from '@/enums/storageEnum'
|
2022-05-20 16:12:27 +08:00
|
|
|
import { axiosPre } from '@/settings/httpSetting'
|
2022-05-21 17:31:01 +08:00
|
|
|
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
|
2022-05-22 15:25:07 +08:00
|
|
|
import { redirectErrorPage, getLocalStorage, routerTurnByName, httpErrorHandle } from '@/utils'
|
2022-05-21 17:31:01 +08:00
|
|
|
import { fetchAllowList } from './axios.config'
|
|
|
|
import includes from 'lodash/includes'
|
2022-05-20 16:12:27 +08:00
|
|
|
|
|
|
|
interface MyResponseType {
|
|
|
|
code: number;
|
|
|
|
msg: string;
|
|
|
|
data: any;
|
|
|
|
}
|
2021-12-14 15:53:30 +08:00
|
|
|
|
|
|
|
const axiosInstance = axios.create({
|
2022-05-20 16:12:27 +08:00
|
|
|
baseURL: axiosPre,
|
2021-12-14 15:53:30 +08:00
|
|
|
timeout: ResultEnum.TIMEOUT,
|
|
|
|
})
|
|
|
|
|
|
|
|
axiosInstance.interceptors.request.use(
|
|
|
|
(config: AxiosRequestConfig) => {
|
2022-05-21 17:31:01 +08:00
|
|
|
// 白名单校验
|
|
|
|
if (includes(fetchAllowList, config.url)) return config
|
|
|
|
// 获取 token
|
|
|
|
const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
|
|
|
|
// 重新登录
|
|
|
|
if (!info) return routerTurnByName(PageEnum.BASE_LOGIN_NAME)
|
|
|
|
config.headers = {
|
|
|
|
[RequestHttpHeaderEnum.TOKEN]: info[SystemStoreEnum.USER_INFO][SystemStoreUserInfoEnum.USER_TOKEN] || ''
|
|
|
|
}
|
2021-12-14 15:53:30 +08:00
|
|
|
return config
|
|
|
|
},
|
|
|
|
(error: AxiosRequestConfig) => {
|
|
|
|
Promise.reject(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
axiosInstance.interceptors.response.use(
|
|
|
|
(res: AxiosResponse) => {
|
|
|
|
const { code } = res.data as { code: number }
|
2022-05-21 17:31:01 +08:00
|
|
|
|
|
|
|
// 成功
|
2022-05-21 18:03:15 +08:00
|
|
|
if (code === ResultEnum.SUCCESS) {
|
2022-05-21 17:31:01 +08:00
|
|
|
return Promise.resolve(res.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录过期
|
2022-05-21 18:03:15 +08:00
|
|
|
if (code === ResultEnum.TOKEN_OVERDUE) {
|
|
|
|
window['$message'].error(window['$t']('http.token_overdue_message'))
|
2022-05-21 17:31:01 +08:00
|
|
|
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
|
2022-05-21 18:03:15 +08:00
|
|
|
return
|
2022-05-21 17:31:01 +08:00
|
|
|
}
|
|
|
|
|
2022-05-21 18:03:15 +08:00
|
|
|
// 固定错误码重定向
|
2022-05-21 17:31:01 +08:00
|
|
|
if (ErrorPageNameMap.get(code)) {
|
|
|
|
redirectErrorPage(code)
|
2022-05-21 18:03:15 +08:00
|
|
|
return
|
2022-05-21 17:31:01 +08:00
|
|
|
}
|
2022-05-21 18:03:15 +08:00
|
|
|
|
|
|
|
// 提示错误
|
|
|
|
window['$message'].error(window['$t']((res.data as any).msg))
|
2022-03-21 23:03:10 +08:00
|
|
|
return Promise.resolve(res.data)
|
2021-12-14 15:53:30 +08:00
|
|
|
},
|
|
|
|
(err: AxiosResponse) => {
|
|
|
|
const { code } = err.data as { code: number }
|
|
|
|
if (ErrorPageNameMap.get(code)) redirectErrorPage(code)
|
2022-05-22 15:25:07 +08:00
|
|
|
httpErrorHandle()
|
2021-12-14 15:53:30 +08:00
|
|
|
Promise.reject(err)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
export default axiosInstance
|