feat(douyu): 支持和平小店商品兑换

This commit is contained in:
yml2213
2026-08-07 22:47:13 +08:00
parent a67d144b78
commit ec5eb637bb
6 changed files with 350 additions and 10 deletions
+100 -1
View File
@@ -455,7 +455,10 @@ class DouyuBatchRunner:
row.price = self._to_int(raw.get("price") or raw.get("iPrice"))
row.org_price = self._to_int(raw.get("org_price") or raw.get("iOrgPrice"))
row.category = str(raw.get("category") or raw.get("iCategoryId") or "")
row.goods_left = self._to_int(raw.get("goods_left") or raw.get("iGoodsLeft"))
goods_left = raw.get("goods_left")
if goods_left is None:
goods_left = raw.get("iGoodsLeft")
row.goods_left = self._to_int(goods_left)
row.raw = raw
row.updated_at = now
db.commit()
@@ -1414,6 +1417,101 @@ class DouyuBatchRunner:
{"fragments": fragments, "role": role, "area_id": int(areaid)},
)
def _execute_exchange_xpd_goods(
self,
db: Session,
task: DouyuTask,
account: Account,
cookie: str,
config: dict,
):
"""兑换和平小店商品,使用本次签发的道聚城短时授权。"""
payload = self._task_payload(task)
commodity_id = str(payload.get("commodity_id") or payload.get("commodityId") or "").strip()
if not commodity_id:
self._mark_task(db, task, "failed", "请选择小店商品")
return
try:
pay_type = int(payload.get("pay_type") or 1)
except (TypeError, ValueError):
self._mark_task(db, task, "failed", "兑换货币参数无效")
return
if pay_type not in (1, 5):
self._mark_task(db, task, "failed", "小店兑换仅支持点券或扭蛋碎片")
return
goods = (
db.query(DouyuXpdGoodsSnapshot)
.filter(DouyuXpdGoodsSnapshot.commodity_id == commodity_id)
.first()
)
if not goods:
self._mark_task(db, task, "failed", "未找到小店商品快照,请先刷新商品列表")
return
goods_snapshot = goods.raw if isinstance(goods.raw, dict) else {}
goods_raw = goods_snapshot.get("raw") if isinstance(goods_snapshot.get("raw"), dict) else goods_snapshot
price_key = "iPrice" if pay_type == 1 else "iJb2Price"
price = self._to_int(goods_raw.get(price_key))
if price is None:
price = goods.price if pay_type == 1 else None
if price is None or price <= 0:
currency = "点券" if pay_type == 1 else "扭蛋碎片"
self._mark_task(db, task, "failed", f"该商品不支持使用{currency}兑换")
return
if goods.goods_left is not None and goods.goods_left <= 0:
self._mark_task(db, task, "failed", "该商品库存不足,请刷新商品列表后重试")
return
client = self._client(cookie)
embed = client.xpd_embed_query(
act_alias=str(config["xpd_act_alias"]),
rid=str(config["xpd_rid"]),
)
role: dict = {}
try:
role = client.xpd_get_role(
embed_query=embed["query"],
act_id=str(config["xpd_act_id"]),
rid=str(config["xpd_rid"]),
)
if role.get("role_id"):
self._apply_xpd_role_to_account(account, role, self._xpd_area_id(role, account))
except DouyuActivityError as exc:
self._push_log("warning", f"小店兑换前刷新角色失败,使用已保存角色: {exc}")
if not role.get("role_id") and not account.xpd_role_id:
self._mark_task(db, task, "failed", "未获取到小店绑定角色,请先生成二维码扫码绑定")
return
result = client.xpd_exchange_goods(
embed_query=embed["query"],
act_id=str(config["xpd_act_id"]),
rid=str(config["xpd_rid"]),
commodity_id=commodity_id,
price=price,
picture=str(goods_raw.get("sGoodsPic") or ""),
pay_type=pay_type,
action_id=str(goods_raw.get("iActionId") or ""),
)
if pay_type == 1 and result.get("new_balance") is not None:
account.xpd_balance = result["new_balance"]
if pay_type == 5 and result.get("new_balance") is not None:
account.xpd_fragments = result["new_balance"]
account.xpd_bind_status = "xpd_goods_exchanged"
account.updated_at = datetime.now(timezone.utc)
db.commit()
currency = "点券" if pay_type == 1 else "扭蛋碎片"
display_role = role.get("role_name") or account.xpd_game_name or ""
channel = "微信" if (role.get("type") == "wx" or account.xpd_area_id == 1) else "手Q"
result.update({
"goods": goods_raw,
"game_name": display_role,
"game_channel": channel,
"account_name": account.nickname or account.username or account.uid or f"#{account.id}",
"currency": currency,
})
self._mark_task(db, task, "success", f"兑换小店商品成功: {goods.name or commodity_id}{price}{currency}", result)
def _execute_get_bind_qr(self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict):
client = self._client(cookie)
qr_act_alias = self._bind_qr_act_alias(config)
@@ -2710,6 +2808,7 @@ class DouyuBatchRunner:
"refresh_xpd_goods": self._execute_refresh_xpd_goods,
"query_xpd_balance": self._execute_query_xpd_balance,
"query_xpd_fragments": self._execute_query_xpd_fragments,
"exchange_xpd_goods": self._execute_exchange_xpd_goods,
}.get(task.task_type)
if handler is None:
self._mark_task(worker_db, task, "failed", "不支持的任务类型")
+1
View File
@@ -44,6 +44,7 @@ SUPPORTED_DOUYU_TASK_TYPES = { "get_bind_qr": "获取绑定二维码",
"refresh_xpd_goods": "刷新小店商品列表",
"query_xpd_balance": "查询小店点券余额",
"query_xpd_fragments": "查询小店扭蛋碎片",
"exchange_xpd_goods": "兑换小店商品",
}