feat: 实现记住我功能并重构认证逻辑为统一的Auth工具类

This commit is contained in:
Ray.Hao
2025-05-21 17:40:14 +08:00
parent aee9443fe0
commit 521ba98d6a
11 changed files with 194 additions and 73 deletions

View File

@@ -1,7 +1,5 @@
import { Client, IMessage, StompSubscription } from "@stomp/stompjs";
import { Storage } from "@/utils/storage";
import { ACCESS_TOKEN_KEY } from "@/constants/cache-keys";
import { ref, watch } from "vue";
import { Auth } from "@/utils/auth";
export interface UseStompOptions {
/** WebSocket 地址,不传时使用 VITE_APP_WS_ENDPOINT 环境变量 */
@@ -67,7 +65,7 @@ export function useStomp(options: UseStompOptions = {}) {
}
// 每次连接前重新获取最新令牌不依赖之前的token值
const currentToken = Storage.get(ACCESS_TOKEN_KEY, "");
const currentToken = Auth.getAccessToken();
// 检查令牌是否为空,如果为空则不进行连接
if (!currentToken) {
@@ -121,7 +119,7 @@ export function useStomp(options: UseStompOptions = {}) {
client.value = null;
// 检查当前是否有有效令牌
const freshToken = Storage.get(ACCESS_TOKEN_KEY, "");
const freshToken = Auth.getAccessToken();
if (freshToken) {
initializeClient();
connect();