refactor(storage): ♻️ 简化缓存管理方式,统一使用Storage类直接操作token和缓存

This commit is contained in:
Ray.Hao
2025-05-19 14:09:46 +08:00
parent a6d76d17d8
commit 2a3d2543ee
10 changed files with 71 additions and 47 deletions

View File

@@ -1,6 +1,7 @@
import type { NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from "vue-router";
import NProgress from "@/utils/nprogress";
import { getAccessToken } from "@/utils/auth";
import { Storage } from "@/utils/storage";
import { ACCESS_TOKEN_KEY } from "@/constants/cache-keys";
import router from "@/router";
import { usePermissionStore, useUserStore } from "@/store";
@@ -11,7 +12,7 @@ export function setupPermission() {
router.beforeEach(async (to, from, next) => {
NProgress.start();
const isLogin = !!getAccessToken(); // 判断是否登录
const isLogin = !!Storage.get(ACCESS_TOKEN_KEY, ""); // 判断是否登录
if (isLogin) {
if (to.path === "/login") {
// 已登录,访问登录页,跳转到首页

View File

View File

@@ -1,5 +1,6 @@
import { useDictSync } from "@/hooks/websocket/services/useDictSync";
import { getAccessToken } from "@/utils/auth";
import { Storage } from "@/utils/storage";
import { ACCESS_TOKEN_KEY } from "@/constants/cache-keys";
// 用于防止重复初始化的状态标记
let isInitialized = false;
@@ -24,7 +25,7 @@ export function setupWebSocket() {
}
// 检查token是否存在
const token = getAccessToken();
const token = Storage.get(ACCESS_TOKEN_KEY, "");
if (!token) {
console.warn(
"[WebSocketPlugin] 未找到访问令牌WebSocket初始化已跳过。用户登录后将自动重新连接。"