From 79797b6ffc73433141843782401c06dfeb0d9a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=93=E6=9C=88=E5=BD=92=E5=B0=98?= Date: Sun, 23 Feb 2025 03:55:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E4=BF=A1=E6=81=AF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 5 ++++- src/utils/auth.ts | 14 ++++++++++++++ src/views/login/index.vue | 6 ------ src/views/monitor/cache/index.vue | 10 ++++++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 43efc59..f85f3af 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -27,7 +27,8 @@ import { type UserInfo, userInfoKey, removeToken, - getTokenInfo + getTokenInfo, + getUserInfo } from "@/utils/auth"; /** 自动导入全部静态路由,无需再手动引入!匹配 src/router/modules 目录(任何嵌套级别)中具有 .ts 扩展名的所有文件,除了 remaining.ts 文件 @@ -129,6 +130,8 @@ router.beforeEach((to: ToRouteType, _from, next) => { whiteList.includes(to.fullPath) ? next(_from.fullPath) : next(); } const data = getTokenInfo(); + /**获取个人信息 */ + getUserInfo(); if (!data.isExpire) { // 无权限跳转403页面 if ( diff --git a/src/utils/auth.ts b/src/utils/auth.ts index e85abb8..5fad242 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,5 +1,6 @@ import { storageLocal } from "@pureadmin/utils"; import { useUserStoreHook } from "@/store/modules/user"; +import { getUserInfoAPI } from "@/api/login"; export interface UserInfo { /** 用户名 */ @@ -114,3 +115,16 @@ export function getTokenInfo(): { export const hasAuth = (auth: string) => { return useUserStoreHook().permissions.includes(auth); }; + +/**获取用户信息 */ +export const getUserInfo = async () => { + const res = await getUserInfoAPI(); + if (res.success) { + const user = storageLocal().getItem(userInfoKey); + storageLocal().setItem(userInfoKey, { + ...user, + ...res.data + }); + setUserInfo(res.data); + } +}; diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 5100bda..2d8b842 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -34,8 +34,6 @@ import Check from "@iconify-icons/ep/check"; import User from "@iconify-icons/ri/user-3-fill"; import Info from "@iconify-icons/ri/information-line"; import { GetCaptchaAPI } from "@/api/login"; -import { getUserInfoAPI } from "@/api/login"; -import { setUserInfo } from "@/utils/auth"; const Period = getConfig("Period"); defineOptions({ @@ -88,10 +86,6 @@ const onLogin = async (formEl: FormInstance | undefined) => { // 获取后端路由 return initRouter().then(async () => { disabled.value = true; - const res = await getUserInfoAPI(); - if (res.success) { - setUserInfo(res.data); - } router .push(getTopMenu(true).path) .then(() => { diff --git a/src/views/monitor/cache/index.vue b/src/views/monitor/cache/index.vue index 11d2be5..230d5ae 100644 --- a/src/views/monitor/cache/index.vue +++ b/src/views/monitor/cache/index.vue @@ -121,6 +121,7 @@ import * as echarts from "echarts"; import Monitor from "@iconify-icons/ep/monitor"; import PieChart from "@iconify-icons/ep/pie-chart"; import Odometer from "@iconify-icons/ep/odometer"; +import { onBeforeRouteLeave } from "vue-router"; const cache = reactive({ commandStats: [] as { name: string; value: number }[], @@ -240,13 +241,18 @@ const initUsedMemoryChart = () => { chart.setOption(option); }; - +/**定时ID */ +const timer = ref(); onMounted(async () => { await getCacheInfo(); - setInterval(() => { + timer.value = setInterval(() => { getCacheInfo(); }, 60000); }); +/**页面离开时清除定时器 */ +onBeforeRouteLeave(() => { + clearInterval(timer.value); +});