From ca195192b660cc829b192c7b3c445a5b0aa46d7a Mon Sep 17 00:00:00 2001 From: duxingchen Date: Wed, 12 Aug 2026 13:55:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=8F=E5=90=8C=E7=95=99=E8=A8=80?= =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: ProductMessage.created_at 使用 datetime.utcnow (UTC), 与其他所有模型使用的 get_beijing_time 不一致。 看板留言抽屉显示的时间比实际晚8小时。 修复: 1. message.py: DateTime → DateTime(timezone=True) default 从 datetime.utcnow → get_beijing_time 2. dashboard_service.py: 兜底转换已有数据 naive UTC → replace(tzinfo=UTC) → astimezone(Beijing) --- backend/app/models/message.py | 3 ++- backend/app/services/dashboard_service.py | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/app/models/message.py b/backend/app/models/message.py index 71102da..e0d1604 100644 --- a/backend/app/models/message.py +++ b/backend/app/models/message.py @@ -6,6 +6,7 @@ from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.models.base import Base +from app.core.time_utils import get_beijing_time class ProductMessage(Base): @@ -28,5 +29,5 @@ class ProductMessage(Base): Text, nullable=False, comment="留言内容", ) created_at: Mapped[datetime] = mapped_column( - DateTime, default=datetime.utcnow, comment="留言时间", + DateTime(timezone=True), default=get_beijing_time, comment="留言时间(北京时间)", ) diff --git a/backend/app/services/dashboard_service.py b/backend/app/services/dashboard_service.py index 127fdab..0581912 100644 --- a/backend/app/services/dashboard_service.py +++ b/backend/app/services/dashboard_service.py @@ -226,8 +226,13 @@ async def search_product_messages( items: list[ProductMessageItem] = [] for msg, sn, mat_name in rows: t = msg.created_at - if t and t.tzinfo is None: - t = t.replace(tzinfo=BEIJING_TZ) + if t: + if t.tzinfo is None: + # 旧数据(datetime.utcnow):naive UTC → 转北京时间 + from datetime import timezone as dt_timezone + t = t.replace(tzinfo=dt_timezone.utc).astimezone(BEIJING_TZ) + else: + t = t.astimezone(BEIJING_TZ) time_str = t.isoformat() if t else "" items.append(ProductMessageItem(