From 1bfa7c18ef66c6981a7e53a2e28d33218bd5ef9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 14 Jan 2026 11:40:50 +0800 Subject: [PATCH] a --- backend/api/main.py | 3 ++- backend/api/routes/config.py | 1 + backend/api/routes/dashboard.py | 3 ++- backend/api/routes/trades.py | 1 + frontend/src/services/api.js | 20 ++++++++++++++++++-- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/api/main.py b/backend/api/main.py index fcbcfc6..df7f72f 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -93,7 +93,8 @@ logger.info(f"日志级别: {os.getenv('LOG_LEVEL', 'INFO')}") app = FastAPI( title="Auto Trade System API", version="1.0.0", - description="币安自动交易系统API" + description="币安自动交易系统API", + redirect_slashes=False # 禁用自动重定向,避免307重定向问题 ) # CORS配置(允许React前端访问) diff --git a/backend/api/routes/config.py b/backend/api/routes/config.py index 10c495e..3fed400 100644 --- a/backend/api/routes/config.py +++ b/backend/api/routes/config.py @@ -16,6 +16,7 @@ from database.models import TradingConfig router = APIRouter() +@router.get("") @router.get("/") async def get_all_configs(): """获取所有配置""" diff --git a/backend/api/routes/dashboard.py b/backend/api/routes/dashboard.py index 48a64be..e6746c0 100644 --- a/backend/api/routes/dashboard.py +++ b/backend/api/routes/dashboard.py @@ -6,5 +6,6 @@ from api.routes.stats import get_dashboard_data router = APIRouter() -# 使用stats模块的dashboard函数 +# 使用stats模块的dashboard函数(同时支持有斜杠和无斜杠) +router.add_api_route("", get_dashboard_data, methods=["GET"]) router.add_api_route("/", get_dashboard_data, methods=["GET"]) diff --git a/backend/api/routes/trades.py b/backend/api/routes/trades.py index 8ce8156..1ebadc3 100644 --- a/backend/api/routes/trades.py +++ b/backend/api/routes/trades.py @@ -43,6 +43,7 @@ def get_date_range(period: Optional[str] = None): return start_date.strftime('%Y-%m-%d 00:00:00'), end_date.strftime('%Y-%m-%d %H:%M:%S') +@router.get("") @router.get("/") async def get_trades( start_date: Optional[str] = Query(None, description="开始日期 (YYYY-MM-DD 或 YYYY-MM-DD HH:MM:SS)"), diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 08ff8f5..2c9a8d7 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -11,12 +11,20 @@ const buildUrl = (path) => { export const api = { // 配置管理 getConfigs: async () => { - const response = await fetch(buildUrl('/api/config/')); + const response = await fetch(buildUrl('/api/config')); + if (!response.ok) { + const error = await response.json().catch(() => ({ detail: '获取配置失败' })); + throw new Error(error.detail || '获取配置失败'); + } return response.json(); }, getConfig: async (key) => { const response = await fetch(buildUrl(`/api/config/${key}`)); + if (!response.ok) { + const error = await response.json().catch(() => ({ detail: '获取配置失败' })); + throw new Error(error.detail || '获取配置失败'); + } return response.json(); }, @@ -82,11 +90,19 @@ export const api = { // 统计 getPerformance: async (days = 7) => { const response = await fetch(buildUrl(`/api/stats/performance?days=${days}`)); + if (!response.ok) { + const error = await response.json().catch(() => ({ detail: '获取性能统计失败' })); + throw new Error(error.detail || '获取性能统计失败'); + } return response.json(); }, getDashboard: async () => { - const response = await fetch(buildUrl('/api/dashboard/')); + const response = await fetch(buildUrl('/api/dashboard')); + if (!response.ok) { + const error = await response.json().catch(() => ({ detail: '获取仪表板数据失败' })); + throw new Error(error.detail || '获取仪表板数据失败'); + } return response.json(); }, };