dao.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package bplus
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-card/model/bplus"
  5. "go-common/app/interface/main/app-feed/conf"
  6. "go-common/library/ecode"
  7. httpx "go-common/library/net/http/blademaster"
  8. "go-common/library/net/metadata"
  9. "net/url"
  10. "strconv"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _dynamicDetail = "/dynamic_detail/v0/dynamic/details"
  15. )
  16. // Dao is a dao.
  17. type Dao struct {
  18. // http client
  19. client *httpx.Client
  20. // ad
  21. dynamicDetail string
  22. }
  23. // New new a dao.
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. // http client
  27. client: httpx.NewClient(c.HTTPClient),
  28. dynamicDetail: c.Host.DynamicCo + _dynamicDetail,
  29. }
  30. return
  31. }
  32. // DynamicDetail is.
  33. func (d *Dao) DynamicDetail(c context.Context, ids ...int64) (picm map[int64]*bplus.Picture, err error) {
  34. ip := metadata.String(c, metadata.RemoteIP)
  35. params := url.Values{}
  36. for _, id := range ids {
  37. params.Add("dynamic_ids[]", strconv.FormatInt(id, 10))
  38. }
  39. var res struct {
  40. Code int `json:"code"`
  41. Data *struct {
  42. List []*bplus.Picture `json:"list"`
  43. } `json:"data"`
  44. }
  45. if err = d.client.Get(c, d.dynamicDetail, ip, params, &res); err != nil {
  46. return
  47. }
  48. if res.Code != ecode.OK.Code() {
  49. err = errors.Wrap(ecode.Int(res.Code), d.dynamicDetail+"?"+params.Encode())
  50. return
  51. }
  52. if res.Data != nil {
  53. picm = make(map[int64]*bplus.Picture, len(res.Data.List))
  54. for _, pic := range res.Data.List {
  55. picm[pic.DynamicID] = pic
  56. }
  57. }
  58. return
  59. }