From eb0b51c2f300cfab8a06e4382752bbe84e60f3d9 Mon Sep 17 00:00:00 2001 From: timerzz Date: Thu, 21 Nov 2024 17:21:17 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/spider.go | 13 +++++++++++++ spider/controller.go | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/server/spider.go b/server/spider.go index 63b3b2b..6004c53 100644 --- a/server/spider.go +++ b/server/spider.go @@ -28,6 +28,7 @@ func (s *SpiderSvc) Registry(r fiber.Router) { api.Post(fmt.Sprintf("provider/%s/price/fetch/:id", s.providerId), s.FetchArticlePrice) api.Post(fmt.Sprintf("provider/%s/detail/fetch/:pid", s.providerId), s.FetchArticleDetail) api.Post(fmt.Sprintf("provider/%s/ats/fetch/:pid", s.providerId), s.FetchArticleAts) + api.Get(fmt.Sprintf("provider/%s/ats/:pid", s.providerId), s.GetArticleAts) //r.Post("spider/us/coach-outlet/calculate", s.UpsertCalculate) //r.Delete("spider/us/coach-outlet/calculate/u/:id", s.DelCalculate) @@ -73,6 +74,18 @@ func (s *SpiderSvc) FetchArticleAts(ctx fiber.Ctx) error { return ctx.JSON(web.NewResponse("ok")) } +func (s *SpiderSvc) GetArticleAts(ctx fiber.Ctx) error { + pid := ctx.Params("pid") + if pid == "" { + return fmt.Errorf("pid is empty") + } + ats, err := s.ctl.GetArticleAts(ctx.Context(), pid) + if err != nil { + return err + } + return ctx.JSON(web.NewResponse(ats)) +} + //func (s *SpiderSvc) DelCalculate(ctx fiber.Ctx) error { // idStr := ctx.Params("id") // if idStr == "" { diff --git a/spider/controller.go b/spider/controller.go index 2d8d5e4..007c3ee 100644 --- a/spider/controller.go +++ b/spider/controller.go @@ -325,6 +325,14 @@ func (c *Controller) FetchArticleAts(ctx context.Context, pid string) error { return c.storage.ProviderArticle().Update(pArticle, "ast") } +func (c *Controller) GetArticleAts(ctx context.Context, pid string) (int, error) { + inv, err := c.client.RequestInventory(ctx, pid) + if err != nil { + return 0, fmt.Errorf("请求商品库存失败: %v", err) + } + return inv.Ats, nil +} + // 更新某个商品的价格 func (c *Controller) FetchArticlePrice(ctx context.Context, id uint) error { pArticle, err := c.storage.ProviderArticle().Get(storage.NewGetProviderArticleQuery().SetProviderId(c.providerId).SetID(id))