dao.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package live
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/main/app-view/conf"
  9. "go-common/app/interface/main/app-view/model/live"
  10. "go-common/library/ecode"
  11. httpx "go-common/library/net/http/blademaster"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _list = "/room/v1/RoomMng/allLivingRoomInfo"
  16. _bnj2019Conf = "/activity/v0/bainian/config"
  17. )
  18. // Dao is space dao
  19. type Dao struct {
  20. client *httpx.Client
  21. list string
  22. bnj2019 string
  23. }
  24. // New initial space dao
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(c.HTTPAsync),
  28. list: c.Host.APILiveCo + _list,
  29. bnj2019: c.Host.APILiveCo + _bnj2019Conf,
  30. }
  31. return
  32. }
  33. // Living is get living rooms from api
  34. func (d *Dao) Living(c context.Context) (ll []*live.Live, err error) {
  35. params := url.Values{}
  36. params.Set("filter_user_cover", "0")
  37. params.Set("need_broadcast_type", "1")
  38. params.Set("extra_fields[]", "title")
  39. var res struct {
  40. Code int `json:"code"`
  41. Data []*live.RoomInfo `json:"data"`
  42. }
  43. if err = d.client.Get(c, d.list, "", params, &res); err != nil {
  44. return
  45. }
  46. if res.Code != ecode.OK.Code() {
  47. err = errors.Wrap(ecode.Int(res.Code), d.list)
  48. return
  49. }
  50. for _, info := range res.Data {
  51. l := &live.Live{}
  52. l.LiveChange(info)
  53. ll = append(ll, l)
  54. }
  55. return
  56. }
  57. // Bnj2019Conf 直播控制白名单
  58. func (d *Dao) Bnj2019Conf(c context.Context) (greyStatus int, mids []int64, err error) {
  59. var res struct {
  60. Code int `json:"code"`
  61. Data struct {
  62. GreyStatus int `json:"grey_status"`
  63. GreyUids string `json:"grey_uids"`
  64. } `json:"data"`
  65. }
  66. if err = d.client.Get(c, d.bnj2019, "", nil, &res); err != nil {
  67. return
  68. }
  69. if res.Code != ecode.OK.Code() {
  70. err = errors.Wrap(ecode.Int(res.Code), d.bnj2019)
  71. return
  72. }
  73. greyStatus = res.Data.GreyStatus
  74. if greyStatus == 1 {
  75. midsStr := strings.Split(res.Data.GreyUids, ",")
  76. for _, midStr := range midsStr {
  77. var mid int64
  78. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  79. err = errors.New(fmt.Sprintf("live grey_uids(%s)", res.Data.GreyUids))
  80. return
  81. }
  82. mids = append(mids, mid)
  83. }
  84. }
  85. return
  86. }