live.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/space/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _liveURI = "/room/v1/Room/getRoomInfoOld"
  15. _medalStatusURI = "/fans_medal/v1/medal/get_medal_opened"
  16. )
  17. // Live is space live data.
  18. func (d *Dao) Live(c context.Context, mid int64, platform string) (live *model.Live, err error) {
  19. var (
  20. params = url.Values{}
  21. ip = metadata.String(c, metadata.RemoteIP)
  22. )
  23. params.Set("mid", strconv.FormatInt(mid, 10))
  24. params.Set("platform", platform)
  25. var res struct {
  26. Code int `json:"code"`
  27. Data *model.Live `json:"data"`
  28. }
  29. if err = d.httpR.Get(c, d.liveURL, ip, params, &res); err != nil {
  30. return
  31. }
  32. if res.Code != ecode.OK.Code() {
  33. err = errors.Wrap(ecode.Int(res.Code), d.liveURL+"?"+params.Encode())
  34. return
  35. }
  36. live = res.Data
  37. return
  38. }
  39. // LiveMetal get live metal
  40. func (d *Dao) LiveMetal(c context.Context, mid int64) (rs bool, err error) {
  41. var (
  42. req *http.Request
  43. ip = metadata.String(c, metadata.RemoteIP)
  44. )
  45. if req, err = d.httpR.NewRequest(http.MethodGet, d.liveMetalURL, ip, url.Values{}); err != nil {
  46. log.Error("d.httpR.NewRequest(%s) error(%v)", d.liveMetalURL, err)
  47. return
  48. }
  49. req.Header.Set("X-BILILIVE-UID", strconv.FormatInt(mid, 10))
  50. var res struct {
  51. Code int `json:"code"`
  52. Data struct {
  53. MasterStatus int `json:"master_status"`
  54. } `json:"data"`
  55. }
  56. if err = d.httpR.Do(c, req, &res); err != nil {
  57. log.Error("d.httpR.Do(%s) error(%v)", d.liveMetalURL, err)
  58. return
  59. }
  60. if res.Code != ecode.OK.Code() {
  61. log.Error("d.httpR.Do(%s) code(%d) error", d.liveMetalURL, res.Code)
  62. err = ecode.Int(res.Code)
  63. return
  64. }
  65. if res.Data.MasterStatus == 1 {
  66. rs = true
  67. }
  68. return
  69. }