实现虎牙充值商品列表与支付二维码

This commit is contained in:
yml2213
2026-07-04 23:48:53 +08:00
parent fa0d49ff8c
commit 5513b5a313
12 changed files with 1209 additions and 61 deletions
+194
View File
@@ -120,6 +120,200 @@ class GetActPrizeListReq(TafStruct):
self.sid = ins.read_int32(1, default=self.sid)
class GetActTaskDetailReq(TafStruct):
"""webActUI.getActTaskDetail 请求。"""
def __init__(self):
self.userId = ActivityUserId()
self.actId: int = 0
def write_to(self, os: TafOutputStream):
os.write_struct(0, self.userId)
os.write_int32(1, self.actId)
def read_from(self, ins: TafInputStream):
self.userId = ins.read_struct(0, ActivityUserId) or self.userId
self.actId = ins.read_int32(1, default=self.actId)
class ActTaskPrizeInfo(TafStruct):
"""活动任务奖励项,刷新充值商品时只取展示需要字段。"""
def __init__(self):
self.actId: int = 0
self.taskId: int = 0
self.prizeId: int = 0
self.name: str = ""
self.count: int = 0
self.percentText: str = ""
self.extra: dict = {}
def read_from(self, ins: TafInputStream):
self.actId = ins.read_int64(0, default=self.actId)
self.taskId = ins.read_int64(1, default=self.taskId)
self.prizeId = ins.read_int64(2, default=self.prizeId)
self.name = ins.read_string(3, default=self.name)
self.count = ins.read_int64(4, default=self.count)
self.percentText = ins.read_string(9, default=self.percentText)
self.extra = ins.read_map(12)
while True:
pos = ins.buf.tell()
tag, dtype = ins.read_head()
if dtype == TafType.STRUCT_END:
ins.buf.seek(pos)
return
ins.skip_field(dtype)
def write_to(self, os: TafOutputStream):
pass
@staticmethod
def read_list_item(ins: TafInputStream, _tag: int):
item = ActTaskPrizeInfo()
item.read_from(ins)
_end_tag, dtype = ins.read_head()
if dtype != TafType.STRUCT_END:
raise ValueError(f"期望任务奖励 STRUCT_END,实际 0x{dtype:02x}")
return item
def to_dict(self) -> dict:
return {
"act_id": self.actId,
"task_id": self.taskId,
"prize_id": self.prizeId,
"name": self.name,
"count": self.count,
"percent_text": self.percentText,
"extra": dict(self.extra),
}
class ActTaskDetailItem(TafStruct):
"""活动任务项,字段位置来自 getActTaskDetail 实测响应。"""
def __init__(self):
self.actId: int = 0
self.taskId: int = 0
self.name: str = ""
self.taskStatus: int = 0
self.taskType: int = 0
self.limitType: int = 0
self.taskParams: str = ""
self.targetValue: str = ""
self.icon: str = ""
self.description: str = ""
self.prizes: list[ActTaskPrizeInfo] = []
self.taskUrl: str = ""
self.startTime: str = ""
self.endTime: str = ""
@staticmethod
def _extract_spu_id(text: str) -> str:
import urllib.parse
raw = str(text or "")
if not raw:
return ""
query = raw
if "?" in raw:
query = raw.split("?", 1)[1]
if "#" in query and "?" in query.split("#", 1)[1]:
query = query.split("#", 1)[1].split("?", 1)[1]
params = urllib.parse.parse_qs(query, keep_blank_values=True)
for key in ("spuId", "spu_id"):
value = params.get(key)
if value and value[0]:
return value[0]
for part in raw.replace("#", "&").split("&"):
if part.startswith("spuId="):
return urllib.parse.unquote(part.split("=", 1)[1])
return ""
def read_from(self, ins: TafInputStream):
self.actId = ins.read_int64(0, default=self.actId)
self.taskId = ins.read_int64(1, default=self.taskId)
self.name = ins.read_string(2, default=self.name)
self.taskStatus = ins.read_int32(3, default=self.taskStatus)
self.taskType = ins.read_int32(4, default=self.taskType)
self.limitType = ins.read_int32(5, default=self.limitType)
self.taskParams = ins.read_string(6, default=self.taskParams)
self.targetValue = ins.read_string(7, default=self.targetValue)
self.icon = ins.read_string(8, default=self.icon)
self.description = ins.read_string(9, default=self.description)
self.prizes = ins.read_list(11, item_reader=ActTaskPrizeInfo.read_list_item)
self.taskUrl = ins.read_string(12, default=self.taskUrl)
self.startTime = ins.read_string(25, default=self.startTime)
self.endTime = ins.read_string(26, default=self.endTime)
while True:
pos = ins.buf.tell()
tag, dtype = ins.read_head()
if dtype == TafType.STRUCT_END:
ins.buf.seek(pos)
return
ins.skip_field(dtype)
def write_to(self, os: TafOutputStream):
pass
@staticmethod
def read_list_item(ins: TafInputStream, _tag: int):
item = ActTaskDetailItem()
item.read_from(ins)
_end_tag, dtype = ins.read_head()
if dtype != TafType.STRUCT_END:
raise ValueError(f"期望任务 STRUCT_END,实际 0x{dtype:02x}")
return item
@property
def spu_id(self) -> str:
return self._extract_spu_id(self.taskUrl) or self._extract_spu_id(self.taskParams)
def to_dict(self) -> dict:
return {
"act_id": self.actId,
"task_id": self.taskId,
"name": self.name,
"task_status": self.taskStatus,
"task_type": self.taskType,
"limit_type": self.limitType,
"task_params": self.taskParams,
"target_value": self.targetValue,
"icon": self.icon,
"description": self.description,
"task_url": self.taskUrl,
"spu_id": self.spu_id,
"start_time": self.startTime,
"end_time": self.endTime,
"prizes": [item.to_dict() for item in self.prizes],
}
class GetActTaskDetailResp(TafStruct):
"""webActUI.getActTaskDetail 响应。"""
def __init__(self):
self.status: int = 0
self.msg: str = ""
self.tasks: list[ActTaskDetailItem] = []
def read_from(self, ins: TafInputStream):
self.status = ins.read_int32(0, default=self.status)
self.msg = ins.read_string(1, default=self.msg)
self.tasks = ins.read_list(2, item_reader=ActTaskDetailItem.read_list_item)
def write_to(self, os: TafOutputStream):
pass
def to_dict(self) -> dict:
tasks = [item.to_dict() for item in self.tasks]
return {
"status": self.status,
"msg": self.msg,
"task_count": len(tasks),
"tasks": tasks,
}
class ActPrizeItem(TafStruct):
"""活动商品项,字段位置来自 getActPrizeList 实测响应。"""
+27 -1
View File
@@ -286,6 +286,22 @@ class HuyaHttpClient:
timeout=timeout,
)
def get_act_task_detail(self, uid: int, cookie: str, act_id: int, timeout: float = 15.0):
"""查询活动任务详情,用于发现充值商品 SPU。"""
from .activity_structs import GetActTaskDetailReq, GetActTaskDetailResp
req = GetActTaskDetailReq()
req.userId = self._build_activity_user(uid, cookie)
req.actId = int(act_id or 0)
return self.call_rpc(
"webActUI",
"getActTaskDetail",
req,
GetActTaskDetailResp,
uid=uid,
cookie=cookie,
timeout=timeout,
)
def check_user_bind_game_account(
self,
uid: int,
@@ -593,6 +609,16 @@ class HuyaHttpClient:
self.logger(f" orderType={ot}: 无响应")
return last_result
@staticmethod
def _normalize_pay_channel(pay_type) -> str:
text = str(pay_type or "").strip()
lowered = text.lower()
if lowered in {"", "1", "zfb", "alipay", "支付宝"}:
return "Zfb"
if lowered in {"2", "wx", "weixin", "wechat", "微信"}:
return "Weixin"
return text
def pay_order_submit(self, uid, guid, cookie, order_id, pay_type=1,
pid=0, source_id="yellowcarlist", scene=7,
item_count=1):
@@ -604,7 +630,7 @@ class HuyaHttpClient:
os.write_struct(0, self._build_user(uid, guid, cookie))
os.write_struct(1, self._build_shop_app(source_id, scene))
os.write_int64(2, order_id)
os.write_string(3, "Zfb" if pay_type == 1 else str(pay_type))
os.write_string(3, self._normalize_pay_channel(pay_type))
os.write_string(4, "QrCode")
callback_url = (
f"https://m-shop.yaoguo.com/index.html#/consumer/paycallback"
+223 -3
View File
@@ -223,11 +223,11 @@ class GetGoodsInfoReqV5(TafStruct):
class GoodsInfoRsp(TafStruct):
"""商品查询响应(简化:只取关键字段)"""
"""商品查询响应"""
def __init__(self):
self.code: int = 0 # tag 0
self.message: str = "" # tag 1
# tag 2 是 goodsInfo 结构,简化跳过
self.goodsInfo = None # tag 2
self.selfGoods: int = 0 # tag 3
self.marketStatus: int = 0 # tag 5
self.timestamp: int = 0 # tag 6
@@ -235,7 +235,7 @@ class GoodsInfoRsp(TafStruct):
def read_from(self, ins: TafInputStream):
self.code = ins.read_int32(0, default=self.code)
self.message = ins.read_string(1, default=self.message)
# goodsInfo (tag 2) 跳过
self.goodsInfo = ins.read_struct(2, GoodsInfoDetail)
self.selfGoods = ins.read_int32(3, default=self.selfGoods)
self.marketStatus = ins.read_int32(5, default=self.marketStatus)
self.timestamp = ins.read_int64(6, default=self.timestamp)
@@ -243,6 +243,215 @@ class GoodsInfoRsp(TafStruct):
def write_to(self, os: TafOutputStream):
pass
@property
def spu_id(self) -> str:
return self.goodsInfo.baseInfo.spuId if self.goodsInfo else ""
@property
def sku_id(self) -> int:
return self.goodsInfo.priceInfo.first_sku_id if self.goodsInfo else 0
@property
def name(self) -> str:
return self.goodsInfo.baseInfo.name if self.goodsInfo else ""
@property
def price(self) -> int:
return self.goodsInfo.priceInfo.price if self.goodsInfo else 0
@property
def stock(self) -> int:
return self.goodsInfo.priceInfo.stock if self.goodsInfo else 0
@property
def buy_limit(self) -> int:
return self.goodsInfo.priceInfo.buyLimit if self.goodsInfo else 0
def to_dict(self) -> dict:
goods = self.goodsInfo.to_dict() if self.goodsInfo else {}
return {
"code": self.code,
"message": self.message,
"spu_id": goods.get("spu_id", ""),
"sku_id": goods.get("sku_id", 0),
"name": goods.get("name", ""),
"description": goods.get("description", ""),
"icon": goods.get("icon", ""),
"detail_url": goods.get("detail_url", ""),
"price": goods.get("price", 0),
"min_price": goods.get("min_price", 0),
"max_price": goods.get("max_price", 0),
"stock": goods.get("stock", 0),
"buy_limit": goods.get("buy_limit", 0),
"sku_list": goods.get("sku_list", []),
"self_goods": self.selfGoods,
"market_status": self.marketStatus,
"timestamp": self.timestamp,
"goods": goods,
}
class GoodsBaseInfo(TafStruct):
"""商品基础信息(getGoodsInfoV5 tag2.tag0)。"""
def __init__(self):
self.spuId: str = ""
self.appId: str = ""
self.name: str = ""
self.description: str = ""
self.icon: str = ""
self.detailUrl: str = ""
self.detailHtml: str = ""
def read_from(self, ins: TafInputStream):
self.spuId = ins.read_string(0, default=self.spuId)
self.appId = ins.read_string(1, default=self.appId)
self.name = ins.read_string(2, default=self.name)
self.description = ins.read_string(4, default=self.description)
self.icon = ins.read_string(5, default=self.icon)
self.detailUrl = ins.read_string(6, default=self.detailUrl)
self.detailHtml = ins.read_string(7, default=self.detailHtml)
_skip_to_struct_end(ins)
def write_to(self, os: TafOutputStream):
pass
def to_dict(self) -> dict:
return {
"spu_id": self.spuId,
"app_id": self.appId,
"name": self.name,
"description": self.description,
"icon": self.icon,
"detail_url": self.detailUrl,
}
class GoodsSkuItem(TafStruct):
"""商品 SKU 信息(getGoodsInfoV5 tag2.tag4.tag3 map value)。"""
def __init__(self):
self.skuId: int = 0
self.spuId: str = ""
self.price: int = 0
self.stock: int = 0
self.status: int = 0
def read_from(self, ins: TafInputStream):
self.skuId = ins.read_int64(0, default=self.skuId)
self.spuId = ins.read_string(1, default=self.spuId)
self.price = ins.read_int64(2, default=self.price)
self.stock = ins.read_int64(3, default=self.stock)
self.status = ins.read_int32(4, default=self.status)
_skip_to_struct_end(ins)
def write_to(self, os: TafOutputStream):
pass
@staticmethod
def read_map_value(ins: TafInputStream, dtype: int):
if dtype != TafType.STRUCT_BEGIN:
ins.skip_field(dtype)
return None
item = GoodsSkuItem()
item.read_from(ins)
_end_tag, end_type = ins.read_head()
if end_type != TafType.STRUCT_END:
raise ValueError(f"期望 SKU STRUCT_END,实际 0x{end_type:02x}")
return item
def to_dict(self) -> dict:
return {
"sku_id": self.skuId,
"spu_id": self.spuId,
"price": self.price,
"stock": self.stock,
"status": self.status,
}
class GoodsPriceInfo(TafStruct):
"""商品价格与 SKU 信息(getGoodsInfoV5 tag2.tag4)。"""
def __init__(self):
self.spuId: str = ""
self.minPrice: int = 0
self.maxPrice: int = 0
self.skuMap: Dict[int, GoodsSkuItem] = {}
self.stock: int = 0
self.buyLimit: int = 0
self.price: int = 0
def _read_sku_map(self, ins: TafInputStream):
found = ins._find_tag(3, required=False)
if not found:
return {}
if found[1] != TafType.MAP:
ins.skip_field(found[1])
return {}
count = ins._read_int_len()
result = {}
for _ in range(count):
_, key_type = ins.read_head()
key = ins._read_int_value(key_type)
_, value_type = ins.read_head()
item = GoodsSkuItem.read_map_value(ins, value_type)
if item is not None:
result[int(key)] = item
return result
def read_from(self, ins: TafInputStream):
self.spuId = ins.read_string(0, default=self.spuId)
self.minPrice = ins.read_int64(1, default=self.minPrice)
self.maxPrice = ins.read_int64(2, default=self.maxPrice)
self.skuMap = self._read_sku_map(ins)
self.stock = ins.read_int64(4, default=self.stock)
self.buyLimit = ins.read_int64(5, default=self.buyLimit)
self.price = ins.read_int64(7, default=self.price)
_skip_to_struct_end(ins)
def write_to(self, os: TafOutputStream):
pass
@property
def first_sku_id(self) -> int:
if not self.skuMap:
return 0
return sorted(self.skuMap.keys())[0]
@property
def sku_list(self) -> list[dict]:
return [self.skuMap[key].to_dict() for key in sorted(self.skuMap.keys())]
def to_dict(self) -> dict:
return {
"spu_id": self.spuId,
"sku_id": self.first_sku_id,
"price": self.price or self.minPrice,
"min_price": self.minPrice,
"max_price": self.maxPrice,
"stock": self.stock,
"buy_limit": self.buyLimit,
"sku_list": self.sku_list,
}
class GoodsInfoDetail(TafStruct):
"""getGoodsInfoV5 响应里的 goodsInfo 主体。"""
def __init__(self):
self.baseInfo = GoodsBaseInfo()
self.priceInfo = GoodsPriceInfo()
def read_from(self, ins: TafInputStream):
self.baseInfo = ins.read_struct(0, GoodsBaseInfo) or self.baseInfo
self.priceInfo = ins.read_struct(4, GoodsPriceInfo) or self.priceInfo
_skip_to_struct_end(ins)
def write_to(self, os: TafOutputStream):
pass
def to_dict(self) -> dict:
data = self.baseInfo.to_dict()
data.update(self.priceInfo.to_dict())
return data
# ============================================================
# 订单历史
@@ -581,3 +790,14 @@ class PayOrderRes(TafStruct):
def write_to(self, os: TafOutputStream):
pass
def to_dict(self) -> dict:
return {
"code": self.code,
"message": self.message,
"order_id": self.orderId,
"app_order_id": self.appOrderId,
"pay_order_id": self.payOrderId,
"pay_url": self.payUrl,
"amount": self.amount,
}