search.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package search
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-feed/conf"
  8. "go-common/app/interface/main/app-feed/model/search"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/net/metadata"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _upper = "/main/recommend"
  17. )
  18. type Dao struct {
  19. client *httpx.Client
  20. upper string
  21. }
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. client: httpx.NewClient(c.HTTPSearch),
  25. upper: c.Host.Search + _upper,
  26. }
  27. return
  28. }
  29. func (d *Dao) Follow(c context.Context, platform, mobiApp, device, buvid string, build int, mid int64) (ups []*search.Upper, trackID string, err error) {
  30. ip := metadata.String(c, metadata.RemoteIP)
  31. params := url.Values{}
  32. params.Set("platform", platform)
  33. params.Set("mobi_app", mobiApp)
  34. params.Set("device", device)
  35. params.Set("clientip", ip)
  36. params.Set("build", strconv.Itoa(build))
  37. params.Set("buvid", buvid)
  38. params.Set("userid", strconv.FormatInt(mid, 10))
  39. params.Set("rec_type", "up_rec")
  40. params.Set("pagesize", "3")
  41. params.Set("service_area", "pegasus")
  42. var res struct {
  43. Code int `json:"code"`
  44. TrackID string `json:"trackid"`
  45. Msg string `json:"msg"`
  46. Data []*search.Upper `json:"data"`
  47. }
  48. if err = d.client.Get(c, d.upper, ip, params, &res); err != nil {
  49. return
  50. }
  51. b, _ := json.Marshal(&res)
  52. log.Info("search list url(%s) response(%s)", d.upper+"?"+params.Encode(), b)
  53. code := ecode.Int(res.Code)
  54. if !code.Equal(ecode.OK) {
  55. err = errors.Wrap(code, d.upper+"?"+params.Encode())
  56. return
  57. }
  58. ups = res.Data
  59. trackID = res.TrackID
  60. return
  61. }