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(