feat(huya): 统一实时商品目录并增加兑换代理开关

This commit is contained in:
yml
2026-11-13 20:12:42 +08:00
parent 563b32b7b7
commit ad782e1954
23 changed files with 930 additions and 511 deletions
+109 -53
View File
@@ -12,6 +12,10 @@ from core.huya.wss_client import POINTS_REDEEM_GOODS_NUM
from ..models import HuyaAccount, HuyaGoodsSnapshot, HuyaTask
from .huya_pay_link import pay_qr_path, resolve_pay_targets
from .huya_points_center import (
fetch_points_center_catalog,
points_center_options_from_config,
)
from .huya_points_exchange import (
EXCHANGE_STATUS_EXCHANGED,
EXCHANGE_STATUS_PAYMENT_REQUIRED,
@@ -53,6 +57,54 @@ class GoodsMixin:
def _wait_until(self, when: datetime, uid: int) -> bool: ...
def _parse_scheduled_time(self, value: Any) -> datetime | None: ...
def _resolve_live_exchange_goods(
self,
client: Any,
*,
uid: int,
cookie: str,
config_info: dict,
submitted_product_id: str,
product_name: str,
) -> tuple[dict | None, str]:
"""用实时积分中心商品把旧 prize_id 或过期选择解析成当前 SPU。
2026-11-13 两份最新 HAR 确认:页面商品列表使用 queryAllocation 返回的
``hy-*`` SPU;旧快照里的数字 prize_id 不能直接用于兑换。
"""
catalog = fetch_points_center_catalog(
uid,
cookie,
client=client,
**points_center_options_from_config(config_info),
)
goods, message = catalog.goods, catalog.message
if not goods:
return None, message or "实时商品列表为空"
wanted_name = product_name.strip()
wanted_id = normalize_spu_id(submitted_product_id)
if wanted_name:
by_name = [
item
for item in goods
if str(item.get("name") or "").strip() == wanted_name
]
if len(by_name) == 1:
return by_name[0], ""
if len(by_name) > 1 and wanted_id:
for item in by_name:
if normalize_spu_id(item.get("product_id")) == wanted_id:
return item, ""
if wanted_id:
for item in goods:
if normalize_spu_id(item.get("product_id")) == wanted_id:
return item, ""
return (
None,
"商品列表已更新,当前实时列表中找不到所选商品,请重新选择",
)
def _execute_query_act_tasks(
self,
worker_db: Session,
@@ -239,33 +291,21 @@ class GoodsMixin:
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
return
from core.huya.shop_structs import extract_points_center_sid
client: Any = self._http_client()
configured_sid = self._to_int(self.payload.get("sid") or config_info.get("sid"))
sid_int = configured_sid
sid_source = "config"
try:
page_code = (
str(config_info.get("points_page_code") or "").strip()
or "heping_baodian_credit_center_page"
# 2026-11-13 HAR:兑换记录只借 queryAllocation 取活动 SID,
# 不重复拉其它商品页签。
catalog = fetch_points_center_catalog(
uid,
cookie,
client=client,
include_all_tabs=False,
**points_center_options_from_config(config_info),
)
source_id = (
str(config_info.get("points_source_id") or "").strip()
or "yellowcarlist"
)
scene = self._to_int(config_info.get("points_scene")) or 7
allocation = client.play_mall_query_allocation(
uid=uid,
guid="",
cookie=cookie,
page_code=page_code,
source_id=source_id,
scene=scene,
deep=True,
)
payload = getattr(allocation, "payload", None)
discovered_sid = extract_points_center_sid(payload)
discovered_sid = catalog.sid
if discovered_sid:
sid_int = discovered_sid
sid_source = "queryAllocation"
@@ -335,36 +375,15 @@ class GoodsMixin:
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
return
from .huya_collab_service import fetch_points_center_gifts
sid_int = self._to_int(config_info.get("sid")) or 2203
page_code = (
str(config_info.get("points_page_code") or "").strip()
or "heping_baodian_credit_center_page"
)
source_id = (
str(config_info.get("points_source_id") or "").strip() or "yellowcarlist"
)
scene = self._to_int(config_info.get("points_scene")) or 7
page_id = self._to_int(config_info.get("points_page_id")) or None
tab_ids = [
int(part)
for part in str(config_info.get("points_tab_ids") or "")
.replace(",", ",")
.replace(" ", ",")
.split(",")
if part.isdigit()
]
goods, message = fetch_points_center_gifts(
query_options = points_center_options_from_config(config_info)
catalog = fetch_points_center_catalog(
uid,
cookie,
client=self._http_client(),
page_code=page_code,
source_id=source_id,
scene=scene,
page_id=page_id,
tab_ids=tab_ids or None,
**query_options,
)
goods, message = catalog.goods, catalog.message
now = datetime.now(UTC)
worker_db.query(HuyaGoodsSnapshot).filter(
HuyaGoodsSnapshot.account_id == account.id,
@@ -387,9 +406,9 @@ class GoodsMixin:
result = {
"source": "PlayMallUI.queryAllocation",
"sid": sid_int,
"page_code": page_code,
"source_id": source_id,
"scene": scene,
"page_code": query_options["page_code"],
"source_id": query_options["source_id"],
"scene": query_options["scene"],
"goods": goods,
}
if not goods:
@@ -413,8 +432,9 @@ class GoodsMixin:
account_info: dict,
config_info: dict,
):
spu_id = normalize_spu_id(self.payload.get("product_id"))
if not spu_id:
submitted_product_id = normalize_spu_id(self.payload.get("product_id"))
requested_product_name = str(self.payload.get("product_name") or "").strip()
if not submitted_product_id and not requested_product_name:
self._mark_task(worker_db, task, "failed", "请选择兑换商品")
return
@@ -447,7 +467,13 @@ class GoodsMixin:
pay_channel = self._normalize_pay_channel(
self.payload.get("pay_channel") or config_info.get("pay_channel")
)
proxies, proxy_source, proxy_error = self._exchange_proxies()
exchange_use_proxy = str(
config_info.get("exchange_use_proxy") or "0"
).strip().lower() in {"1", "true", "on", "yes"}
if exchange_use_proxy:
proxies, proxy_source, proxy_error = self._exchange_proxies()
else:
proxies, proxy_source, proxy_error = None, "disabled", ""
if proxy_error:
self._mark_task(
worker_db,
@@ -459,7 +485,35 @@ class GoodsMixin:
return
if proxies:
self._push_log("info", f"[{uid}] 兑换任务使用代理: {proxy_source}")
elif exchange_use_proxy:
self._push_log("info", f"[{uid}] 兑换任务未使用代理: {proxy_source}")
else:
self._push_log("info", f"[{uid}] 兑换任务直连(兑换代理开关关闭)")
client: Any = self._http_client(proxies=proxies)
live_goods, live_goods_error = self._resolve_live_exchange_goods(
client,
uid=uid,
cookie=cookie,
config_info=config_info,
submitted_product_id=submitted_product_id,
product_name=requested_product_name,
)
if live_goods is None:
self._mark_task(
worker_db,
task,
"failed",
f"实时商品校验失败:{live_goods_error}",
{
"submitted_product_id": submitted_product_id,
"product_name": requested_product_name,
"proxy_used": bool(proxies),
"proxy_source": proxy_source,
},
)
return
spu_id = normalize_spu_id(live_goods.get("product_id"))
product_name = str(live_goods.get("name") or requested_product_name or spu_id)
prepared = prepare_exchange(
client,
uid=uid,
@@ -472,8 +526,10 @@ class GoodsMixin:
)
common = {
"spu_id": spu_id,
"submitted_product_id": submitted_product_id,
"resolved_product_id": spu_id,
"product_name": (prepared.target.get("name") if prepared.target else "")
or str(self.payload.get("product_name") or spu_id),
or product_name,
"scheduled_at": scheduled_at.isoformat() if scheduled_at else "",
"executed_at": datetime.now(UTC).isoformat(),
"redeem_state": prepared.state.to_dict() if prepared.state else None,