This commit is contained in:
薇薇安 2026-01-23 21:29:31 +08:00
parent 6341bacc20
commit 14b5acae09
2 changed files with 33 additions and 17 deletions

View File

@ -588,14 +588,24 @@ class ConfigManager:
# 账号私有API Key/Secret/Testnet 从 accounts 表读取(不走 trading_config # 账号私有API Key/Secret/Testnet 从 accounts 表读取(不走 trading_config
if key in ("BINANCE_API_KEY", "BINANCE_API_SECRET", "USE_TESTNET") and Account is not None: if key in ("BINANCE_API_KEY", "BINANCE_API_SECRET", "USE_TESTNET") and Account is not None:
try: 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": 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": 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) 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 pass
# 1. 优先从Redis缓存读取最新 # 1. 优先从Redis缓存读取最新

View File

@ -174,23 +174,29 @@ async def main():
logger.warning(f"重新加载配置失败: {e}", exc_info=True) logger.warning(f"重新加载配置失败: {e}", exc_info=True)
# 检查API密钥重新获取确保是最新值 # 检查API密钥重新获取确保是最新值
api_key = config._get_config_value('BINANCE_API_KEY', '') # 优先直接从 config_manager 获取(使用正确的 account_id
api_secret = config._get_config_value('BINANCE_API_SECRET', '') api_key = None
api_secret = None
# 如果从配置管理器获取失败尝试直接从config_manager获取 if config._config_manager:
if (not api_key or api_key == 'your_api_key_here') and config._config_manager:
try: try:
api_key = config._config_manager.get('BINANCE_API_KEY', '') # 直接从 config_manager 获取(会调用 Account.get_credentials使用正确的 account_id
logger.info(f"直接从config_manager获取API_KEY: 存在={bool(api_key)}") 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: 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: # 如果从 config_manager 获取失败,尝试从 _get_config_value 获取(兜底)
try: if not api_key or api_key == 'your_api_key_here':
api_secret = config._config_manager.get('BINANCE_API_SECRET', '') api_key = config._get_config_value('BINANCE_API_KEY', '')
logger.info(f"直接从config_manager获取API_SECRET: 存在={bool(api_secret)}") logger.info(f"从_get_config_value获取API_KEY: 存在={bool(api_key)}")
except Exception as e:
logger.warning(f"从config_manager获取API_SECRET失败: {e}") 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)}") 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: if api_key and api_key != 'your_api_key_here' and len(api_key) > 4: