!165 更新 axios 到 1.4.0,并在axios响应拦截中处理 401 状态(跳转到登录)

Merge pull request !165 from dodu/fetch-401
This commit is contained in:
奔跑的面条 2023-05-28 09:09:46 +00:00 committed by Gitee
commit 8f996173b3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 660 additions and 697 deletions

View File

@ -21,7 +21,7 @@
"@types/keymaster": "^1.6.30",
"@types/lodash": "^4.14.184",
"animate.css": "^4.1.1",
"axios": "^0.27.2",
"axios": "^1.4.0",
"color": "^4.2.3",
"crypto-js": "^4.1.1",
"dayjs": "^1.11.7",

1327
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
import axios, { AxiosResponse, AxiosRequestConfig, Axios } from 'axios'
import { ResultEnum, ModuleTypeEnum } from "@/enums/httpEnum"
import { PageEnum, ErrorPageNameMap } from "@/enums/pageEnum"
import axios, { AxiosResponse, AxiosRequestConfig, Axios, AxiosError, InternalAxiosRequestConfig } from 'axios'
import { RequestHttpHeaderEnum, ResultEnum, ModuleTypeEnum } from '@/enums/httpEnum'
import { PageEnum, ErrorPageNameMap } from '@/enums/pageEnum'
import { StorageEnum } from '@/enums/storageEnum'
import { axiosPre } from '@/settings/httpSetting'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
@ -20,11 +20,11 @@ export interface MyRequestInstance extends Axios {
const axiosInstance = axios.create({
baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`,
timeout: ResultEnum.TIMEOUT,
timeout: ResultEnum.TIMEOUT
}) as unknown as MyRequestInstance
axiosInstance.interceptors.request.use(
(config: AxiosRequestConfig) => {
(config: InternalAxiosRequestConfig) => {
// 白名单校验
if (includes(fetchAllowList, config.url)) return config
// 获取 token
@ -41,7 +41,7 @@ axiosInstance.interceptors.request.use(
}
return config
},
(err: AxiosRequestConfig) => {
(err: AxiosError) => {
Promise.reject(err)
}
)
@ -74,13 +74,23 @@ axiosInstance.interceptors.response.use(
redirectErrorPage(code)
return Promise.resolve(res.data)
}
// 提示错误
window['$message'].error(window['$t']((res.data as any).msg))
return Promise.resolve(res.data)
},
(err: AxiosResponse) => {
Promise.reject(err)
(err: AxiosError) => {
const status = err.response?.status
switch (status) {
case 401:
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
Promise.reject(err)
break
default:
Promise.reject(err)
break
}
}
)