cards.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. // TicketCard get ticket card from api
  13. func (d *Dao) TicketCard(c context.Context, ids []int64) (resp map[int64]*model.TicketCard, err error) {
  14. params := url.Values{}
  15. params.Set("id", xstr.JoinInts(ids))
  16. params.Set("for", "2")
  17. params.Set("tag", "0")
  18. params.Set("price", "1")
  19. params.Set("imgtype", "2")
  20. params.Set("rettype", "1")
  21. var res struct {
  22. Code int `json:"errno"`
  23. Msg string `json:"msg"`
  24. Data map[int64]*model.TicketCard `json:"data"`
  25. }
  26. err = d.httpClient.Get(c, d.c.Cards.TicketURL, "", params, &res)
  27. if err != nil {
  28. PromError("cards:ticket接口")
  29. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.TicketURL+"?"+params.Encode(), err)
  30. return
  31. }
  32. if res.Code != 0 {
  33. PromError("cards:ticket接口")
  34. log.Error("cards: url(%s) res code(%d) msg: %s", d.c.Cards.TicketURL+"?"+params.Encode(), res.Code, res.Msg)
  35. err = ecode.Int(res.Code)
  36. return
  37. }
  38. resp = res.Data
  39. return
  40. }
  41. // MallCard .
  42. func (d *Dao) MallCard(c context.Context, ids []int64) (resp map[int64]*model.MallCard, err error) {
  43. idsStr := `{"itemsIdList":[` + xstr.JoinInts(ids) + "]}"
  44. req, err := http.NewRequest("POST", d.c.Cards.MallURL, strings.NewReader(idsStr))
  45. if err != nil {
  46. PromError("cards:mall接口")
  47. log.Error("cards: NewRequest(%s) error(%+v)", d.c.Cards.MallURL+"?"+idsStr, err)
  48. return
  49. }
  50. var res struct {
  51. Code int `json:"code"`
  52. Data struct {
  53. List []*model.MallCard `json:"list"`
  54. } `json:"data"`
  55. }
  56. req.Header.Set("Content-Type", "application/json")
  57. req.Header.Set("Accept", "application/json")
  58. req.Header.Set("X-BACKEND-BILI-REAL-IP", "")
  59. err = d.httpClient.Do(c, req, &res)
  60. if err != nil {
  61. PromError("cards:mall接口")
  62. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.MallURL+"?"+idsStr, err)
  63. return
  64. }
  65. if res.Code != 0 {
  66. PromError("cards:mall接口")
  67. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.MallURL+"?"+idsStr, err)
  68. err = ecode.Int(res.Code)
  69. return
  70. }
  71. resp = make(map[int64]*model.MallCard)
  72. for _, l := range res.Data.List {
  73. resp[l.ID] = l
  74. }
  75. return
  76. }
  77. // AudioCard .
  78. func (d *Dao) AudioCard(c context.Context, ids []int64) (resp map[int64]*model.AudioCard, err error) {
  79. params := url.Values{}
  80. params.Set("ids", xstr.JoinInts(ids))
  81. params.Set("level", "1")
  82. var res struct {
  83. Code int `json:"code"`
  84. Data map[int64]*model.AudioCard `json:"data"`
  85. }
  86. err = d.httpClient.Get(c, d.c.Cards.AudioURL, "", params, &res)
  87. if err != nil {
  88. PromError("cards:audio接口")
  89. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.AudioURL+"?"+params.Encode(), err)
  90. return
  91. }
  92. if res.Code != 0 {
  93. PromError("cards:audio接口")
  94. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.AudioURL+"?"+params.Encode(), err)
  95. err = ecode.Int(res.Code)
  96. return
  97. }
  98. resp = res.Data
  99. return
  100. }
  101. // BangumiCard .
  102. func (d *Dao) BangumiCard(c context.Context, seasonIDs []int64, episodeIDs []int64) (resp map[int64]*model.BangumiCard, err error) {
  103. params := url.Values{}
  104. params.Set("season_ids", xstr.JoinInts(seasonIDs))
  105. params.Set("episode_ids", xstr.JoinInts(episodeIDs))
  106. var res struct {
  107. Code int `json:"code"`
  108. Data struct {
  109. SeasonMap map[int64]*model.BangumiCard `json:"season_map"`
  110. EpisodeMap map[int64]*model.BangumiCard `json:"episode_map"`
  111. } `json:"result"`
  112. }
  113. err = d.httpClient.Post(c, d.c.Cards.BangumiURL, "", params, &res)
  114. if err != nil {
  115. PromError("cards:bangumi接口")
  116. log.Error("cards: d.client.Get(%s) error(%+v)", d.c.Cards.BangumiURL+"?"+params.Encode(), err)
  117. return
  118. }
  119. if res.Code != 0 {
  120. PromError("cards:bangumi接口")
  121. log.Error("cards: url(%s) res code(%d)", d.c.Cards.BangumiURL+"?"+params.Encode(), res.Code)
  122. err = ecode.Int(res.Code)
  123. return
  124. }
  125. resp = make(map[int64]*model.BangumiCard)
  126. for id, item := range res.Data.EpisodeMap {
  127. resp[id] = item
  128. }
  129. for id, item := range res.Data.SeasonMap {
  130. resp[id] = item
  131. }
  132. return
  133. }