Initial commit

This commit is contained in:
zyp
2026-05-22 16:13:20 +08:00
commit b3089a254e
1444 changed files with 372077 additions and 0 deletions

View File

@@ -0,0 +1,201 @@
/*
* @Date: 2024-12-19
* @Description: GK数据WebSocket配置文件
* @Author: Assistant
* @FilePath: \drone-v2-command-screen\src\api\websocket\gkDataConfig.ts
*/
import { ELocalStorageKey } from '/@/types/enums'
/**
* GK数据WebSocket配置接口
*/
export interface GKWebSocketConfig {
/** 主服务器地址 */
primary: string
/** 备用服务器地址 */
fallback: string
/** WebSocket连接路径 */
path: string
/** 自动重连配置 */
reconnect: {
maxReconnectionDelay: number
minReconnectionDelay: number
maxRetries: number
reconnectionDelayGrowFactor: number
}
}
/**
* 默认GK数据WebSocket配置
* 基于项目中的主服务器配置
*/
export const DEFAULT_GK_WEBSOCKET_CONFIG: GKWebSocketConfig = {
// 主服务器地址(当前使用)
primary: 'ws://27.11.11.30',
// 备用服务器地址
fallback: 'ws://221.226.33.58:18800',
// WebSocket连接路径与项目中的WebSocket路径保持一致
path: '/dji/api/v1/ws',
// 自动重连配置
reconnect: {
maxReconnectionDelay: 20000, // 最大重连延迟20秒
minReconnectionDelay: 5000, // 最小重连延迟5秒
maxRetries: 10, // 最大重试次数
reconnectionDelayGrowFactor: 1.3
}
}
/**
* 获取完整的GK数据WebSocket URL带token认证
* @param useFallback 是否使用备用地址
* @returns 完整的WebSocket URL
*/
export function getGKWebSocketUrl(useFallback: boolean = false): string {
// 获取token与项目现有方式保持一致
const token: string = localStorage.getItem(ELocalStorageKey.Token) || localStorage.getItem(ELocalStorageKey.WsToken) || ''
// 在开发环境下使用代理路径,生产环境使用直接连接
if (import.meta.env.DEV) {
// 开发环境使用vite代理
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const host = window.location.host
const baseUrl = `${protocol}//${host}/dji/api/v1/ws`
// 添加token认证参数
if (token) {
return `${baseUrl}?x-auth-token=${encodeURIComponent(token)}`
}
return baseUrl
} else {
// 生产环境:直接连接
const config = DEFAULT_GK_WEBSOCKET_CONFIG
const baseUrl = useFallback ? config.fallback : config.primary
const fullUrl = `${baseUrl}${config.path}`
// 添加token认证参数
if (token) {
return `${fullUrl}?x-auth-token=${encodeURIComponent(token)}`
}
return fullUrl
}
}
/**
* 获取所有可用的GK数据WebSocket URL
* @returns 主地址和备用地址数组
*/
export function getAllGKWebSocketUrls(): string[] {
return [
getGKWebSocketUrl(false), // 主地址
getGKWebSocketUrl(true) // 备用地址
]
}
/**
* 从环境变量或进程中获取基础URL
* 与vite.config.ts中的配置保持一致
*/
export function getBaseTargetUrl(): string {
// 尝试从环境变量中获取与vite.config.ts保持一致
if (typeof process !== 'undefined' && process.env?.BASE_TARGET_URL) {
return process.env.BASE_TARGET_URL.replace(/"/g, '') // 移除JSON.stringify添加的引号
}
// 默认返回主服务器地址
return '27.11.11.30/'
}
/**
* 动态构建GK数据WebSocket URL
* 基于项目运行时的配置
*/
export function buildGKWebSocketUrl(): string {
const baseUrl = getBaseTargetUrl()
// 确保URL格式正确
let wsUrl = baseUrl.startsWith('ws://') ? baseUrl : `ws://${baseUrl}`
// 移除末尾的斜杠(如果存在)
if (wsUrl.endsWith('/')) {
wsUrl = wsUrl.slice(0, -1)
}
return `${wsUrl}${DEFAULT_GK_WEBSOCKET_CONFIG.path}`
}
/**
* GK数据WebSocket配置管理器
*/
export class GKWebSocketConfigManager {
private currentUrl: string
private fallbackUrl: string
private retryCount: number = 0
constructor() {
this.currentUrl = getGKWebSocketUrl(false)
this.fallbackUrl = getGKWebSocketUrl(true)
}
/**
* 获取当前使用的WebSocket URL
*/
getCurrentUrl(): string {
return this.currentUrl
}
/**
* 切换到备用地址
*/
switchToFallback(): void {
console.log(`GK WebSocket 切换到备用地址: ${this.fallbackUrl}`)
this.currentUrl = this.fallbackUrl
this.retryCount++
}
/**
* 重置到主地址
*/
resetToPrimary(): void {
console.log(`GK WebSocket 重置到主地址: ${getGKWebSocketUrl(false)}`)
this.currentUrl = getGKWebSocketUrl(false)
this.retryCount = 0
}
/**
* 获取重试次数
*/
getRetryCount(): number {
return this.retryCount
}
/**
* 是否应该切换到备用地址
*/
shouldSwitchToFallback(): boolean {
return this.retryCount >= 3 && this.currentUrl !== this.fallbackUrl
}
/**
* 获取重连配置
*/
getReconnectConfig() {
return DEFAULT_GK_WEBSOCKET_CONFIG.reconnect
}
}
/**
* 全局GK数据WebSocket配置管理器实例
*/
export const gkWebSocketConfigManager = new GKWebSocketConfigManager()
export default {
DEFAULT_GK_WEBSOCKET_CONFIG,
getGKWebSocketUrl,
getAllGKWebSocketUrls,
buildGKWebSocketUrl,
GKWebSocketConfigManager,
gkWebSocketConfigManager
}