From 44514fbf39c8a55d5d3b1fae18039ec76da71359 Mon Sep 17 00:00:00 2001 From: timerzz Date: Fri, 28 Mar 2025 15:50:27 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=B7=BB=E5=8A=A0RequestProductDetail?= =?UTF-8?q?List=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E8=8E=B7=E5=8F=96=E5=95=86=E5=93=81=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/coach-client/ca.go | 15 +++++++++++++++ pkg/coach-client/ca_test.go | 4 ++-- pkg/coach-client/us.go | 16 ++++++++++++++++ pkg/coach-client/us_test.go | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/coach-client/ca.go b/pkg/coach-client/ca.go index 865eac0..e4d8e7c 100644 --- a/pkg/coach-client/ca.go +++ b/pkg/coach-client/ca.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "strings" "time" "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" @@ -98,6 +99,20 @@ func (c *ca) RequestProductDetail(ctx context.Context, pid string) (data Product 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) { if page < 1 { page = 1 diff --git a/pkg/coach-client/ca_test.go b/pkg/coach-client/ca_test.go index d1338ff..26ab401 100644 --- a/pkg/coach-client/ca_test.go +++ b/pkg/coach-client/ca_test.go @@ -22,9 +22,9 @@ func TestCAClient(t *testing.T) { //} //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 { t.Fatal(err) } - t.Log(inv) + t.Log(resp) } diff --git a/pkg/coach-client/us.go b/pkg/coach-client/us.go index 4a038ec..cdab408 100644 --- a/pkg/coach-client/us.go +++ b/pkg/coach-client/us.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "strings" "time" "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" @@ -108,6 +109,7 @@ func (u *USClientBuilder) CreateByType(clientType string) USCAClient { type USCAClient interface { RequestInventory(ctx context.Context, pid string) (inv Inventory, 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) BaseUrl() string } @@ -236,6 +238,20 @@ func (c *us) RequestProductDetail(ctx context.Context, pid string) (data Product 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 { PageData struct { Total int `json:"total"` diff --git a/pkg/coach-client/us_test.go b/pkg/coach-client/us_test.go index 04c360e..fbbd357 100644 --- a/pkg/coach-client/us_test.go +++ b/pkg/coach-client/us_test.go @@ -15,7 +15,7 @@ func TestClient(t *testing.T) { Interval: 10 * time.Minute, }) 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 { t.Fatal(err) }