Bläddra i källkod

fix: 前端 VITE_API_BASE_URL 使用 ?? 替代 || 支持空字符串

nginx 反代模式下前端 API 调用走相对路径,VITE_API_BASE_URL
应为空字符串。用 || 会把空字符串当 falsy 导致 fallback 到
localhost:8010。改用 ?? 只在 undefined 时 fallback。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
mengboxin137-blip 15 timmar sedan
förälder
incheckning
93c92922b5

+ 1 - 1
frontend/App.tsx

@@ -18,7 +18,7 @@ import { Loader2 } from './icons/commonIcons';
 import { NotificationProvider } from './contexts/NotificationContext';
 import './styles/markdown.css';
 
-const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 // 品牌配置 Context
 export interface BrandingConfig { system_name: string; system_logo: string; icp_number: string }

+ 1 - 1
frontend/pages/Login.tsx

@@ -7,7 +7,7 @@ import { Mail, Lock, GraduationCap, AlertCircle, CheckCircle2, Phone } from 'luc
 import { User, Loader2 } from '../icons/commonIcons';
 import { BrandingContext } from '../App';
 
-const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 
 // ==================== 忘记密码弹窗 ====================

+ 1 - 1
frontend/pages/SSOCallback.tsx

@@ -4,7 +4,7 @@ import { useNavigate, useSearchParams } from 'react-router-dom';
 import { authService } from '../services/authService';
 import { Loader2, CheckCircle2, AlertCircle } from 'lucide-react';
 
-const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 /**
  * SSO 回调页面

+ 1 - 1
frontend/services/authService.ts

@@ -69,7 +69,7 @@ function convertUserToUserInfo(user: User): UserInfo {
 const TOKEN_KEY = 'aigc_space_token';
 const REFRESH_TOKEN_KEY = 'aigc_space_refresh_token';
 const USER_INFO_KEY = 'aigc_space_user_info';
-const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 // 认证状态变化事件名
 const AUTH_CHANGE_EVENT = 'auth_state_change';

+ 1 - 1
frontend/services/featureApi.ts

@@ -1,4 +1,4 @@
-const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8010';
+const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://127.0.0.1:8010';
 
 export interface FeatureFlags {
   enable_openclaw: boolean;

+ 2 - 7
frontend/services/modelApi.ts

@@ -1,13 +1,8 @@
 import { Model } from '../types/models';
 import { authService } from './authService';
 
-// API Base URL - 从环境变量获取,必须配置
-const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
-
-if (!API_BASE_URL) {
-  console.error('VITE_API_BASE_URL 环境变量未配置,请在 .env 文件中设置 VITE_API_BASE_URL');
-  throw new Error('VITE_API_BASE_URL 环境变量未配置');
-}
+// API Base URL - nginx 反代模式下为空字符串(走相对路径 /api/)
+const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? '';
 
 // API 响应类型定义
 export interface ApiResponse<T> {

+ 1 - 1
frontend/services/passwordApi.ts

@@ -1,7 +1,7 @@
 import { authService } from './authService';
 
 // API Base URL
-const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 // 密码强度等级
 export type PasswordStrengthLevel = 'weak' | 'medium' | 'strong';

+ 1 - 1
frontend/services/userApi.ts

@@ -2,7 +2,7 @@ import { authService } from './authService';
 
 // API Base URL - 从环境变量获取
 // 默认改为 8010,与当前后端一致。若有自定义域名/端口,请在 .env.local 配置 VITE_API_BASE_URL
-const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8010';
+const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8010';
 
 // 用户信息接口(与后端API对应)
 export interface User {