From d0d9bee8b703c96b98231de7320a1e7e8da5ff76 Mon Sep 17 00:00:00 2001 From: valarchie <343928303@qq.com> Date: Fri, 7 Jul 2023 18:07:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Etoken=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=20=E8=B7=B3=E5=87=BA=E6=8F=90=E7=A4=BA=E6=A1=86=20?= =?UTF-8?q?=E5=B9=B6=E8=B7=B3=E8=BD=AC=E7=99=BB=E5=BD=95=E9=A1=B5=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/common.ts | 7 ++++++- src/router/index.ts | 3 ++- src/utils/auth.ts | 2 +- src/utils/http/index.ts | 40 +++++++++++++++++++++++++++++++++---- src/views/error/403.vue | 11 +++++++--- src/views/login/index.vue | 42 ++++++++++++++++++++++++--------------- 6 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/api/common.ts b/src/api/common.ts index b7a7172..e6c65bd 100644 --- a/src/api/common.ts +++ b/src/api/common.ts @@ -36,7 +36,7 @@ export type TokenDTO = { }; export type CurrentLoginUserDTO = { - user: any; + userInfo: any; roleKey: string; permissions: Set; }; @@ -61,3 +61,8 @@ export const getCaptchaCode = () => { export const loginByPassword = (data: LoginByPasswordDTO) => { return http.request>("post", "/login", { data }); }; + +/** 获取当前登录用户接口 */ +export const getLoginUserInfo = () => { + return http.request>("get", "/getLoginUserInfo"); +}; diff --git a/src/router/index.ts b/src/router/index.ts index 5c340cc..b23c59f 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -122,7 +122,8 @@ router.beforeEach((to: ToRouteType, _from, next) => { } /** 如果已经登录并存在登录信息后不能跳转到路由白名单,而是继续保持在当前页面 */ function toCorrectRoute() { - whiteList.includes(to.fullPath) ? next(_from.fullPath) : next(); + /** 新增判断是否存在登录信息 */ + whiteList.includes(to.fullPath) && userInfo ? next(_from.fullPath) : next(); } if (userInfo) { // 无权限跳转403页面 diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 67c3a3f..d499574 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -30,7 +30,7 @@ export function getToken(): TokenDTO { // 此处与`TokenKey`相同,此写法解决初始化时`Cookies`中不存在`TokenKey`报错 return Cookies.get(tokenKey) ? JSON.parse(Cookies.get(tokenKey)) - : storageSession().getItem(sessionKey).token; + : storageSession().getItem(sessionKey)?.token; } /** diff --git a/src/utils/http/index.ts b/src/utils/http/index.ts index 068d793..b62e4c6 100644 --- a/src/utils/http/index.ts +++ b/src/utils/http/index.ts @@ -13,6 +13,10 @@ import { stringify } from "qs"; import NProgress from "../progress"; import { getToken, formatToken } from "@/utils/auth"; import { message } from "../message"; +import { ElMessageBox } from "element-plus"; +import { router } from "@/router"; +import { removeToken } from "@/utils/auth"; +// console.log("Utils:" + router); const { VITE_APP_BASE_API } = import.meta.env; // 相关配置请参考:www.axios-js.com/zh-cn/docs/#axios-request-config-1 @@ -101,9 +105,32 @@ class PureHttp { const instance = PureHttp.axiosInstance; instance.interceptors.response.use( (response: PureHttpResponse) => { + // 请求返回失败时,有业务错误时,弹出错误提示 if (response.data.code !== 0) { - message(response.data.msg, { type: "error" }); + // token失效时弹出过期提示 + if (response.data.code === 20101) { + ElMessageBox.confirm( + "登录状态已过期,您可以继续留在该页面,或者重新登录", + "系统提示", + { + confirmButtonText: "重新登录", + cancelButtonText: "取消", + type: "warning" + } + ) + .then(() => { + removeToken(); + router.push("/login"); + }) + .catch(() => { + message("取消重新登录", { type: "info" }); + }); + } else { + // 其余情况弹出错误提示框 + message(response.data.msg, { type: "error" }); + } } + const $config = response.config; // 关闭进度条动画 NProgress.done(); @@ -151,15 +178,20 @@ class PureHttp { resolve(response); }) .catch(error => { - if (error.response.status >= 500) { + // 某些情况网络失效,此时直接进入error流程,所以在这边也进行拦截 + if (error.response && error.response.status >= 500) { message("网络异常", { type: "error" }); } - if (error.response.status >= 400 && error.response.status < 500) { + if ( + error.response && + error.response.status >= 400 && + error.response.status < 500 + ) { message("请求接口不存在", { type: "error" }); } - reject(error.response.statusText); + reject(error); }); }); } diff --git a/src/views/error/403.vue b/src/views/error/403.vue index 83b0838..0639298 100644 --- a/src/views/error/403.vue +++ b/src/views/error/403.vue @@ -1,12 +1,17 @@