dao.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package live
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-card/model/card/live"
  8. "go-common/app/interface/main/app-feed/conf"
  9. model "go-common/app/interface/main/app-feed/model/live"
  10. "go-common/library/ecode"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/net/metadata"
  13. "go-common/library/xstr"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _appMRoom = "/room/v1/Room/rooms_for_app_index"
  18. _feedList = "/feed/v1/feed/getList"
  19. _card = "/room/v1/RoomRecommend/getInfoByCardId"
  20. )
  21. // Dao is show dao.
  22. type Dao struct {
  23. // http client
  24. client *httpx.Client
  25. clientAsyn *httpx.Client
  26. // live
  27. appMRoom string
  28. feedList string
  29. card string
  30. }
  31. // New new a bangumi dao.
  32. func New(c *conf.Config) (d *Dao) {
  33. d = &Dao{
  34. // http client
  35. client: httpx.NewClient(c.HTTPClient),
  36. clientAsyn: httpx.NewClient(c.HTTPClientAsyn),
  37. appMRoom: c.Host.LiveAPI + _appMRoom,
  38. feedList: c.Host.LiveAPI + _feedList,
  39. card: c.Host.LiveAPI + _card,
  40. }
  41. return
  42. }
  43. func (d *Dao) AppMRoom(c context.Context, roomids []int64) (rs map[int64]*live.Room, err error) {
  44. ip := metadata.String(c, metadata.RemoteIP)
  45. params := url.Values{}
  46. params.Set("room_ids", xstr.JoinInts(roomids))
  47. var res struct {
  48. Code int `json:"code"`
  49. Data []*live.Room `json:"data"`
  50. }
  51. if err = d.client.Get(c, d.appMRoom, ip, params, &res); err != nil {
  52. return
  53. }
  54. if res.Code != ecode.OK.Code() {
  55. err = errors.Wrap(err, d.appMRoom+"?"+params.Encode())
  56. return
  57. }
  58. rs = make(map[int64]*live.Room, len(res.Data))
  59. for _, r := range res.Data {
  60. rs[r.RoomID] = r
  61. }
  62. return
  63. }
  64. func (d *Dao) FeedList(c context.Context, mid int64, pn, ps int) (fs []*model.Feed, count int, err error) {
  65. var req *http.Request
  66. params := url.Values{}
  67. params.Set("page", strconv.Itoa(pn))
  68. params.Set("pagesize", strconv.Itoa(ps))
  69. if req, err = d.client.NewRequest("GET", d.feedList, "", params); err != nil {
  70. return
  71. }
  72. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  73. var res struct {
  74. Code int `json:"code"`
  75. Data struct {
  76. Rooms []*model.Feed `json:"rooms"`
  77. Count int `json:"count"`
  78. } `json:"data"`
  79. }
  80. if err = d.client.Do(c, req, &res); err != nil {
  81. return
  82. }
  83. if res.Code != ecode.OK.Code() {
  84. err = errors.Wrap(err, d.feedList+"?"+params.Encode())
  85. return
  86. }
  87. fs = res.Data.Rooms
  88. count = res.Data.Count
  89. return
  90. }
  91. func (d *Dao) Card(c context.Context) (csm map[int64][]*live.Card, err error) {
  92. var res struct {
  93. Code int `json:"code"`
  94. Data map[int64][]*live.Card `json:"data"`
  95. }
  96. if err = d.clientAsyn.Get(c, d.card, "", nil, &res); err != nil {
  97. return
  98. }
  99. if res.Code != ecode.OK.Code() {
  100. err = errors.Wrap(err, d.card)
  101. return
  102. }
  103. csm = res.Data
  104. return
  105. }
  106. func (d *Dao) Rooms(c context.Context, roomids []int64, ip string) (rs map[int64]*live.Room, err error) {
  107. params := url.Values{}
  108. params.Set("room_ids", xstr.JoinInts(roomids))
  109. var res struct {
  110. Code int `json:"code"`
  111. Data []*live.Room `json:"data"`
  112. }
  113. if err = d.clientAsyn.Get(c, d.appMRoom, ip, params, &res); err != nil {
  114. return
  115. }
  116. if res.Code != ecode.OK.Code() {
  117. err = errors.Wrap(err, d.appMRoom+"?"+params.Encode())
  118. return
  119. }
  120. rs = make(map[int64]*live.Room, len(res.Data))
  121. for _, r := range res.Data {
  122. rs[r.RoomID] = r
  123. }
  124. return
  125. }