dao.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package audio
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/conf"
  5. "go-common/library/ecode"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "net/url"
  9. "go-common/app/interface/main/app-intl/model/view"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _audioByCids = "/audio/music-service-c/internal/songs-by-cids"
  15. )
  16. // Dao is archive dao.
  17. type Dao struct {
  18. // http client
  19. client *bm.Client
  20. audioByCidsURL string
  21. }
  22. // New new a archive dao.
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. // http client
  26. client: bm.NewClient(c.HTTPAudio),
  27. audioByCidsURL: c.Host.APICo + _audioByCids,
  28. }
  29. return
  30. }
  31. // AudioByCids is.
  32. func (d *Dao) AudioByCids(c context.Context, cids []int64) (vam map[int64]*view.Audio, err error) {
  33. ip := metadata.String(c, metadata.RemoteIP)
  34. params := url.Values{}
  35. params.Set("cids", xstr.JoinInts(cids))
  36. var res struct {
  37. Code int `json:"code"`
  38. Data map[int64]*view.Audio `json:"data"`
  39. }
  40. if err = d.client.Get(c, d.audioByCidsURL, ip, params, &res); err != nil {
  41. return
  42. }
  43. if res.Code != ecode.OK.Code() {
  44. err = errors.Wrap(ecode.Int(res.Code), d.audioByCidsURL+"?"+params.Encode())
  45. return
  46. }
  47. vam = res.Data
  48. return
  49. }