dao.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package audio
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/interface/main/app-card/model/card/audio"
  6. "go-common/app/interface/main/app-feed/conf"
  7. "go-common/library/ecode"
  8. httpx "go-common/library/net/http/blademaster"
  9. "go-common/library/net/metadata"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _audios = "/x/internal/v1/audio/menus/batch"
  15. )
  16. type Dao struct {
  17. client *httpx.Client
  18. getAudios string
  19. }
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. client: httpx.NewClient(c.HTTPClient),
  23. getAudios: c.Host.APICo + _audios,
  24. }
  25. return
  26. }
  27. func (d *Dao) Audios(c context.Context, ids []int64) (aum map[int64]*audio.Audio, err error) {
  28. ip := metadata.String(c, metadata.RemoteIP)
  29. params := url.Values{}
  30. params.Set("ids", xstr.JoinInts(ids))
  31. var res struct {
  32. Code int `json:"code"`
  33. Data map[int64]*audio.Audio `json:"data"`
  34. }
  35. if err = d.client.Get(c, d.getAudios, ip, params, &res); err != nil {
  36. return
  37. }
  38. if res.Code != ecode.OK.Code() {
  39. err = errors.Wrap(ecode.Int(res.Code), d.getAudios+"?"+params.Encode())
  40. return
  41. }
  42. aum = res.Data
  43. return
  44. }