新增和平小店兑换记录查询
This commit is contained in:
@@ -284,6 +284,26 @@ type xiaodianRoleContext struct {
|
||||
PlatID xiaodianFlexibleString `json:"platId"`
|
||||
}
|
||||
|
||||
type xiaodianOrderList struct {
|
||||
Data struct {
|
||||
Count xiaodianFlexibleString `json:"cnt"`
|
||||
List []xiaodianOrder `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type xiaodianOrder struct {
|
||||
BuyTime string `json:"dtBuyTime"`
|
||||
PayTime string `json:"dtPayTime"`
|
||||
Status string `json:"iStatus"`
|
||||
GoodsRaw string `json:"sGoodsInfo"`
|
||||
}
|
||||
|
||||
type xiaodianOrderGoods struct {
|
||||
GoodsID string `json:"iGoodsId"`
|
||||
GoodsName string `json:"sGoodsName"`
|
||||
Quantity xiaodianFlexibleString `json:"iQuantity"`
|
||||
}
|
||||
|
||||
// xiaodianEmbedQuery 获取斗鱼为当前账号签发的短时道具城参数。
|
||||
func (c *DouyuClient) xiaodianEmbedQuery(cookie string) (url.Values, error) {
|
||||
uid := cookieValue(cookie, "acf_uid")
|
||||
@@ -567,6 +587,81 @@ func (c *DouyuClient) XiaodianAssets(cookie string) (types.XiaodianAssets, error
|
||||
return types.XiaodianAssets{Success: true, Coupons: coupons, Fragments: fragments}, nil
|
||||
}
|
||||
|
||||
// XiaodianExchangeList 查询和平小店真实订单记录。
|
||||
func (c *DouyuClient) XiaodianExchangeList(cookie string) ([]types.XiaodianExchangeRecord, error) {
|
||||
if !c.Activity.IsXiaodian {
|
||||
return nil, errors.New("当前活动不是和平小店")
|
||||
}
|
||||
if strings.TrimSpace(cookie) == "" {
|
||||
return nil, errors.New("Cookie 为空")
|
||||
}
|
||||
embedQuery, err := c.xiaodianEmbedQuery(cookie)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
endpoint, err := url.Parse(strings.TrimRight(c.Activity.RoleURL, "/") + "/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
all := make([]types.XiaodianExchangeRecord, 0)
|
||||
const pageSize = 100
|
||||
for page := 1; page <= 100; page++ {
|
||||
query := endpoint.Query()
|
||||
query.Set("_service", "order.list")
|
||||
query.Set("_jsvar", "info")
|
||||
query.Set("acctype", "livelink")
|
||||
query.Set("_app_id", "2123")
|
||||
query.Set("hist", "0")
|
||||
query.Set("ps", strconv.Itoa(pageSize))
|
||||
query.Set("pn", strconv.Itoa(page))
|
||||
query.Set("_biz_code", "cjm")
|
||||
query.Set("_act_id", c.Activity.ActID)
|
||||
query.Set("authType", "delegate")
|
||||
query.Set("_sid", "8")
|
||||
query.Set("set", "8")
|
||||
for _, key := range []string{"gameId", "actId", "appId", "livePlatId", "code", "timestamp", "v", "sig"} {
|
||||
query.Set(key, embedQuery.Get(key))
|
||||
}
|
||||
endpoint.RawQuery = query.Encode()
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.setXiaodianHeaders(req)
|
||||
body, err := c.doText(req, "和平小店兑换记录", true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var payload xiaodianOrderList
|
||||
if err := parseXiaodianVariable(body, "info", &payload); err != nil {
|
||||
return nil, fmt.Errorf("和平小店兑换记录响应解析失败: %w", err)
|
||||
}
|
||||
for _, order := range payload.Data.List {
|
||||
var goods struct {
|
||||
List []xiaodianOrderGoods `json:"list"`
|
||||
}
|
||||
if strings.TrimSpace(order.GoodsRaw) != "" {
|
||||
if err := json.Unmarshal([]byte(order.GoodsRaw), &goods); err != nil {
|
||||
return nil, fmt.Errorf("和平小店订单商品解析失败: %w", err)
|
||||
}
|
||||
}
|
||||
for _, item := range goods.List {
|
||||
all = append(all, types.XiaodianExchangeRecord{
|
||||
CommodityID: item.GoodsID, CommodityName: item.GoodsName,
|
||||
Quantity: parseXiaodianInt(string(item.Quantity)), BuyTime: order.BuyTime,
|
||||
PayTime: order.PayTime, Status: order.Status,
|
||||
})
|
||||
}
|
||||
}
|
||||
totalCount := parseXiaodianInt(string(payload.Data.Count))
|
||||
if len(payload.Data.List) == 0 || (totalCount > 0 && len(all) >= totalCount) ||
|
||||
(totalCount <= 0 && len(payload.Data.List) < pageSize) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
func (c *DouyuClient) xiaodianAsset(cookie string, role xiaodianRoleContext, areaID, platID int, variable, service, field string, withTime bool) (int, error) {
|
||||
endpoint, err := url.Parse(strings.TrimRight(c.Activity.RoleURL, "/") + "/")
|
||||
if err != nil {
|
||||
@@ -936,6 +1031,21 @@ func (r ExchangeListResponse) APIStatus() (int, string, bool) {
|
||||
|
||||
// ExchangeList 查询兑换记录列表。
|
||||
func (c *DouyuClient) ExchangeList(cookie string) ([]types.ExchangeRecord, error) {
|
||||
if c.Activity.IsXiaodian {
|
||||
xiaodianRecords, err := c.XiaodianExchangeList(cookie)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
records := make([]types.ExchangeRecord, 0, len(xiaodianRecords))
|
||||
for _, record := range xiaodianRecords {
|
||||
records = append(records, types.ExchangeRecord{
|
||||
CommodityID: record.CommodityID, CommodityName: record.CommodityName,
|
||||
ShowName: record.CommodityName, ExchangeNum: record.Quantity,
|
||||
Time: record.BuyTime,
|
||||
})
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
var result ExchangeListResponse
|
||||
if err := c.getJSON(c.Activity.ExchangeListURL, cookie, "兑换记录", &result); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -230,6 +230,48 @@ func TestProductsAutomaticallyResolvesXiaodianRoleAndPaginates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestXiaodianExchangeListUsesOrderListAndParsesGoods(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/embed":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"txurlH5":"https://app.daoju.qq.com/?gameId=cjm&actId=18882&appId=bp_cf&livePlatId=douyu&code=code×tamp=1&v=2.0&sig=sig"}}`))
|
||||
case "/role/":
|
||||
if got := r.URL.Query().Get("_service"); got != "order.list" {
|
||||
t.Errorf("_service = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("code"); got != "code" {
|
||||
t.Errorf("code = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("_sid"); got != "8" {
|
||||
t.Errorf("_sid = %q", got)
|
||||
}
|
||||
_, _ = w.Write([]byte(`var info={"data":{"cnt":1,"list":[{"dtBuyTime":"2026-08-08 01:00:42","dtPayTime":"2026-08-08 01:00:55","iStatus":"3","sGoodsInfo":"{\"list\":[{\"iGoodsId\":\"1565395\",\"sGoodsName\":\"改名卡\",\"iQuantity\":\"2\"}]}"}]}};`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := &activity.Activity{
|
||||
ID: "xiaodian", DisplayName: "和平小店", RoomID: "9263298", ActAlias: "20260623KDQFH",
|
||||
ActID: "46195", RoleParamURL: server.URL + "/embed", RoleURL: server.URL + "/role", IsXiaodian: true,
|
||||
}
|
||||
cli := New(act, "", &trace.Recorder{})
|
||||
cli.HTTPClient = server.Client()
|
||||
|
||||
records, err := cli.XiaodianExchangeList("acf_uid=uid; acf_stk=stk; acf_ltkid=ltkid")
|
||||
if err != nil {
|
||||
t.Fatalf("XiaodianExchangeList() error = %v", err)
|
||||
}
|
||||
if len(records) != 1 {
|
||||
t.Fatalf("记录数量 = %d", len(records))
|
||||
}
|
||||
got := records[0]
|
||||
if got.CommodityID != "1565395" || got.CommodityName != "改名卡" || got.Quantity != 2 || got.Status != "3" {
|
||||
t.Fatalf("记录解析异常: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testCTNClient(t *testing.T, ctnURL string) *DouyuClient {
|
||||
t.Helper()
|
||||
act := &activity.Activity{
|
||||
|
||||
Reference in New Issue
Block a user