refactor: ♻️ eslint 代码规范调整

This commit is contained in:
ray
2024-10-13 10:42:48 +08:00
parent 72eb87d005
commit a4ef6eb696
66 changed files with 376 additions and 376 deletions

View File

@@ -1,4 +1,4 @@
const TOKEN_KEY = "v3-admin-token";
const TOKEN_KEY = "admin-token";
function getToken(): string {
return localStorage.getItem(TOKEN_KEY) || "";

View File

@@ -1,4 +1,7 @@
import axios, { InternalAxiosRequestConfig, AxiosResponse } from "axios";
import axios, {
type InternalAxiosRequestConfig,
type AxiosResponse,
} from "axios";
import { useUserStoreHook } from "@/store/modules/user";
import { ResultEnum } from "@/enums/ResultEnum";
import { getToken } from "@/utils/auth";

View File

@@ -5,13 +5,22 @@ class WebSocketManager {
private client: Client | null = null;
private messageHandlers: Map<string, ((message: string) => void)[]> =
new Map();
constructor() {}
private reconnectAttempts = 0;
private maxReconnectAttempts = 3; // 自定义最大重试次数
private reconnectDelay = 5000; // 重试延迟(单位:毫秒)
// 初始化 WebSocket 客户端
setupWebSocket() {
const endpoint = import.meta.env.VITE_APP_WS_ENDPOINT;
// 如果没有配置 WebSocket 端点或显式关闭,直接返回
if (!endpoint) {
console.log(
"WebSocket 已被禁用,如需打开请在配置文件中配置 VITE_APP_WS_ENDPOINT"
);
return;
}
if (this.client && this.client.connected) {
console.log("客户端已存在并且连接正常");
return this.client;
@@ -24,8 +33,10 @@ class WebSocketManager {
},
heartbeatIncoming: 30000,
heartbeatOutgoing: 30000,
reconnectDelay: 0, // 设置为 0 禁用重连
onConnect: () => {
console.log(`连接到 WebSocket 服务器: ${endpoint}`);
this.reconnectAttempts = 0; // 重置重连计数
this.messageHandlers.forEach((handlers, topic) => {
handlers.forEach((handler) => {
this.subscribeToTopic(topic, handler);
@@ -38,6 +49,13 @@ class WebSocketManager {
},
onDisconnect: () => {
console.log(`WebSocket 连接已断开: ${endpoint}`);
this.reconnectAttempts++;
if (this.reconnectAttempts < this.maxReconnectAttempts) {
console.log(`正在尝试重连... 尝试次数: ${this.reconnectAttempts}`);
} else {
console.log("重连次数已达上限,停止重连");
this.client?.deactivate();
}
},
});