media.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dao
  2. import (
  3. "context"
  4. "errors"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _mediaURL = "http://api.bilibili.co/pgc/internal/review/score/card"
  13. _setScoreURL = "http://api.bilibili.co/pgc/internal/review/score/rating"
  14. _delScoreURL = "http://api.bilibili.co/pgc/internal/review/score/delete"
  15. )
  16. // Media get media score.
  17. func (d *Dao) Media(c context.Context, mediaID, mid int64) (res *model.MediaResult, err error) {
  18. if mediaID == 0 {
  19. return
  20. }
  21. params := url.Values{}
  22. params.Set("media_id", strconv.FormatInt(mediaID, 10))
  23. params.Set("mid", strconv.FormatInt(mid, 10))
  24. resp := &model.MediaResp{}
  25. err = d.httpClient.Get(c, _mediaURL, "", params, &resp)
  26. if err != nil {
  27. PromError("media:番剧信息接口")
  28. log.Error("media: d.client.Get(%s) error(%+v)", _mediaURL+"?"+params.Encode(), err)
  29. return
  30. }
  31. if resp.Code != 0 {
  32. PromError("media:番剧信息接口")
  33. log.Error("media: url(%s) res: %+v", _mediaURL+"?"+params.Encode(), resp)
  34. err = ecode.Int(resp.Code)
  35. return
  36. }
  37. res = resp.Result
  38. return
  39. }
  40. // SetScore set media score.
  41. func (d *Dao) SetScore(c context.Context, score, aid, mediaID, mid int64) (err error) {
  42. if score < 1 || score > 10 || score%2 != 0 {
  43. err = errors.New("评分分值错误")
  44. return
  45. }
  46. params := url.Values{}
  47. params.Set("media_id", strconv.FormatInt(mediaID, 10))
  48. params.Set("mid", strconv.FormatInt(mid, 10))
  49. params.Set("oid", strconv.FormatInt(aid, 10))
  50. params.Set("score", strconv.FormatInt(score, 10))
  51. params.Set("from", "1")
  52. var resp struct {
  53. Code int `json:"code"`
  54. Message string `json:"message"`
  55. }
  56. err = d.httpClient.Post(c, _setScoreURL, "", params, &resp)
  57. if err != nil {
  58. PromError("media:番剧评分接口")
  59. log.Error("media: d.client.Post(%s, data:%s) error(%+v)", _setScoreURL, params.Encode(), err)
  60. return
  61. }
  62. if resp.Code != 0 {
  63. PromError("media:番剧评分接口")
  64. log.Error("media: d.client.Post(%s, data:%s) res: %+v", _setScoreURL, params.Encode(), resp)
  65. err = ecode.Int(resp.Code)
  66. return
  67. }
  68. log.Info("media: set score success(media_id: %d, mid: %d, oid: %d, score: %d)", mediaID, mid, aid, score)
  69. return
  70. }
  71. // DelScore get media score.
  72. func (d *Dao) DelScore(c context.Context, aid, mediaID, mid int64) (err error) {
  73. if mediaID == 0 || mid == 0 || aid == 0 {
  74. return
  75. }
  76. params := url.Values{}
  77. params.Set("media_id", strconv.FormatInt(mediaID, 10))
  78. params.Set("mid", strconv.FormatInt(mid, 10))
  79. params.Set("oid", strconv.FormatInt(aid, 10))
  80. params.Set("from", "1")
  81. var resp struct {
  82. Code int `json:"code"`
  83. Message string `json:"message"`
  84. }
  85. err = d.httpClient.Post(c, _delScoreURL, "", params, &resp)
  86. if err != nil {
  87. PromError("media:番剧删除评分接口")
  88. log.Error("media: d.client.Post(%s, data:%s) error(%+v)", _delScoreURL, params.Encode(), err)
  89. return
  90. }
  91. if resp.Code != 0 {
  92. PromError("media:番剧删除评分接口")
  93. log.Error("media: d.client.Post(%s, data:%s) res: %+v", _delScoreURL, params.Encode(), resp)
  94. err = ecode.Int(resp.Code)
  95. }
  96. log.Info("media: del score success(media_id: %d, mid: %d, oid: %d)", mediaID, mid, aid)
  97. return
  98. }