From e1c6cc26813d3cea9b8647050e36bab44c74e23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Fri, 23 Jan 2026 20:24:06 +0800 Subject: [PATCH] a --- backend/api/auth_deps.py | 3 +++ backend/api/routes/account.py | 9 +++++++-- backend/database/models.py | 8 ++++++-- frontend/src/services/api.js | 2 ++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/backend/api/auth_deps.py b/backend/api/auth_deps.py index a67a606..04fb1a6 100644 --- a/backend/api/auth_deps.py +++ b/backend/api/auth_deps.py @@ -88,7 +88,10 @@ def get_account_id( x_account_id: Optional[int] = Header(None, alias="X-Account-Id"), user: Dict[str, Any] = Depends(get_current_user), ) -> int: + import logging + logger = logging.getLogger(__name__) aid = int(x_account_id or 1) + logger.info(f"get_account_id: X-Account-Id header={x_account_id}, parsed account_id={aid}, user_id={user.get('id')}") return require_account_access(aid, user) diff --git a/backend/api/routes/account.py b/backend/api/routes/account.py index 7687f5b..ce76e25 100644 --- a/backend/api/routes/account.py +++ b/backend/api/routes/account.py @@ -351,7 +351,9 @@ async def get_realtime_account_data(account_id: int = 1): try: # 从 accounts 表读取账号私有API密钥 logger.info(f"步骤1: 从accounts读取API配置... (account_id={account_id})") + logger.info(f" - 请求的 account_id: {account_id}") api_key, api_secret, use_testnet, status = Account.get_credentials(account_id) + logger.info(f" - 获取到的 account_id 状态: {status}") logger.info(f" - API密钥存在: {bool(api_key)}") if api_key: @@ -401,8 +403,9 @@ async def get_realtime_account_data(account_id: int = 1): api_secret=api_secret, testnet=use_testnet ) - logger.info(api_key) - logger.info(api_secret) + logger.info(f" - 使用的 account_id: {account_id}") + logger.info(f" - API Key 前4位: {api_key[:4] if api_key and len(api_key) >= 4 else 'N/A'}...") + logger.info(f" - API Secret 前4位: {api_secret[:4] if api_secret and len(api_secret) >= 4 else 'N/A'}...") logger.info(f" ✓ 客户端创建成功 (testnet={use_testnet})") # 连接币安API @@ -567,7 +570,9 @@ async def get_realtime_positions(account_id: int = Depends(get_account_id)): """获取实时持仓数据""" client = None try: + logger.info(f"get_realtime_positions: 请求的 account_id={account_id}") api_key, api_secret, use_testnet, status = Account.get_credentials(account_id) + logger.info(f"get_realtime_positions: 获取到的 account_id={account_id}, status={status}, api_key exists={bool(api_key)}") logger.info(f"尝试获取实时持仓数据 (testnet={use_testnet}, account_id={account_id})") diff --git a/backend/database/models.py b/backend/database/models.py index 3ceced3..aafd305 100644 --- a/backend/database/models.py +++ b/backend/database/models.py @@ -93,12 +93,16 @@ class Account: @staticmethod def get_credentials(account_id: int): """ - 返回 (api_key, api_secret, use_testnet);密文字段会自动解密。 + 返回 (api_key, api_secret, use_testnet, status);密文字段会自动解密。 若未配置 master key 且库里是明文,仍可工作(但不安全)。 """ + import logging + logger = logging.getLogger(__name__) + logger.info(f"Account.get_credentials called with account_id={account_id}") row = Account.get(account_id) if not row: - return "", "", False + logger.warning(f"Account.get_credentials: account_id={account_id} not found in database") + return "", "", False, "disabled" try: from security.crypto import decrypt_str status = row.get("status") or "active" diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index cc49cb8..9d1383e 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -88,6 +88,8 @@ const withAccountHeaders = (headers = {}, accountIdOverride = null) => { currentAccountIdFromStore = aid; } } + // 调试日志:记录使用的 accountId + console.log(`[API] withAccountHeaders: accountIdOverride=${accountIdOverride}, currentAccountIdFromStore=${currentAccountIdFromStore}, final accountId=${aid}`); return withAuthHeaders({ ...headers, 'X-Account-Id': String(aid) }); };