实现虎牙商品兑换并优化列表展示

This commit is contained in:
yml2213
2026-07-05 10:46:00 +08:00
parent b63d685f2f
commit 5534293ed1
5 changed files with 381 additions and 14 deletions
+125
View File
@@ -136,6 +136,131 @@ class GetUserPrizeRecordsReq(TafStruct):
self.sid = ins.read_int32(1, default=self.sid)
class ScoreExchangePrizeReq(TafStruct):
"""webActUI.scoreExchangePrize 请求。"""
def __init__(self):
self.userId = ActivityUserId()
self.sid: int = 0
self.pid: int = 0
self.ip: str = ""
self.clientEnv: dict = {}
self.source: str = ""
self.isRole: int = 1
def write_to(self, os: TafOutputStream):
os.write_struct(0, self.userId)
os.write_int32(1, self.sid)
os.write_int32(2, self.pid)
os.write_string(3, self.ip)
os.write_map(4, self.clientEnv)
os.write_string(5, self.source)
os.write_int32(6, self.isRole)
def read_from(self, ins: TafInputStream):
self.userId = ins.read_struct(0, ActivityUserId) or self.userId
self.sid = ins.read_int32(1, default=self.sid)
self.pid = ins.read_int32(2, default=self.pid)
self.ip = ins.read_string(3, default=self.ip)
self.clientEnv = ins.read_map(4)
self.source = ins.read_string(5, default=self.source)
self.isRole = ins.read_int32(6, default=self.isRole)
class ExchangeInfo(TafStruct):
"""积分兑换结果信息。"""
def __init__(self):
self.pid: int = 0
self.exchangeScore: int = 0
self.desc: str = ""
def read_from(self, ins: TafInputStream):
self.pid = ins.read_int32(1, default=self.pid)
self.exchangeScore = ins.read_int64(2, default=self.exchangeScore)
self.desc = ins.read_string(3, default=self.desc)
def write_to(self, os: TafOutputStream):
os.write_int32(1, self.pid)
os.write_int64(2, self.exchangeScore)
os.write_string(3, self.desc)
def to_dict(self) -> dict:
return {
"pid": self.pid,
"exchange_score": self.exchangeScore,
"desc": self.desc,
}
class ExchangeActPreCondition(TafStruct):
"""兑换前置条件提示。"""
def __init__(self):
self.conditionType: int = 0
self.conditionParams: dict = {}
self.tip: str = ""
self.jumpUrl: str = ""
self.buttonDesc: str = ""
def read_from(self, ins: TafInputStream):
self.conditionType = ins.read_int32(0, default=self.conditionType)
self.conditionParams = ins.read_map(1)
self.tip = ins.read_string(2, default=self.tip)
self.jumpUrl = ins.read_string(3, default=self.jumpUrl)
self.buttonDesc = ins.read_string(4, default=self.buttonDesc)
def write_to(self, os: TafOutputStream):
os.write_int32(0, self.conditionType)
os.write_map(1, self.conditionParams)
os.write_string(2, self.tip)
os.write_string(3, self.jumpUrl)
os.write_string(4, self.buttonDesc)
def to_dict(self) -> dict:
return {
"condition_type": self.conditionType,
"condition_params": dict(self.conditionParams),
"tip": self.tip,
"jump_url": self.jumpUrl,
"button_desc": self.buttonDesc,
}
class ScoreExchangePrizeResp(TafStruct):
"""webActUI.scoreExchangePrize 响应。"""
def __init__(self):
self.status: int = 0
self.msg: str = ""
self.orderId: str = ""
self.exchangeInfo = ExchangeInfo()
self.actPreCondition = ExchangeActPreCondition()
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.orderId = ins.read_string(3, default=self.orderId)
self.exchangeInfo = ins.read_struct(4, ExchangeInfo) or self.exchangeInfo
self.actPreCondition = ins.read_struct(5, ExchangeActPreCondition) or self.actPreCondition
def write_to(self, os: TafOutputStream):
os.write_int32(0, self.status)
os.write_string(1, self.msg)
os.write_string(3, self.orderId)
os.write_struct(4, self.exchangeInfo)
os.write_struct(5, self.actPreCondition)
def to_dict(self) -> dict:
return {
"status": self.status,
"msg": self.msg,
"order_id": self.orderId,
"exchange_info": self.exchangeInfo.to_dict(),
"act_pre_condition": self.actPreCondition.to_dict(),
}
class UserPrizeRecordItem(TafStruct):
"""用户兑换记录项,字段来自 getUserPrizeRecords 实测响应。"""
+17
View File
@@ -302,6 +302,23 @@ class HuyaHttpClient:
timeout=timeout,
)
def score_exchange_prize(self, uid: int, cookie: str, sid: int, pid: int, timeout: float = 15.0):
"""兑换活动积分商品。"""
from .activity_structs import ScoreExchangePrizeReq, ScoreExchangePrizeResp
req = ScoreExchangePrizeReq()
req.userId = self._build_activity_user(uid, cookie)
req.sid = int(sid or 0)
req.pid = int(pid or 0)
return self.call_rpc(
"webActUI",
"scoreExchangePrize",
req,
ScoreExchangePrizeResp,
uid=uid,
cookie=cookie,
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