代码结构优化
This commit is contained in:
@@ -21,8 +21,6 @@ instance.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const token = store.token
|
||||
|
||||
// if(config.method === "get") return config
|
||||
|
||||
// 如果有token就把赋值给请求头
|
||||
if (token) config.headers["Authorization"] = `Bearer ${token}`;
|
||||
|
||||
@@ -41,18 +39,6 @@ instance.interceptors.request.use(
|
||||
// 响应拦截
|
||||
instance.interceptors.response.use(
|
||||
(res: AxiosResponse) => {
|
||||
// 如果code为401就证明认证失败
|
||||
if (res.data.code === 401) {
|
||||
return Modal.error({
|
||||
title: '暂无权限',
|
||||
content: '🔒️ 登录已过期,是否重新登录?',
|
||||
okText: "去登录",
|
||||
onOk: () => {
|
||||
store.quitLogin()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 只要code不等于200, 就相当于响应失败
|
||||
if (res.data?.code !== 200) {
|
||||
notification.error({
|
||||
@@ -66,6 +52,20 @@ instance.interceptors.response.use(
|
||||
return res.data;
|
||||
},
|
||||
(err: AxiosError) => {
|
||||
// 如果code为401就证明认证失败
|
||||
if (err.response?.status === 401) {
|
||||
Modal.error({
|
||||
title: '暂无权限',
|
||||
content: '🔒️ 登录已过期,是否重新登录?',
|
||||
okText: "去登录",
|
||||
onOk: () => {
|
||||
store.quitLogin()
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.reject(err.response?.data);
|
||||
}
|
||||
|
||||
// 服务器异常:网络错误、请求超时、状态码不在200-299之间等等
|
||||
notification.error({
|
||||
message: '服务器异常',
|
||||
|
||||
Reference in New Issue
Block a user