fix: 处理登录过期的请求响应
This commit is contained in:
+22
-2
@@ -1,5 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import { ElMessage } from "element-plus";
|
||||
import { ElMessage, ElMessageBox } from "element-plus";
|
||||
import router from "@/router";
|
||||
|
||||
const basic_url = import.meta.env.VITE_BASE_URL;
|
||||
|
||||
@@ -12,8 +13,27 @@ const axiosInstance = axios.create({
|
||||
const handleError = (error: any) => {
|
||||
console.log(error);
|
||||
|
||||
// 401 未授权:token 过期或未登录(优先检查,避免被后续逻辑拦截)
|
||||
if (error?.response?.status === 401) {
|
||||
ElMessageBox.confirm(
|
||||
'登录已过期,请重新登录',
|
||||
'提示',
|
||||
{
|
||||
confirmButtonText: '重新登录',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning',
|
||||
}
|
||||
).then(() => {
|
||||
localStorage.removeItem("isLoggedIn");
|
||||
router.push("/login");
|
||||
}).catch(() => {
|
||||
// 用户点击取消,不做处理
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
// 业务异常由 validateResponse 负责提示,这里透传,避免被网络错误文案覆盖。
|
||||
if (error && typeof error === "object" && "code" in error) {
|
||||
if (error && typeof error === "object" && "code" in error && !error.response) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user