小店商品列表shujuok
This commit is contained in:
+333
-3
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -212,15 +213,18 @@ func (c *DouyuClient) fetchServerTimeOnce() (types.TimeSample, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Products 拉取商城商品列表。
|
||||
func (c *DouyuClient) Products() ([]types.Product, error) {
|
||||
// Products 拉取当前活动的商品列表。
|
||||
func (c *DouyuClient) Products(cookie string, xiaodianOptions types.XiaodianProductListOptions) ([]types.Product, error) {
|
||||
if c.Activity.IsXiaodian {
|
||||
return c.xiaodianProducts(cookie, xiaodianOptions)
|
||||
}
|
||||
var result struct {
|
||||
types.APIResponse
|
||||
Data struct {
|
||||
List []types.Product `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := c.getJSON(c.Activity.StoreHomeURL, "", "商品列表", &result); err != nil {
|
||||
if err := c.getJSON(c.Activity.StoreHomeURL, cookie, "商品列表", &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
@@ -229,6 +233,286 @@ func (c *DouyuClient) Products() ([]types.Product, error) {
|
||||
return result.Data.List, nil
|
||||
}
|
||||
|
||||
type xiaodianRecommendation struct {
|
||||
ErrCode string `json:"errcode"`
|
||||
ErrDesc string `json:"errdesc"`
|
||||
Data struct {
|
||||
ClientData struct {
|
||||
Items []xiaodianItem `json:"itemsdetail"`
|
||||
} `json:"client_data"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type xiaodianItem struct {
|
||||
GoodsID string `json:"iGoodsId"`
|
||||
GoodsName string `json:"sGoodsName"`
|
||||
Price string `json:"iPrice"`
|
||||
GoodsLeft string `json:"iGoodsLeft"`
|
||||
GoodsPic string `json:"sGoodsPic"`
|
||||
ShowBegin string `json:"dtShowBeginTime"`
|
||||
ShowEnd string `json:"dtShowEndTime"`
|
||||
ActionID string `json:"iActionId"`
|
||||
}
|
||||
|
||||
type xiaodianFlexibleString string
|
||||
|
||||
// UnmarshalJSON 兼容道具接口把同一字段返回为字符串或数字。
|
||||
func (s *xiaodianFlexibleString) UnmarshalJSON(data []byte) error {
|
||||
value := strings.TrimSpace(string(data))
|
||||
if value == "" || value == "null" {
|
||||
*s = ""
|
||||
return nil
|
||||
}
|
||||
if strings.HasPrefix(value, `"`) {
|
||||
var decoded string
|
||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
*s = xiaodianFlexibleString(decoded)
|
||||
return nil
|
||||
}
|
||||
*s = xiaodianFlexibleString(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
type xiaodianRoleContext struct {
|
||||
OpenID xiaodianFlexibleString `json:"gameOpenId"`
|
||||
RoleID xiaodianFlexibleString `json:"roleId"`
|
||||
Type xiaodianFlexibleString `json:"type"`
|
||||
Area xiaodianFlexibleString `json:"area"`
|
||||
}
|
||||
|
||||
// xiaodianEmbedQuery 获取斗鱼为当前账号签发的短时道具城参数。
|
||||
func (c *DouyuClient) xiaodianEmbedQuery(cookie string) (url.Values, error) {
|
||||
uid := cookieValue(cookie, "acf_uid")
|
||||
stk := cookieValue(cookie, "acf_stk")
|
||||
ltkid := cookieValue(cookie, "acf_ltkid")
|
||||
if uid == "" || stk == "" || ltkid == "" {
|
||||
return nil, errors.New("斗鱼 Cookie 缺少 acf_uid、acf_stk 或 acf_ltkid,无法获取和平小店授权")
|
||||
}
|
||||
endpoint, err := url.Parse(c.Activity.RoleParamURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("和平小店授权接口地址无效: %w", err)
|
||||
}
|
||||
query := endpoint.Query()
|
||||
query.Set("actAlias", c.Activity.ActAlias)
|
||||
query.Set("rid", c.Activity.RoomID)
|
||||
query.Set("token", uid+"_1_"+stk+"_0_"+ltkid)
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.setBrowserHeaders(req, cookie)
|
||||
req.Header.Set("Origin", constants.BaseURL)
|
||||
req.Header.Set("Referer", "https://www.douyu.com/topic/h5/hpxd01")
|
||||
var result struct {
|
||||
types.APIResponse
|
||||
Data struct {
|
||||
TXURLH5 string `json:"txurlH5"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if _, err := c.doJSON(req, &result, "获取小店 H5 参数"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return nil, fmt.Errorf("获取和平小店授权失败: %s", result.Msg)
|
||||
}
|
||||
h5, err := url.Parse(result.Data.TXURLH5)
|
||||
if err != nil || result.Data.TXURLH5 == "" {
|
||||
return nil, errors.New("和平小店授权响应缺少 txurlH5")
|
||||
}
|
||||
return h5.Query(), nil
|
||||
}
|
||||
|
||||
// xiaodianRole 使用短时授权参数解析当前斗鱼账号绑定的游戏角色。
|
||||
func (c *DouyuClient) xiaodianRole(embedQuery url.Values) (xiaodianRoleContext, error) {
|
||||
endpoint, err := url.Parse(c.Activity.RoleURL)
|
||||
if err != nil {
|
||||
return xiaodianRoleContext{}, fmt.Errorf("和平小店角色接口地址无效: %w", err)
|
||||
}
|
||||
query := endpoint.Query()
|
||||
query.Set("acctype", "livelink")
|
||||
query.Set("_jsvar", "info")
|
||||
query.Set("_service", "other.livelink.getrole.out")
|
||||
query.Set("_biz_code", "cjm")
|
||||
query.Set("_act_id", c.Activity.ActID)
|
||||
query.Set("_app_id", "2123")
|
||||
query.Set("isCode", "1")
|
||||
for _, key := range []string{"gameId", "actId", "appId", "livePlatId", "code", "timestamp", "v", "sig"} {
|
||||
query.Set(key, embedQuery.Get(key))
|
||||
}
|
||||
query.Set("authType", "delegate")
|
||||
query.Set("sAnchorId", c.Activity.RoomID)
|
||||
query.Set("sVideoId", "")
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return xiaodianRoleContext{}, err
|
||||
}
|
||||
c.setXiaodianHeaders(req)
|
||||
body, err := c.doText(req, "查询小店绑定角色", true)
|
||||
if err != nil {
|
||||
return xiaodianRoleContext{}, err
|
||||
}
|
||||
var result xiaodianRoleContext
|
||||
if err := parseXiaodianVariable(body, "info", &result); err != nil {
|
||||
return xiaodianRoleContext{}, fmt.Errorf("和平小店角色响应解析失败: %w", err)
|
||||
}
|
||||
switch strings.ToLower(string(result.Type)) {
|
||||
case "wx":
|
||||
result.Area = "1"
|
||||
case "qq":
|
||||
result.Area = "2"
|
||||
}
|
||||
areaID := parseXiaodianInt(string(result.Area))
|
||||
if areaID <= 0 {
|
||||
areaID = 1
|
||||
}
|
||||
result.Area = xiaodianFlexibleString(strconv.Itoa(areaID))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func parseXiaodianVariable(body []byte, name string, out any) error {
|
||||
text := strings.TrimSpace(string(body))
|
||||
marker := "var " + name
|
||||
start := strings.Index(text, marker)
|
||||
if start < 0 {
|
||||
return fmt.Errorf("响应中找不到 var %s", name)
|
||||
}
|
||||
text = strings.TrimSpace(text[start+len(marker):])
|
||||
if !strings.HasPrefix(text, "=") {
|
||||
return fmt.Errorf("var %s 格式无效", name)
|
||||
}
|
||||
text = strings.TrimSpace(strings.TrimPrefix(text, "="))
|
||||
text = strings.TrimSuffix(text, ";")
|
||||
return json.Unmarshal([]byte(text), out)
|
||||
}
|
||||
|
||||
// xiaodianProducts 查询和平小店全部商品,接口单页最多返回 200 条。
|
||||
func (c *DouyuClient) xiaodianProducts(cookie string, options types.XiaodianProductListOptions) ([]types.Product, error) {
|
||||
if strings.TrimSpace(cookie) == "" {
|
||||
return nil, errors.New("和平小店需要至少一个有效斗鱼 Cookie")
|
||||
}
|
||||
if options.PageSize <= 0 || options.PageSize > 200 {
|
||||
options.PageSize = 200
|
||||
}
|
||||
embedQuery, err := c.xiaodianEmbedQuery(cookie)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
role, err := c.xiaodianRole(embedQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if role.OpenID == "" || role.RoleID == "" {
|
||||
return nil, errors.New("未获取到和平小店绑定角色,请确认该斗鱼账号已绑定游戏角色")
|
||||
}
|
||||
areaID := parseXiaodianInt(string(role.Area))
|
||||
if areaID <= 0 {
|
||||
areaID = 1
|
||||
}
|
||||
|
||||
endpoint, err := url.Parse(c.Activity.StoreHomeURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("和平小店商品接口地址无效: %w", err)
|
||||
}
|
||||
query := endpoint.Query()
|
||||
query.Set("_service", "act.recommend.query")
|
||||
query.Set("_biz_code", "cjm")
|
||||
query.Set("_app_id", "2123")
|
||||
query.Set("app_id", "2123")
|
||||
query.Set("page_size", strconv.Itoa(options.PageSize))
|
||||
query.Set("fields", "iRelationGoodsid,dtModifyTime,sBuyLimitInfo,sGoodsWaterMark,dtShowBeginTime,dtShowEndTime,iSort,iGoodsId,sGoodsName,iJbPrice,iJbOrgPrice,iJb2Price,iJb2OrgPrice,iPrice,iOrgPrice,sGoodsPic,sGoodsDesc,sPayType,dtBeginTime,dtEndTime,iActionId,sExtShowInfo,sExtInfo,iCategoryId,dtRushBegin,dtRushEnd")
|
||||
query.Set("actionFields", "iActionId,sActionName,dtBeginTime,dtEndTime")
|
||||
query.Set("order_by", "dtShowBeginTime")
|
||||
query.Set("orderColumnBatch", `[{"key":"iSort","sort":"desc"},{"key":"dtShowBeginTime","sort":"desc"},{"key":"iGoodsId","sort":"asc"}]`)
|
||||
query.Set("desc", "1")
|
||||
query.Set("_act_id", c.Activity.ActID)
|
||||
query.Set("actid", c.Activity.ActID)
|
||||
query.Set("excludeFields", "iCategoryId_76")
|
||||
query.Set("userid", string(role.OpenID))
|
||||
query.Set("roleid", string(role.RoleID))
|
||||
query.Set("area", "1")
|
||||
query.Set("areaid", strconv.Itoa(areaID))
|
||||
query.Set("need_limit", "1")
|
||||
products := make([]types.Product, 0)
|
||||
seen := make(map[string]struct{})
|
||||
for offset, pageNum := 0, 1; ; pageNum++ {
|
||||
pageQuery := make(url.Values, len(query))
|
||||
for key, values := range query {
|
||||
pageQuery[key] = append([]string(nil), values...)
|
||||
}
|
||||
pageQuery.Set("page_begin", strconv.Itoa(offset))
|
||||
pageQuery.Set("page_num", strconv.Itoa(pageNum))
|
||||
items, err := c.xiaodianProductPage(endpoint, pageQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, item := range items {
|
||||
if item.GoodsID == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[item.GoodsID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[item.GoodsID] = struct{}{}
|
||||
products = append(products, types.Product{
|
||||
CommodityID: item.GoodsID,
|
||||
CommodityName: item.GoodsName,
|
||||
Score: parseXiaodianInt(item.Price),
|
||||
Count: parseXiaodianInt(item.GoodsLeft),
|
||||
Pic: item.GoodsPic,
|
||||
})
|
||||
}
|
||||
if len(items) < options.PageSize {
|
||||
break
|
||||
}
|
||||
if pageNum >= 100 {
|
||||
return nil, errors.New("和平小店商品分页超出安全上限")
|
||||
}
|
||||
offset += options.PageSize
|
||||
}
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (c *DouyuClient) xiaodianProductPage(endpoint *url.URL, query url.Values) ([]xiaodianItem, error) {
|
||||
pageURL := *endpoint
|
||||
pageURL.RawQuery = query.Encode()
|
||||
req, err := http.NewRequest(http.MethodGet, pageURL.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.setXiaodianHeaders(req)
|
||||
body, err := c.doText(req, "和平小店商品列表", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result xiaodianRecommendation
|
||||
if err := parseXiaodianVariable(body, "recommend", &result); err != nil {
|
||||
return nil, fmt.Errorf("和平小店商品响应解析失败: %w", err)
|
||||
}
|
||||
if result.ErrCode != "" && result.ErrCode != "0" {
|
||||
return nil, fmt.Errorf("和平小店商品查询失败: %s", result.ErrDesc)
|
||||
}
|
||||
items := result.Data.ClientData.Items
|
||||
if items == nil {
|
||||
return nil, errors.New("和平小店商品响应格式异常")
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// parseXiaodianInt 将 QQ 道具接口中的字符串数值转为界面商品字段。
|
||||
func parseXiaodianInt(value string) int {
|
||||
parsed, err := strconv.Atoi(strings.TrimSpace(value))
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
// Score 查询账号积分余额。不同活动的积分接口方法/路径不同:
|
||||
// 和平手册用 POST /japi/oms/web/peace/credit/balance;
|
||||
// 电竞手册用 GET /wgapi/ordnc/activity/peacematch/score/balance。
|
||||
@@ -659,6 +943,41 @@ func (c *DouyuClient) doJSON(req *http.Request, out any, label string) (*http.Re
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// doText 执行返回 JavaScript 变量的接口请求,并复用统一网络诊断。
|
||||
func (c *DouyuClient) doText(req *http.Request, label string, redactBody bool) ([]byte, error) {
|
||||
start := time.Now()
|
||||
reqBody := c.Trace.RequestBody(req)
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
c.Trace.Record(label, req, 0, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
c.logNetworkError(label, req, time.Since(start), err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.Trace.RecordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
c.logNetworkError(label, req, time.Since(start), err)
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
traceBody := body
|
||||
if redactBody {
|
||||
traceBody = []byte("<redacted>")
|
||||
}
|
||||
c.Trace.RecordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, traceBody, "", 0, "")
|
||||
c.logNetworkHTTPError(label, req, resp.StatusCode, time.Since(start), body)
|
||||
return nil, fmt.Errorf("%s HTTP %d: %s", label, resp.StatusCode, utils.TrimForLog(string(body), 160))
|
||||
}
|
||||
traceBody := body
|
||||
if redactBody {
|
||||
traceBody = []byte("<redacted>")
|
||||
}
|
||||
c.Trace.RecordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, traceBody, "", 0, "")
|
||||
c.logNetworkSuccess(label, req, time.Since(start), 0, "")
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func shouldLogNetworkSuccess(label string) bool {
|
||||
return label != "服务时间"
|
||||
}
|
||||
@@ -857,3 +1176,14 @@ func (c *DouyuClient) setJSONHeaders(req *http.Request, cookie string) {
|
||||
c.setBrowserHeaders(req, cookie)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
// setXiaodianHeaders 设置和平小店商品列表请求所需的浏览器头。
|
||||
func (c *DouyuClient) setXiaodianHeaders(req *http.Request) {
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Accept-Language", "zh-CN,zh-Hans;q=0.9")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148, Douyu_IOS")
|
||||
req.Header.Set("Referer", "https://app.daoju.qq.com/")
|
||||
req.Header.Set("Sec-Fetch-Dest", "script")
|
||||
req.Header.Set("Sec-Fetch-Mode", "no-cors")
|
||||
req.Header.Set("Sec-Fetch-Site", "same-site")
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -126,6 +128,84 @@ func TestNetworkDiagnosticIncludesActivityAndFieldNamesOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductsAutomaticallyResolvesXiaodianRoleAndPaginates(t *testing.T) {
|
||||
var goodsOffsets []string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/embed":
|
||||
if !strings.Contains(r.Header.Get("Cookie"), "acf_uid=uid") {
|
||||
t.Error("授权请求未携带斗鱼 Cookie")
|
||||
}
|
||||
if got := r.URL.Query().Get("token"); got != "uid_1_stk_0_ltkid" {
|
||||
t.Errorf("token = %q", got)
|
||||
}
|
||||
_, _ = 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("code"); got != "code" {
|
||||
t.Errorf("code = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("_act_id"); got != "46195" {
|
||||
t.Errorf("_act_id = %q", got)
|
||||
}
|
||||
_, _ = w.Write([]byte(`var info = {"gameOpenId":"openid","roleId":1192083858,"type":"qq","area":1};`))
|
||||
case "/goods":
|
||||
offset := r.URL.Query().Get("page_begin")
|
||||
goodsOffsets = append(goodsOffsets, offset)
|
||||
if got := r.URL.Query().Get("areaid"); got != "2" {
|
||||
t.Errorf("areaid = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("page_size"); got != "2" {
|
||||
t.Errorf("page_size = %q", got)
|
||||
}
|
||||
items := []map[string]string{}
|
||||
switch offset {
|
||||
case "0":
|
||||
items = []map[string]string{{"iGoodsId": "g1", "sGoodsName": "商品一", "iPrice": "10", "iGoodsLeft": "-1"}, {"iGoodsId": "g2", "sGoodsName": "商品二", "iPrice": "20", "iGoodsLeft": "3"}}
|
||||
case "2":
|
||||
items = []map[string]string{{"iGoodsId": "g2", "sGoodsName": "重复商品", "iPrice": "20", "iGoodsLeft": "3"}, {"iGoodsId": "g3", "sGoodsName": "商品三", "iPrice": "30", "iGoodsLeft": "0"}}
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{"data": map[string]any{"client_data": map[string]any{"itemsdetail": items}}})
|
||||
_, _ = w.Write(append([]byte("var recommend="), append(body, ';')...))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := &activity.Activity{
|
||||
ID: "xiaodian",
|
||||
DisplayName: "和平小店",
|
||||
RoomID: "9263298",
|
||||
ActAlias: "20260623KDQFH",
|
||||
ActID: "46195",
|
||||
BaseURL: server.URL,
|
||||
ShopReferer: server.URL,
|
||||
RoleParamURL: server.URL + "/embed",
|
||||
RoleURL: server.URL + "/role",
|
||||
StoreHomeURL: server.URL + "/goods",
|
||||
IsXiaodian: true,
|
||||
}
|
||||
cli := New(act, "", &trace.Recorder{})
|
||||
cli.HTTPClient = server.Client()
|
||||
|
||||
products, err := cli.Products("acf_uid=uid; acf_stk=stk; acf_ltkid=ltkid", types.XiaodianProductListOptions{PageSize: 2})
|
||||
if err != nil {
|
||||
t.Fatalf("Products() error = %v", err)
|
||||
}
|
||||
if len(products) != 3 {
|
||||
t.Fatalf("商品数量 = %d,期望 3", len(products))
|
||||
}
|
||||
if products[0].CommodityID != "g1" || products[2].CommodityID != "g3" {
|
||||
t.Fatalf("商品顺序 = %#v", products)
|
||||
}
|
||||
if products[1].Count != 3 || products[2].Score != 30 {
|
||||
t.Fatalf("商品字段 = %#v", products)
|
||||
}
|
||||
if got := strings.Join(goodsOffsets, ","); got != strings.Join([]string{"0", strconv.Itoa(2), strconv.Itoa(4)}, ",") {
|
||||
t.Fatalf("分页偏移 = %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testCTNClient(t *testing.T, ctnURL string) *DouyuClient {
|
||||
t.Helper()
|
||||
act := &activity.Activity{
|
||||
|
||||
Reference in New Issue
Block a user