90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
|
||
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
||
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
||
|
"gitea.timerzz.com/kedaya_haitao/dw-spider/spider"
|
||
|
"github.com/gofiber/fiber/v3"
|
||
|
)
|
||
|
|
||
|
type SpiderSvc struct {
|
||
|
ctl *spider.Controller
|
||
|
sellerId v2.SellerId
|
||
|
}
|
||
|
|
||
|
func NewSpiderSvc(ctl *spider.Controller, sellerId v2.SellerId) *SpiderSvc {
|
||
|
return &SpiderSvc{
|
||
|
ctl: ctl,
|
||
|
sellerId: sellerId,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *SpiderSvc) Registry(r fiber.Router) {
|
||
|
api := r.Group("/api/v2")
|
||
|
api.Post(fmt.Sprintf("seller/%s/pull", s.sellerId), s.Pull)
|
||
|
api.Post(fmt.Sprintf("seller/%s/match", s.sellerId), s.Match)
|
||
|
api.Post(fmt.Sprintf("seller/%s/fetch/:id", s.sellerId), s.FetchArticlePrice)
|
||
|
//r.Post("spider/us/coach-outlet/calculate", s.UpsertCalculate)
|
||
|
//r.Delete("spider/us/coach-outlet/calculate/u/:id", s.DelCalculate)
|
||
|
}
|
||
|
|
||
|
func (s *SpiderSvc) Pull(ctx fiber.Ctx) error {
|
||
|
// 接收到,就爬取数据
|
||
|
go s.ctl.Crawl()
|
||
|
return ctx.JSON(web.NewResponse("ok", "ok"))
|
||
|
}
|
||
|
|
||
|
func (s *SpiderSvc) Match(ctx fiber.Ctx) error {
|
||
|
var article v2.SellerArticle
|
||
|
if err := ctx.Bind().JSON(&article); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if article.ID == 0 {
|
||
|
return fiber.ErrBadRequest
|
||
|
}
|
||
|
article, err := s.ctl.MatchDWSluId(article)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return ctx.JSON(web.NewResponse(article))
|
||
|
}
|
||
|
|
||
|
func (s *SpiderSvc) FetchArticlePrice(ctx fiber.Ctx) error {
|
||
|
i := ctx.Params("id")
|
||
|
id, _ := strconv.Atoi(i)
|
||
|
if id == 0 {
|
||
|
return fiber.ErrBadRequest
|
||
|
}
|
||
|
err := s.ctl.FetchArticlePrice(uint(id))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return ctx.JSON(web.NewResponse("ok"))
|
||
|
}
|
||
|
|
||
|
//func (s *SpiderSvc) DelCalculate(ctx fiber.Ctx) error {
|
||
|
// idStr := ctx.Params("id")
|
||
|
// if idStr == "" {
|
||
|
// return fiber.ErrBadRequest
|
||
|
// }
|
||
|
// id, _ := strconv.Atoi(idStr)
|
||
|
// if err := s.ctl.DeleteCalculateProcess(uint(id)); err != nil {
|
||
|
// return err
|
||
|
// }
|
||
|
// return nil
|
||
|
//}
|
||
|
//
|
||
|
//func (s *SpiderSvc) UpsertCalculate(ctx fiber.Ctx) error {
|
||
|
// var cs []productv1.CalculateProcess
|
||
|
// if err := ctx.Bind().JSON(&cs); err != nil {
|
||
|
// return err
|
||
|
// }
|
||
|
// if err := s.ctl.SaveCalculateProcess(cs); err != nil {
|
||
|
// return err
|
||
|
// }
|
||
|
// return nil
|
||
|
//}
|