fix: 协同留言时间改为北京时间
根因: 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)
This commit is contained in:
@ -6,6 +6,7 @@ from sqlalchemy.dialects.postgresql import UUID
|
|||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
|
from app.core.time_utils import get_beijing_time
|
||||||
|
|
||||||
|
|
||||||
class ProductMessage(Base):
|
class ProductMessage(Base):
|
||||||
@ -28,5 +29,5 @@ class ProductMessage(Base):
|
|||||||
Text, nullable=False, comment="留言内容",
|
Text, nullable=False, comment="留言内容",
|
||||||
)
|
)
|
||||||
created_at: Mapped[datetime] = mapped_column(
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
DateTime, default=datetime.utcnow, comment="留言时间",
|
DateTime(timezone=True), default=get_beijing_time, comment="留言时间(北京时间)",
|
||||||
)
|
)
|
||||||
|
|||||||
@ -226,8 +226,13 @@ async def search_product_messages(
|
|||||||
items: list[ProductMessageItem] = []
|
items: list[ProductMessageItem] = []
|
||||||
for msg, sn, mat_name in rows:
|
for msg, sn, mat_name in rows:
|
||||||
t = msg.created_at
|
t = msg.created_at
|
||||||
if t and t.tzinfo is None:
|
if t:
|
||||||
t = t.replace(tzinfo=BEIJING_TZ)
|
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 ""
|
time_str = t.isoformat() if t else ""
|
||||||
|
|
||||||
items.append(ProductMessageItem(
|
items.append(ProductMessageItem(
|
||||||
|
|||||||
Reference in New Issue
Block a user