fix: 假单打乱时间比较改用时间戳,避免 naive/aware 相减报错

This commit is contained in:
XingQue
2026-06-28 18:41:42 +08:00
parent b152f95ef8
commit 3d2fb61df5

View File

@@ -1,6 +1,6 @@
"""抢单大厅假单:无资格/封禁打手仅展示假数据。"""
import random
from datetime import timedelta
from datetime import datetime, timedelta
from decimal import Decimal
from django.core.exceptions import ObjectDoesNotExist
@@ -55,6 +55,26 @@ def _ensure_aware(dt):
return dt
def _to_unix_ts(dt):
"""统一转为 Unix 时间戳,避免 naive/aware 直接相减报错。"""
if dt is None:
return None
if timezone.is_aware(dt):
return dt.timestamp()
try:
return timezone.make_aware(dt, timezone.get_current_timezone()).timestamp()
except Exception:
return dt.timestamp()
def _elapsed_seconds(since_dt, until_dt):
since_ts = _to_unix_ts(since_dt)
until_ts = _to_unix_ts(until_dt)
if since_ts is None or until_ts is None:
return None
return until_ts - since_ts
def _parse_shuffle_time(value):
if not value:
return None
@@ -68,8 +88,14 @@ def _display_time_str_for_order(order_id, anchor_dt):
"""同一假单在打乱窗口内展示时间固定。"""
seed = abs(hash(str(order_id))) % (2 ** 32)
seconds_ago = 120 + (seed % 181)
base = _ensure_aware(anchor_dt) or timezone.now()
return fmt_datetime(base - timedelta(seconds=seconds_ago))
base_ts = _to_unix_ts(_ensure_aware(anchor_dt) or timezone.now())
if base_ts is None:
base_ts = _to_unix_ts(timezone.now()) or 0
display_dt = datetime.fromtimestamp(
base_ts - seconds_ago,
tz=timezone.get_current_timezone(),
)
return fmt_datetime(display_dt)
def _load_fake_rows_in_saved_order(club_id, leixing_id):
@@ -107,10 +133,12 @@ def _load_fake_rows_in_saved_order(club_id, leixing_id):
if not last_dt and not created:
last_dt = _ensure_aware(state.LastShuffleTime)
elapsed = _elapsed_seconds(last_dt, now)
need_reshuffle = (
not stored_ids
or last_dt is None
or (now - last_dt).total_seconds() >= interval
or elapsed is None
or elapsed >= interval
)
if need_reshuffle: