优化相关设备的格式显示

This commit is contained in:
Coldin04 2025-08-13 22:48:17 +08:00
parent b18805aa26
commit 4a3eaf87e8
6 changed files with 67 additions and 142 deletions

View file

@ -2,15 +2,21 @@ from typing import Dict, Any, List
from datetime import datetime
from .base_handler import BaseHandler
from services.crypto_service import CryptoService
from config import Config # 导入配置
class CryptoHandler(BaseHandler):
"""加密货币价格处理器"""
def __init__(self, crypto_service: CryptoService):
def __init__(self, crypto_service: CryptoService, config=None):
self.crypto_service = crypto_service
self.config = config or Config # 使用传入的配置或默认配置
def handle(self, pair: str = "ETH-USDT", **kwargs) -> List[Dict[str, Any]]:
def handle(self, pair: str = None, **kwargs) -> List[Dict[str, Any]]:
"""处理加密货币价格请求"""
# 如果未指定交易对,则使用配置中的默认值
if pair is None:
pair = self.config.DEFAULT_CRYPTO_PAIR
data = self.crypto_service.get_cached_data(f"{pair.lower()}_price", pair=pair)
if not data:
@ -25,17 +31,13 @@ class CryptoHandler(BaseHandler):
# 格式化价格信息
price = data['price']
change_24h = data['change_24h']
change_symbol = "📈" if change_24h >= 0 else "📉"
change_symbol = "" if change_24h >= 0 else ""
title = f"{pair.replace('-', '/')} 当前价格: ${price:,.4f}"
title = f"${price:,.2f} {change_symbol} {abs(change_24h):.2f}%"
description = f"""
🔸 当前价格: ${price:,.4f}
🔸 24小时涨跌: {change_symbol} {change_24h:+.2f}%
🔸 24小时最高: ${data['high_24h']:,.4f}
🔸 24小时最低: ${data['low_24h']:,.4f}
🔸 24小时成交量: {data['volume_24h']:,.2f}
🔸 更新时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
24小时涨跌: {change_symbol} {change_24h:+.2f}%
更新时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
""".strip()
return [{