From c3f3a5e291719f81e4b09ad067435eb45efbc80d Mon Sep 17 00:00:00 2001 From: duxingchen Date: Wed, 12 Aug 2026 13:28:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=9C=8B=E6=9D=BF=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=94=B9=E4=B8=BA=E5=8C=97=E4=BA=AC=E6=97=B6?= =?UTF-8?q?=E9=97=B4=20+=20=E6=93=8D=E4=BD=9C=E4=BA=BA=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=A7=93=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 时区修复: - database.py: PG连接池会话级设置 TimeZone=Asia/Shanghai - dashboard_service.py: 格式化时间显式 astimezone(BEIJING_TZ) - 兜底: tzinfo为None时默认当作北京时间处理 2. 操作人中文姓名: - 收集所有operator_id → 调用mom_cache批量翻译 - 优先中文姓名 → 英文用户名兜底 - operator_id为None时不再显示"系统",留空 --- backend/app/core/database.py | 3 +++ backend/app/services/dashboard_service.py | 24 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/app/core/database.py b/backend/app/core/database.py index 130594d..a8192b9 100644 --- a/backend/app/core/database.py +++ b/backend/app/core/database.py @@ -9,6 +9,9 @@ engine = create_async_engine( max_overflow=10, # 超出 pool_size 时最多再创建的连接数 pool_recycle=3600, # 连接回收时间(秒),防止 MySQL 8 小时断连 pool_pre_ping=True, # 每次取出连接前先 ping 检测可用性 + connect_args={ + "server_settings": {"TimeZone": "Asia/Shanghai"}, # PG 会话级北京时间 + }, ) AsyncSessionLocal = async_sessionmaker( diff --git a/backend/app/services/dashboard_service.py b/backend/app/services/dashboard_service.py index 8786908..f2fe875 100644 --- a/backend/app/services/dashboard_service.py +++ b/backend/app/services/dashboard_service.py @@ -73,6 +73,7 @@ async def get_recent_activity(db: AsyncSession, limit: int = 10) -> list[RecentA from app.models.task_log import TaskLog from app.models.task import Task from app.models.product import Product + from app.core.time_utils import BEIJING_TZ stmt = ( select(TaskLog, Task.task_name, Product.serial_number) @@ -84,14 +85,33 @@ async def get_recent_activity(db: AsyncSession, limit: int = 10) -> list[RecentA result = await db.execute(stmt) rows = result.all() + # 收集 operator_id → 批量翻译中文姓名 + operator_ids = list({row[0].operator_id for row in rows if row[0].operator_id}) + name_map: dict[str, str] = {} + if operator_ids: + from app.services.mom_cache import get_display_names + name_map = get_display_names(operator_ids) + activities: list[RecentActivity] = [] for log, task_name, product_sn in rows: action_label = _action_label(log.action_type) - time_str = log.created_at.strftime("%m-%d %H:%M") if log.created_at else "" + # 强制转北京时间显示 + t = log.created_at + if t: + if t.tzinfo is None: + t = t.replace(tzinfo=BEIJING_TZ) + else: + t = t.astimezone(BEIJING_TZ) + time_str = t.strftime("%m-%d %H:%M") + else: + time_str = "" + # operator_id: 优先显示中文姓名 → 英文用户名兜底 → 无记录时为空 + op = log.operator_id + op_display = name_map.get(op, op or "") activities.append(RecentActivity( action=action_label, task_name=task_name or "", - operator=log.operator_id or "系统", + operator=op_display, product_sn=product_sn or "", time=time_str, remark=log.remark,