fix(douyu): 按到期时间修正精英宝典状态
This commit is contained in:
@@ -169,7 +169,8 @@ class Account(Base):
|
||||
role_change_available_at: Mapped[datetime | None] = mapped_column(
|
||||
UTCDateTime, nullable=True
|
||||
)
|
||||
# 精英宝典开通状态(getUserInfo 的 manualType / expireTime)。
|
||||
# 精英宝典当前有效开通状态(getUserInfo 的 manualType + expireTime)。
|
||||
# manualType 只表示开过;写入时还要按 expireTime 判断是否已过期。
|
||||
# 光看积分区分不了「开通后积分花完」和「从未开通」,必须单独落库。
|
||||
elite_opened: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
||||
elite_expire_time: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
|
||||
@@ -31,6 +31,10 @@ from .douyu_activity_query import (
|
||||
query_bind_info,
|
||||
query_exchange_eligibility,
|
||||
)
|
||||
from .douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
normalize_elite_expire_time,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -182,10 +186,16 @@ def refresh_douyu_account_state(
|
||||
except Exception as exc: # noqa: BLE001 绑定查询失败不应丢弃资产查询结果
|
||||
errors.append(f"绑定状态查询失败: {type(exc).__name__}: {exc}"[:240])
|
||||
|
||||
elite_opened = bool(eligibility.elite_user_info.get("manual_type"))
|
||||
elite_expire_time = _to_int(eligibility.elite_user_info.get("expire_time"))
|
||||
elite_expire_time = normalize_elite_expire_time(
|
||||
eligibility.elite_user_info.get("expire_time")
|
||||
)
|
||||
elite_opened = elite_is_currently_open(
|
||||
eligibility.elite_user_info.get("manual_type"),
|
||||
elite_expire_time,
|
||||
now=now,
|
||||
)
|
||||
account.elite_opened = elite_opened
|
||||
account.elite_expire_time = elite_expire_time or None
|
||||
account.elite_expire_time = elite_expire_time
|
||||
account.points = eligibility.points
|
||||
account.points_expire_at = _points_expire_at(
|
||||
eligibility.points_response,
|
||||
|
||||
@@ -57,6 +57,10 @@ from .douyu_activity_query import (
|
||||
query_exchange_records,
|
||||
query_gold_account,
|
||||
)
|
||||
from .douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
normalize_elite_expire_time,
|
||||
)
|
||||
from .douyu_goods_service import (
|
||||
serialize_live_goods as _serialize_live_goods,
|
||||
)
|
||||
@@ -346,7 +350,7 @@ def _probe_with_client(
|
||||
|
||||
manual_type = _to_int(elite.get("manual_type"))
|
||||
manual_score = _to_int(elite.get("manual_score"))
|
||||
expire_time = _to_int(elite.get("expire_time"))
|
||||
expire_time = normalize_elite_expire_time(elite.get("expire_time"))
|
||||
user_info = elite.get("user_info") or {}
|
||||
profile = DouyuActivityClient.profile_from_cookie(str(account.cookie or ""))
|
||||
bind_role = _text(bind_snapshot["role_name"], max_length=128)
|
||||
@@ -373,7 +377,7 @@ def _probe_with_client(
|
||||
"manual_type": manual_type,
|
||||
"manual_score": manual_score,
|
||||
"expire_time": expire_time,
|
||||
"manual_opened": bool(manual_type is not None and manual_type >= 1),
|
||||
"manual_opened": elite_is_currently_open(manual_type, expire_time),
|
||||
"bind_status": bind_snapshot["bind_status"],
|
||||
"bind_phase": bind_snapshot["bind_phase"],
|
||||
"bind_game_account": bind_account,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""斗鱼精英宝典当前有效状态。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _to_int(value: Any) -> int | None:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
try:
|
||||
return int(float(value))
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def normalize_elite_expire_time(value: Any) -> int | None:
|
||||
expire_time = _to_int(value)
|
||||
return expire_time if expire_time and expire_time > 0 else None
|
||||
|
||||
|
||||
def elite_is_expired(
|
||||
expire_time: Any,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> bool:
|
||||
normalized = normalize_elite_expire_time(expire_time)
|
||||
if normalized is None:
|
||||
return False
|
||||
moment = now or datetime.now(UTC)
|
||||
if moment.tzinfo is None:
|
||||
moment = moment.replace(tzinfo=UTC)
|
||||
return normalized <= int(moment.timestamp())
|
||||
|
||||
|
||||
def elite_is_currently_open(
|
||||
manual_type: Any,
|
||||
expire_time: Any,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> bool:
|
||||
opened = (_to_int(manual_type) or 0) >= 1
|
||||
return opened and not elite_is_expired(expire_time, now=now)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"elite_is_currently_open",
|
||||
"elite_is_expired",
|
||||
"normalize_elite_expire_time",
|
||||
]
|
||||
@@ -13,6 +13,10 @@ from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_events_service import EVENT_ELITE_OPENED, EVENT_ESPORTS_OPENED, emit
|
||||
from .douyu_activity_query import query_csrf, query_elite_user_info, query_points
|
||||
from .douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
normalize_elite_expire_time,
|
||||
)
|
||||
from .douyu_runner_core import DOUYU_PAYMENT_POLL_INTERVAL, DOUYU_PAYMENT_POLL_SECONDS
|
||||
from .douyu_service import account_uid, update_account_profile_from_cookie
|
||||
|
||||
@@ -77,14 +81,15 @@ class ManualMixin:
|
||||
) -> dict:
|
||||
"""查询精英宝典开通状态(manualType/expireTime)并写回账号。
|
||||
|
||||
积分区分不了「开通后积分花完」和「从未开通」,manualType 才是权威口径:
|
||||
0=未开通,>=1=已开通。
|
||||
积分区分不了「开通后积分花完」和「从未开通」。manualType >= 1
|
||||
表示开通过,expireTime 决定当前是否仍有效;已到期必须按未开通处理。
|
||||
"""
|
||||
result = query_elite_user_info(client, manual_id=manual_id, rid=rid)
|
||||
manual_type = self._to_int(result.get("manual_type")) or 0
|
||||
manual_score = self._to_int(result.get("manual_score"))
|
||||
expire_time = self._to_int(result.get("expire_time")) or 0
|
||||
opened = manual_type >= 1
|
||||
expire_time = normalize_elite_expire_time(result.get("expire_time")) or 0
|
||||
opened = elite_is_currently_open(manual_type, expire_time)
|
||||
expired = manual_type >= 1 and not opened
|
||||
account.elite_opened = opened
|
||||
account.elite_expire_time = expire_time or None
|
||||
if manual_score is not None:
|
||||
@@ -95,6 +100,7 @@ class ManualMixin:
|
||||
"elite_manual_score": manual_score,
|
||||
"elite_expire_time": expire_time,
|
||||
"elite_opened": opened,
|
||||
"elite_expired": expired,
|
||||
"elite_user_info": result,
|
||||
"points": manual_score,
|
||||
}
|
||||
@@ -115,7 +121,11 @@ class ManualMixin:
|
||||
rid=str(config.get("rid") or ""),
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
state = "已开通" if result["elite_opened"] else "未开通"
|
||||
state = (
|
||||
"已过期"
|
||||
if result["elite_expired"]
|
||||
else ("已开通" if result["elite_opened"] else "未开通")
|
||||
)
|
||||
expire_text = self._format_expire_text(result["elite_expire_time"])
|
||||
points = result["elite_manual_score"]
|
||||
self._mark_task(
|
||||
|
||||
@@ -40,6 +40,10 @@ from ..business_events import record_account_query, record_douyu_task_started
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_activity_query import activity_client, query_exchange_eligibility
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
from ..douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
normalize_elite_expire_time,
|
||||
)
|
||||
from ..douyu_service import (
|
||||
SUPPORTED_DOUYU_TASK_TYPES,
|
||||
douyu_config_value,
|
||||
@@ -2194,7 +2198,14 @@ def refresh_exchange_eligibility(
|
||||
rid=str(douyu_config_value("rid", config.rid)),
|
||||
uid=str(account.uid or ""),
|
||||
)
|
||||
account.elite_opened = bool(eligibility.elite_user_info.get("manual_type"))
|
||||
elite_expire_time = normalize_elite_expire_time(
|
||||
eligibility.elite_user_info.get("expire_time")
|
||||
)
|
||||
account.elite_opened = elite_is_currently_open(
|
||||
eligibility.elite_user_info.get("manual_type"),
|
||||
elite_expire_time,
|
||||
)
|
||||
account.elite_expire_time = elite_expire_time
|
||||
account.points = eligibility.points
|
||||
except Exception as exc: # noqa: BLE001 外部接口错误要落成可重试的检查失败
|
||||
record_account_query(
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from '@ant-design/icons';
|
||||
import { accountApi, douyuApi, userApi, type AccountCommodityState, type AccountEventItem, type AccountOverview, type HighValueAssetState, type HighValueStatus, type UserInfo } from '../api/modules';
|
||||
import AccountOverviewPanel from './douyu-tasks/AccountOverviewPanel';
|
||||
import { eliteIsExpired } from '../utils/douyuElite';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import { formatTime, formatUnixSeconds } from '../utils/time';
|
||||
@@ -734,18 +735,32 @@ export default function AccountDetailPage() {
|
||||
<Col span={4}>
|
||||
<Statistic
|
||||
title="精英宝典"
|
||||
value={overview?.elite_opened ? '已开通' : '未开通'}
|
||||
value={
|
||||
eliteIsExpired(overview ?? {})
|
||||
? '已过期'
|
||||
: overview?.elite_opened
|
||||
? '已开通'
|
||||
: '未开通'
|
||||
}
|
||||
valueStyle={{
|
||||
fontSize: 18,
|
||||
color: overview?.elite_opened ? '#3f8600' : undefined,
|
||||
color: eliteIsExpired(overview ?? {})
|
||||
? '#cf1322'
|
||||
: overview?.elite_opened
|
||||
? '#3f8600'
|
||||
: undefined,
|
||||
}}
|
||||
/>
|
||||
{/* 已开通才显示时间。开通时间来自事件(**上线后才有**);
|
||||
更早的历史查不到 —— 官方兑换记录里只有商品兑换,没有开通记录。 */}
|
||||
{overview?.elite_opened && (
|
||||
{(overview?.elite_opened || eliteIsExpired(overview ?? {})) && (
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>
|
||||
开通 {formatTime(overview?.opened_at?.elite)}
|
||||
<br />
|
||||
{overview?.elite_opened && (
|
||||
<>
|
||||
开通 {formatTime(overview?.opened_at?.elite)}
|
||||
<br />
|
||||
</>
|
||||
)}
|
||||
到期 {formatUnixSeconds(overview?.elite_expire_time)}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
@@ -46,6 +46,7 @@ import {
|
||||
resultFlag,
|
||||
taskQrUrl,
|
||||
eliteStatusText,
|
||||
eliteIsExpired,
|
||||
bindReadyForConfirm,
|
||||
hasBindQrcode,
|
||||
hasPaymentQrcode,
|
||||
@@ -1361,6 +1362,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
render: (_: unknown, record: DouyuTaskAccountItem) => {
|
||||
const text = eliteStatusText(record);
|
||||
if (!text) return <Text type="secondary">未查</Text>;
|
||||
if (eliteIsExpired(record)) return <Tag color="error">{text}</Tag>;
|
||||
if (record.elite_opened) return <Tag color="green">{text}</Tag>;
|
||||
return <Tag color="default">未开通</Tag>;
|
||||
},
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
QrcodeOutlined, ReloadOutlined, SearchOutlined, ShoppingOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import type { DouyuGoodsItem, DouyuTaskItem } from '../../api/modules';
|
||||
export { eliteIsExpired, eliteStatusText } from '../../utils/douyuElite';
|
||||
|
||||
export type HandbookKind = 'elite' | 'esports' | 'peace';
|
||||
|
||||
@@ -159,21 +160,6 @@ export function taskQrUrl(task: DouyuTaskItem | null | undefined): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 精英宝典开通状态展示:积分区分不了"开通后积分花完"和"从未开通",
|
||||
// 以后端落库的 manualType(elite_opened)为准。
|
||||
export function eliteStatusText(
|
||||
account: { elite_opened?: boolean | null; elite_expire_time?: number | null },
|
||||
): string {
|
||||
if (account.elite_opened == null) return '';
|
||||
if (!account.elite_opened) return '未开通';
|
||||
const expire = account.elite_expire_time;
|
||||
if (!expire || expire <= 0) return '已开通';
|
||||
const at = new Date(expire * 1000);
|
||||
if (Number.isNaN(at.getTime())) return '已开通';
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
return `已开通 ${at.getFullYear()}-${pad(at.getMonth() + 1)}-${pad(at.getDate())} 到期`;
|
||||
}
|
||||
|
||||
export function bindReadyForConfirm(task: DouyuTaskItem | null | undefined): boolean {
|
||||
if (!task) return false;
|
||||
if (task.task_type === 'get_xpd_bind_qr') {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
export interface DouyuEliteState {
|
||||
elite_opened?: boolean | null;
|
||||
elite_expire_time?: number | null;
|
||||
}
|
||||
|
||||
export function eliteExpireDate(
|
||||
account: DouyuEliteState,
|
||||
): Date | null {
|
||||
const expire = Number(account.elite_expire_time || 0);
|
||||
if (!Number.isFinite(expire) || expire <= 0) return null;
|
||||
const at = new Date(expire * 1000);
|
||||
return Number.isNaN(at.getTime()) ? null : at;
|
||||
}
|
||||
|
||||
export function eliteIsExpired(
|
||||
account: DouyuEliteState,
|
||||
now = Date.now(),
|
||||
): boolean {
|
||||
const at = eliteExpireDate(account);
|
||||
return at !== null && at.getTime() <= now;
|
||||
}
|
||||
|
||||
export function eliteStatusText(account: DouyuEliteState): string {
|
||||
if (account.elite_opened == null) return '';
|
||||
const expireAt = eliteExpireDate(account);
|
||||
const dateText = expireAt
|
||||
? `${expireAt.getFullYear()}-${String(expireAt.getMonth() + 1).padStart(2, '0')}-${String(expireAt.getDate()).padStart(2, '0')}`
|
||||
: '';
|
||||
if (eliteIsExpired(account)) {
|
||||
return dateText ? `已于 ${dateText} 到期` : '已过期';
|
||||
}
|
||||
if (!account.elite_opened) return '未开通';
|
||||
return dateText ? `已开通 ${dateText} 到期` : '已开通';
|
||||
}
|
||||
Reference in New Issue
Block a user