update 添加RequestProductDetailList方法,用于批量获取商品信息

This commit is contained in:
timerzz 2025-03-28 15:50:27 +08:00
parent 89a406d32f
commit 44514fbf39
4 changed files with 34 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
"strings"
"time" "time"
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
@ -98,6 +99,20 @@ func (c *ca) RequestProductDetail(ctx context.Context, pid string) (data Product
return return
} }
func (c *ca) RequestProductDetailList(ctx context.Context, pids ...string) (list []*ProductData, err error) {
sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut)
defer cancel()
var resp ProductDataResponse
var ids = strings.Join(pids, ",")
urlPath := fmt.Sprintf("/api/get-products?ids=%s&includeInventory=false", url.QueryEscape(ids))
err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator())
if len(resp.ProductData) == 0 && err != nil {
err = fmt.Errorf("获取详情信息为空")
return
}
return resp.ProductData, nil
}
func (c *ca) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) { func (c *ca) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) {
if page < 1 { if page < 1 {
page = 1 page = 1

View File

@ -22,9 +22,9 @@ func TestCAClient(t *testing.T) {
//} //}
//t.Log(resp) //t.Log(resp)
inv, err := client.RequestProductDetail(ctx, "CP150-LHBLK") resp, err := client.RequestProductDetailList(ctx, "CK535-WEC", "CP081-SVVSW", "C9949-SVVDT", "CM235-SVELC", "CP081-JIBLK", "CAA92-IMCHR", "C9949-SVQVE", "CW637-B4CED")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(inv) t.Log(resp)
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
"strings"
"time" "time"
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
@ -108,6 +109,7 @@ func (u *USClientBuilder) CreateByType(clientType string) USCAClient {
type USCAClient interface { type USCAClient interface {
RequestInventory(ctx context.Context, pid string) (inv Inventory, err error) RequestInventory(ctx context.Context, pid string) (inv Inventory, err error)
RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error) RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error)
RequestProductDetailList(ctx context.Context, pids ...string) (list []*ProductData, err error)
ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error)
BaseUrl() string BaseUrl() string
} }
@ -236,6 +238,20 @@ func (c *us) RequestProductDetail(ctx context.Context, pid string) (data Product
return return
} }
func (c *us) RequestProductDetailList(ctx context.Context, pids ...string) (list []*ProductData, err error) {
sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut)
defer cancel()
var resp ProductDataResponse
var ids = strings.Join(pids, ",")
urlPath := fmt.Sprintf("/api/get-products?ids=%s&includeInventory=false", url.QueryEscape(ids))
err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator())
if len(resp.ProductData) == 0 && err != nil {
err = fmt.Errorf("获取详情信息为空")
return
}
return resp.ProductData, nil
}
type PageDataResponse struct { type PageDataResponse struct {
PageData struct { PageData struct {
Total int `json:"total"` Total int `json:"total"`

View File

@ -15,7 +15,7 @@ func TestClient(t *testing.T) {
Interval: 10 * time.Minute, Interval: 10 * time.Minute,
}) })
client := US(pool).CreateByType(USClient_Type_Coach) client := US(pool).CreateByType(USClient_Type_Coach)
resp, err := client.ViewAllBags(context.Background(), 1) resp, err := client.RequestProductDetailList(context.Background(), "CK535-WEC", "CP081-SVVSW", "C9949-SVVDT", "CM235-SVELC", "CP081-JIBLK", "CAA92-IMCHR", "C9949-SVQVE", "CW637-B4CED")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }