From 14b5acae09fe51729b67f8be3031a97d4646a550 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 21:29:31 +0800 Subject: [PATCH] a --- backend/config_manager.py | 18 ++++++++++++++---- trading_system/main.py | 32 +++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/backend/config_manager.py b/backend/config_manager.py index a35054a..e8e92bd 100644 --- a/backend/config_manager.py +++ b/backend/config_manager.py @@ -588,14 +588,24 @@ class ConfigManager: # 账号私有:API Key/Secret/Testnet 从 accounts 表读取(不走 trading_config) if key in ("BINANCE_API_KEY", "BINANCE_API_SECRET", "USE_TESTNET") and Account is not None: try: - api_key, api_secret, use_testnet = Account.get_credentials(self.account_id) + api_key, api_secret, use_testnet, status = Account.get_credentials(self.account_id) + logger.debug(f"ConfigManager.get({key}, account_id={self.account_id}): api_key存在={bool(api_key)}, api_secret存在={bool(api_secret)}, status={status}") if key == "BINANCE_API_KEY": - return api_key if api_key else default + # 如果 api_key 为空字符串,返回 None 而不是 default(避免返回 'your_api_key_here') + if not api_key or api_key.strip() == "": + logger.warning(f"ConfigManager.get(BINANCE_API_KEY, account_id={self.account_id}): API密钥为空字符串") + return None # 返回 None,让调用方知道密钥未配置 + return api_key if key == "BINANCE_API_SECRET": - return api_secret if api_secret else default + # 如果 api_secret 为空字符串,返回 None 而不是 default(避免返回 'your_api_secret_here') + if not api_secret or api_secret.strip() == "": + logger.warning(f"ConfigManager.get(BINANCE_API_SECRET, account_id={self.account_id}): API密钥Secret为空字符串") + return None # 返回 None,让调用方知道密钥未配置 + return api_secret return bool(use_testnet) - except Exception: + except Exception as e: # 回退到后续逻辑(旧数据/无表) + logger.warning(f"ConfigManager.get({key}, account_id={self.account_id}): Account.get_credentials 失败: {e}") pass # 1. 优先从Redis缓存读取(最新) diff --git a/trading_system/main.py b/trading_system/main.py index 760756a..5f3b0e3 100644 --- a/trading_system/main.py +++ b/trading_system/main.py @@ -174,23 +174,29 @@ async def main(): logger.warning(f"重新加载配置失败: {e}", exc_info=True) # 检查API密钥(重新获取,确保是最新值) - api_key = config._get_config_value('BINANCE_API_KEY', '') - api_secret = config._get_config_value('BINANCE_API_SECRET', '') + # 优先直接从 config_manager 获取(使用正确的 account_id) + api_key = None + api_secret = None - # 如果从配置管理器获取失败,尝试直接从config_manager获取 - if (not api_key or api_key == 'your_api_key_here') and config._config_manager: + if config._config_manager: try: - api_key = config._config_manager.get('BINANCE_API_KEY', '') - logger.info(f"直接从config_manager获取API_KEY: 存在={bool(api_key)}") + # 直接从 config_manager 获取(会调用 Account.get_credentials,使用正确的 account_id) + api_key = config._config_manager.get('BINANCE_API_KEY') + api_secret = config._config_manager.get('BINANCE_API_SECRET') + logger.info(f"从config_manager获取API密钥: KEY存在={bool(api_key)}, SECRET存在={bool(api_secret)}") + if api_key: + logger.info(f"API_KEY前4位: {api_key[:4]}...") except Exception as e: - logger.warning(f"从config_manager获取API_KEY失败: {e}") + logger.warning(f"从config_manager获取API密钥失败: {e}") - if (not api_secret or api_secret == 'your_api_secret_here') and config._config_manager: - try: - api_secret = config._config_manager.get('BINANCE_API_SECRET', '') - logger.info(f"直接从config_manager获取API_SECRET: 存在={bool(api_secret)}") - except Exception as e: - logger.warning(f"从config_manager获取API_SECRET失败: {e}") + # 如果从 config_manager 获取失败,尝试从 _get_config_value 获取(兜底) + if not api_key or api_key == 'your_api_key_here': + api_key = config._get_config_value('BINANCE_API_KEY', '') + logger.info(f"从_get_config_value获取API_KEY: 存在={bool(api_key)}") + + if not api_secret or api_secret == 'your_api_secret_here': + api_secret = config._get_config_value('BINANCE_API_SECRET', '') + logger.info(f"从_get_config_value获取API_SECRET: 存在={bool(api_secret)}") logger.info(f"API密钥检查: KEY存在={bool(api_key)}, SECRET存在={bool(api_secret)}") if api_key and api_key != 'your_api_key_here' and len(api_key) > 4: