feat(douyu): 完成账号平台阶段一到四并接入最新协议
This commit is contained in:
@@ -19,7 +19,11 @@ from core.douyu import (
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_sync_service import DOMAIN_GOLD, record_sync_success
|
||||
from .business_events import record_recharge
|
||||
from .douyu_activity_query import query_exchange_balance, query_gold_account
|
||||
from .douyu_activity_query import (
|
||||
query_exchange_balance,
|
||||
query_gold_account,
|
||||
query_gold_qr_state,
|
||||
)
|
||||
from .douyu_service import update_account_profile_from_cookie
|
||||
|
||||
|
||||
@@ -45,16 +49,70 @@ class GoldMixin:
|
||||
) -> dict:
|
||||
"""刷新鱼翅和钱包兑换余额并写回账号表。"""
|
||||
gold = query_gold_account(client)
|
||||
exchange = query_exchange_balance(client)
|
||||
account.gold_balance = self._to_int(gold.get("gold"))
|
||||
account.exchange_balance = self._to_int(exchange.get("count"))
|
||||
# 2026-11-21:鱼翅余额是充值确认的关键结果。钱包兑换余额属于旁路资产,
|
||||
# 单项失败不能反过来把已经成功拉到的鱼翅余额判为刷新失败。
|
||||
try:
|
||||
exchange = query_exchange_balance(client)
|
||||
account.exchange_balance = self._to_int(exchange.get("count"))
|
||||
exchange_error = ""
|
||||
except Exception as exc: # noqa: BLE001 兑换余额不能阻断鱼翅确认
|
||||
exchange = {}
|
||||
exchange_error = str(exc)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
return {
|
||||
result = {
|
||||
"gold_balance": account.gold_balance,
|
||||
"exchange_balance": account.exchange_balance,
|
||||
"gold": gold,
|
||||
"exchange_balance_query": exchange,
|
||||
}
|
||||
if exchange_error:
|
||||
result["exchange_balance_error"] = exchange_error
|
||||
return result
|
||||
|
||||
def _finish_wallet_gold_recharge(
|
||||
self,
|
||||
db: Session,
|
||||
task: DouyuTask,
|
||||
account: Account,
|
||||
result: dict,
|
||||
*,
|
||||
amount: int,
|
||||
channel: str,
|
||||
order_id: str = "",
|
||||
balance_refreshed: bool,
|
||||
confirm_source: str,
|
||||
) -> None:
|
||||
"""2026-11-21:新版支付状态确认后统一刷新余额并写充值事实。"""
|
||||
result.pop("payment_poll_error", None)
|
||||
result["gold_recharged"] = True
|
||||
result["payment_polling"] = False
|
||||
result["gold_refreshed_after_success"] = balance_refreshed
|
||||
result["gold_confirm_source"] = confirm_source
|
||||
if order_id:
|
||||
result["order_id"] = order_id
|
||||
account.updated_at = datetime.now(UTC)
|
||||
record_recharge(
|
||||
db,
|
||||
account,
|
||||
task_id=task.id,
|
||||
amount=max(1, int(amount)),
|
||||
order_id=order_id,
|
||||
channel=channel,
|
||||
balance_after=account.gold_balance,
|
||||
confirmed=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
order_text = f",订单 {order_id}" if order_id else ""
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
"鱼翅已到账"
|
||||
f"{order_text},当前余额: "
|
||||
f"{account.gold_balance if account.gold_balance is not None else '-'}",
|
||||
result,
|
||||
)
|
||||
|
||||
def _poll_create_gold_qr_once(
|
||||
self,
|
||||
@@ -68,12 +126,85 @@ class GoldMixin:
|
||||
result = dict(task.result or {})
|
||||
result["payment_polling"] = True
|
||||
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
|
||||
amount = int(
|
||||
result.get("payment_amount")
|
||||
or self._task_payload(task).get("amount")
|
||||
or self._task_payload(task).get("gold_amount")
|
||||
or 1
|
||||
)
|
||||
channel = str(
|
||||
result.get("payment_channel")
|
||||
or config.get("gold_recharge_channel")
|
||||
or "wechat_qr"
|
||||
)
|
||||
try:
|
||||
client = self._client(cookie, direct=True)
|
||||
balance_result = self._refresh_account_gold_balance(client, account)
|
||||
db.commit()
|
||||
result.update(balance_result)
|
||||
last_gold = balance_result["gold_balance"]
|
||||
token = str(result.get("payment_token") or result.get("token") or "")
|
||||
qr_state: int | None = None
|
||||
qr_state_error = ""
|
||||
if token:
|
||||
try:
|
||||
state_result = query_gold_qr_state(client, token=token)
|
||||
if not isinstance(state_result, dict):
|
||||
raise TypeError("鱼翅支付状态响应格式异常")
|
||||
qr_state = self._to_int(state_result.get("state"))
|
||||
result["gold_qr_state"] = qr_state
|
||||
if state_result.get("quantity") is not None:
|
||||
result["gold_qr_quantity"] = state_result.get("quantity")
|
||||
quantity = self._to_int(state_result.get("quantity"))
|
||||
if quantity is not None and quantity > 0:
|
||||
amount = quantity
|
||||
if state_result.get("order_id"):
|
||||
result["order_id"] = str(state_result["order_id"])
|
||||
except Exception as exc: # noqa: BLE001 新接口失败时兼容余额轮询
|
||||
qr_state_error = str(exc)
|
||||
result["gold_qr_state_error"] = qr_state_error
|
||||
|
||||
balance_result: dict[str, Any] = {}
|
||||
balance_error = ""
|
||||
try:
|
||||
balance_result = self._refresh_account_gold_balance(client, account)
|
||||
db.commit()
|
||||
result.update(balance_result)
|
||||
except Exception as exc: # noqa: BLE001 官方状态成功后仍应完成充值
|
||||
balance_error = str(exc)
|
||||
result["payment_poll_error"] = balance_error
|
||||
last_gold = balance_result.get("gold_balance")
|
||||
|
||||
if qr_state == 2:
|
||||
self._finish_wallet_gold_recharge(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
result,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
order_id=str(result.get("order_id") or ""),
|
||||
balance_refreshed=not balance_error,
|
||||
confirm_source="gold_qr_state",
|
||||
)
|
||||
return
|
||||
if qr_state is not None:
|
||||
state_text = {-2: "等待支付", 0: "支付处理中"}.get(
|
||||
qr_state, f"状态 {qr_state}"
|
||||
)
|
||||
self._schedule_payment_poll(
|
||||
db,
|
||||
task,
|
||||
result,
|
||||
f"鱼翅充值{state_text}"
|
||||
f"(当前鱼翅 {last_gold if last_gold is not None else '-'})",
|
||||
)
|
||||
return
|
||||
if balance_error:
|
||||
self._schedule_payment_poll(
|
||||
db,
|
||||
task,
|
||||
result,
|
||||
f"等待鱼翅到账: {balance_error}",
|
||||
)
|
||||
return
|
||||
|
||||
baseline_gold = self._to_int(result.get("baseline_gold_balance"))
|
||||
if baseline_gold is None:
|
||||
result["baseline_gold_balance"] = last_gold
|
||||
@@ -86,37 +217,16 @@ class GoldMixin:
|
||||
return
|
||||
# 余额必须增加才算到账,下降可能来自同账号送礼等消费。
|
||||
if last_gold is not None and last_gold > baseline_gold:
|
||||
result.pop("payment_poll_error", None)
|
||||
result["gold_recharged"] = True
|
||||
result["payment_polling"] = False
|
||||
amount = int(
|
||||
result.get("payment_amount")
|
||||
or self._task_payload(task).get("amount")
|
||||
or self._task_payload(task).get("gold_amount")
|
||||
or 1
|
||||
)
|
||||
channel = str(
|
||||
result.get("payment_channel")
|
||||
or config.get("gold_recharge_channel")
|
||||
or "wechat_qr"
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
record_recharge(
|
||||
db,
|
||||
account,
|
||||
task_id=task.id,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
balance_after=account.gold_balance,
|
||||
confirmed=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
self._finish_wallet_gold_recharge(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
f"鱼翅已到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}",
|
||||
account,
|
||||
result,
|
||||
amount=amount,
|
||||
channel=channel,
|
||||
order_id=str(result.get("order_id") or ""),
|
||||
balance_refreshed=True,
|
||||
confirm_source="balance_increase",
|
||||
)
|
||||
return
|
||||
result.pop("payment_poll_error", None)
|
||||
@@ -443,6 +553,7 @@ class GoldMixin:
|
||||
{
|
||||
"payment_amount": amount,
|
||||
"payment_channel": channel,
|
||||
"payment_token": str(result.get("token") or ""),
|
||||
"baseline_gold_balance": baseline_gold,
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user