feat: 动态获取 WebSocket URL 和当前域名,优化配置管理

This commit is contained in:
MatrixSeven
2025-08-02 15:20:32 +08:00
parent 0ff3e67694
commit 0942d11019
2 changed files with 33 additions and 10 deletions

View File

@@ -3,7 +3,7 @@
* 统一处理开发模式和静态模式下的API调用 * 统一处理开发模式和静态模式下的API调用
*/ */
import { config, getApiUrl, getDirectBackendUrl } from './config'; import { config, getApiUrl, getDirectBackendUrl, getWsUrl } from './config';
/** /**
* 统一的 fetch 函数自动处理不同环境下的API调用 * 统一的 fetch 函数自动处理不同环境下的API调用
@@ -120,7 +120,7 @@ export async function apiUpload(
* 获取 WebSocket URL * 获取 WebSocket URL
*/ */
export function getWebSocketUrl(): string { export function getWebSocketUrl(): string {
return config.api.wsUrl; return getWsUrl(); // 使用实时获取的 WebSocket URL
} }
/** /**

View File

@@ -14,6 +14,26 @@ const getEnv = (key: string, defaultValue: string = '') => {
return defaultValue; return defaultValue;
}; };
// 动态获取当前域名和协议
const getCurrentBaseUrl = () => {
if (typeof window !== 'undefined') {
// 客户端运行时,使用当前页面的 origin
return window.location.origin;
}
// 服务器端默认值
return 'http://localhost:8080';
};
// 动态获取 WebSocket URL
const getCurrentWsUrl = () => {
if (typeof window !== 'undefined') {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${window.location.host}/ws`;
}
// 服务器端默认值
return 'ws://localhost:8080/ws';
};
export const config = { export const config = {
// 环境判断 // 环境判断
isDev: getEnv('NODE_ENV') === 'development', isDev: getEnv('NODE_ENV') === 'development',
@@ -28,11 +48,11 @@ export const config = {
// 前端API基础URL (客户端使用) - 开发模式下调用 Next.js API 路由 // 前端API基础URL (客户端使用) - 开发模式下调用 Next.js API 路由
baseUrl: getEnv('NEXT_PUBLIC_API_BASE_URL', 'http://localhost:3000'), baseUrl: getEnv('NEXT_PUBLIC_API_BASE_URL', 'http://localhost:3000'),
// 直接后端URL (客户端在静态模式下使用) // 直接后端URL (客户端在静态模式下使用) - 如果环境变量为空,则使用当前域名
directBackendUrl: getEnv('NEXT_PUBLIC_BACKEND_URL', 'http://localhost:8080'), directBackendUrl: getEnv('NEXT_PUBLIC_BACKEND_URL') || getCurrentBaseUrl(),
// WebSocket地址 // WebSocket地址 - 如果环境变量为空,则使用当前域名构建
wsUrl: getEnv('NEXT_PUBLIC_WS_URL', 'ws://localhost:8080/ws'), wsUrl: getEnv('NEXT_PUBLIC_WS_URL') || getCurrentWsUrl(),
}, },
// 超时配置 // 超时配置
@@ -76,7 +96,8 @@ export function getApiUrl(path: string): string {
* @returns 完整的API URL * @returns 完整的API URL
*/ */
export function getDirectBackendUrl(path: string): string { export function getDirectBackendUrl(path: string): string {
const baseUrl = config.api.directBackendUrl.replace(/\/$/, '') // 实时获取当前域名(支持动态域名)
const baseUrl = (getEnv('NEXT_PUBLIC_BACKEND_URL') || getCurrentBaseUrl()).replace(/\/$/, '')
const apiPath = path.startsWith('/') ? path : `/${path}` const apiPath = path.startsWith('/') ? path : `/${path}`
return `${baseUrl}${apiPath}` return `${baseUrl}${apiPath}`
} }
@@ -86,7 +107,8 @@ export function getDirectBackendUrl(path: string): string {
* @returns WebSocket连接地址 * @returns WebSocket连接地址
*/ */
export function getWsUrl(): string { export function getWsUrl(): string {
return config.api.wsUrl // 实时获取当前域名构建的 WebSocket URL
return getEnv('NEXT_PUBLIC_WS_URL') || getCurrentWsUrl()
} }
/** /**
@@ -97,8 +119,9 @@ export function getEnvInfo() {
environment: getEnv('NODE_ENV'), environment: getEnv('NODE_ENV'),
backendUrl: config.api.backendUrl, backendUrl: config.api.backendUrl,
baseUrl: config.api.baseUrl, baseUrl: config.api.baseUrl,
directBackendUrl: config.api.directBackendUrl, directBackendUrl: getDirectBackendUrl(''), // 实时获取
wsUrl: config.api.wsUrl, wsUrl: getWsUrl(), // 实时获取
currentOrigin: typeof window !== 'undefined' ? window.location.origin : 'server-side',
isDev: config.isDev, isDev: config.isDev,
isProd: config.isProd, isProd: config.isProd,
isStatic: config.isStatic, isStatic: config.isStatic,