feat: 更新 axios 到 1.4.0,在响应拦截中处理 401 状态(跳转到登录)
This commit is contained in:
parent
dfe97bf5d5
commit
c8e0b6c474
@ -21,7 +21,7 @@
|
|||||||
"@types/keymaster": "^1.6.30",
|
"@types/keymaster": "^1.6.30",
|
||||||
"@types/lodash": "^4.14.184",
|
"@types/lodash": "^4.14.184",
|
||||||
"animate.css": "^4.1.1",
|
"animate.css": "^4.1.1",
|
||||||
"axios": "^0.27.2",
|
"axios": "^1.4.0",
|
||||||
"color": "^4.2.3",
|
"color": "^4.2.3",
|
||||||
"crypto-js": "^4.1.1",
|
"crypto-js": "^4.1.1",
|
||||||
"dayjs": "^1.11.7",
|
"dayjs": "^1.11.7",
|
||||||
|
1338
pnpm-lock.yaml
generated
1338
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
import axios, { AxiosResponse, AxiosRequestConfig, Axios } from 'axios'
|
import axios, { AxiosResponse, AxiosRequestConfig, Axios, AxiosError, InternalAxiosRequestConfig } from 'axios'
|
||||||
import { ResultEnum, ModuleTypeEnum } from "@/enums/httpEnum"
|
import { RequestHttpHeaderEnum, ResultEnum, ModuleTypeEnum } from '@/enums/httpEnum'
|
||||||
import { PageEnum, ErrorPageNameMap } from "@/enums/pageEnum"
|
import { PageEnum, ErrorPageNameMap } from '@/enums/pageEnum'
|
||||||
import { StorageEnum } from '@/enums/storageEnum'
|
import { StorageEnum } from '@/enums/storageEnum'
|
||||||
import { axiosPre } from '@/settings/httpSetting'
|
import { axiosPre } from '@/settings/httpSetting'
|
||||||
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
|
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
|
||||||
@ -20,11 +20,11 @@ export interface MyRequestInstance extends Axios {
|
|||||||
|
|
||||||
const axiosInstance = axios.create({
|
const axiosInstance = axios.create({
|
||||||
baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`,
|
baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`,
|
||||||
timeout: ResultEnum.TIMEOUT,
|
timeout: ResultEnum.TIMEOUT
|
||||||
}) as unknown as MyRequestInstance
|
}) as unknown as MyRequestInstance
|
||||||
|
|
||||||
axiosInstance.interceptors.request.use(
|
axiosInstance.interceptors.request.use(
|
||||||
(config: AxiosRequestConfig) => {
|
(config: InternalAxiosRequestConfig) => {
|
||||||
// 白名单校验
|
// 白名单校验
|
||||||
if (includes(fetchAllowList, config.url)) return config
|
if (includes(fetchAllowList, config.url)) return config
|
||||||
// 获取 token
|
// 获取 token
|
||||||
@ -41,7 +41,7 @@ axiosInstance.interceptors.request.use(
|
|||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
(err: AxiosRequestConfig) => {
|
(err: AxiosError) => {
|
||||||
Promise.reject(err)
|
Promise.reject(err)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -74,13 +74,23 @@ axiosInstance.interceptors.response.use(
|
|||||||
redirectErrorPage(code)
|
redirectErrorPage(code)
|
||||||
return Promise.resolve(res.data)
|
return Promise.resolve(res.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提示错误
|
// 提示错误
|
||||||
window['$message'].error(window['$t']((res.data as any).msg))
|
window['$message'].error(window['$t']((res.data as any).msg))
|
||||||
return Promise.resolve(res.data)
|
return Promise.resolve(res.data)
|
||||||
},
|
},
|
||||||
(err: AxiosResponse) => {
|
(err: AxiosError) => {
|
||||||
Promise.reject(err)
|
const { status } = err
|
||||||
|
switch (status) {
|
||||||
|
case 401:
|
||||||
|
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
|
||||||
|
Promise.reject(err)
|
||||||
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
|
Promise.reject(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user