This commit is contained in:
薇薇安 2026-01-23 20:24:06 +08:00
parent 0c98bfe236
commit e1c6cc2681
4 changed files with 18 additions and 4 deletions

View File

@ -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)

View File

@ -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})")

View File

@ -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"

View File

@ -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) });
};