dao.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package music
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/interface/main/favorite/conf"
  7. "go-common/app/interface/main/favorite/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. )
  14. const _music = "http://api.bilibili.co/x/internal/v1/audio/songs/batch"
  15. // Dao defeine fav Dao
  16. type Dao struct {
  17. httpClient *httpx.Client
  18. }
  19. // New return fav dao
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. httpClient: httpx.NewClient(c.HTTPClient),
  23. }
  24. return
  25. }
  26. // MusicMap return the music map data(all state).
  27. func (d *Dao) MusicMap(c context.Context, musicIds []int64) (data map[int64]*model.Music, err error) {
  28. params := url.Values{}
  29. params.Set("level", "1")
  30. params.Set("ids", xstr.JoinInts(musicIds))
  31. res := new(model.MusicResult)
  32. ip := metadata.String(c, metadata.RemoteIP)
  33. if err = d.httpClient.Get(c, _music, ip, params, res); err != nil {
  34. log.Error("d.HTTPClient.Get(%s?%s) error(%v)", _music, params.Encode())
  35. return
  36. }
  37. if res.Code != ecode.OK.Code() {
  38. log.Error("d.HTTPClient.Get(%s?%s) code:%d msg:%s", _music, params.Encode(), res.Code)
  39. err = fmt.Errorf("Get Music failed!code:=%v", res.Code)
  40. return
  41. }
  42. return res.Data, nil
  43. }