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

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
+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,
}