feat: ✨ 实现记住我功能并重构认证逻辑为统一的Auth工具类
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type { NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from "vue-router";
|
||||
import NProgress from "@/utils/nprogress";
|
||||
import { Storage } from "@/utils/storage";
|
||||
import { ACCESS_TOKEN_KEY } from "@/constants/cache-keys";
|
||||
import { Auth } from "@/utils/auth";
|
||||
import router from "@/router";
|
||||
import { usePermissionStore, useUserStore } from "@/store";
|
||||
import { ROLE_ROOT } from "@/constants";
|
||||
@@ -12,15 +11,19 @@ export function setupPermission() {
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
NProgress.start();
|
||||
console.log("to.path", to.path);
|
||||
|
||||
const isLogin = !!Storage.get(ACCESS_TOKEN_KEY, ""); // 判断是否登录
|
||||
const isLogin = Auth.isLoggedIn();
|
||||
console.log("isLogin", isLogin);
|
||||
if (isLogin) {
|
||||
console.log("to.path", to.path);
|
||||
if (to.path === "/login") {
|
||||
// 已登录,跳转到首页
|
||||
// 如果已登录,跳转到首页
|
||||
next({ path: "/" });
|
||||
} else {
|
||||
// 未登录
|
||||
const permissionStore = usePermissionStore();
|
||||
console.log("permissionStore.routesLoaded", permissionStore.routesLoaded);
|
||||
// 判断路由是否加载完成
|
||||
if (permissionStore.routesLoaded) {
|
||||
if (to.matched.length === 0) {
|
||||
@@ -43,7 +46,7 @@ export function setupPermission() {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// 路由加载失败,重置 token 并重定向到登录页
|
||||
await useUserStore().clearSessionAndCache();
|
||||
await useUserStore().resetAllState();
|
||||
redirectToLogin(to, next);
|
||||
NProgress.done();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useDictSync } from "@/composables/useDictSync";
|
||||
import { Storage } from "@/utils/storage";
|
||||
import { ACCESS_TOKEN_KEY } from "@/constants/cache-keys";
|
||||
import { Auth } from "@/utils/auth";
|
||||
|
||||
// 用于防止重复初始化的状态标记
|
||||
let isInitialized = false;
|
||||
@@ -24,9 +23,8 @@ export function setupWebSocket() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查token是否存在
|
||||
const token = Storage.get(ACCESS_TOKEN_KEY, "");
|
||||
if (!token) {
|
||||
// 检查是否已登录
|
||||
if (!Auth.isLoggedIn()) {
|
||||
console.warn(
|
||||
"[WebSocketPlugin] 未找到访问令牌,WebSocket初始化已跳过。用户登录后将自动重新连接。"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user