search.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package search
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-view/conf"
  8. "go-common/app/interface/main/app-view/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, vmid 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("context_id", strconv.FormatInt(vmid, 10))
  40. params.Set("rec_type", "up_rec")
  41. params.Set("pagesize", "20")
  42. params.Set("service_area", "play_suggest")
  43. var res struct {
  44. Code int `json:"code"`
  45. TrackID string `json:"trackid"`
  46. Msg string `json:"msg"`
  47. Data []*search.Upper `json:"data"`
  48. }
  49. if err = d.client.Get(c, d.upper, ip, params, &res); err != nil {
  50. return
  51. }
  52. b, _ := json.Marshal(&res)
  53. log.Info("search list url(%s) response(%s)", d.upper+"?"+params.Encode(), b)
  54. code := ecode.Int(res.Code)
  55. if !code.Equal(ecode.OK) {
  56. err = errors.Wrap(code, d.upper+"?"+params.Encode())
  57. return
  58. }
  59. ups = res.Data
  60. trackID = res.TrackID
  61. return
  62. }