dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package history
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/library/ecode"
  9. "github.com/pkg/errors"
  10. "go-common/app/interface/live/web-ucenter/conf"
  11. "go-common/app/interface/live/web-ucenter/model"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/metadata"
  15. )
  16. // Dao dao
  17. type Dao struct {
  18. c *conf.Config
  19. client *bm.Client
  20. }
  21. const (
  22. _historyResourceURI = "/x/internal/v2/history/resource"
  23. _historyDeleteURI = "/x/internal/v2/history/clear"
  24. _historyPageSize = 24
  25. )
  26. // New init
  27. func New(c *conf.Config) (dao *Dao) {
  28. dao = &Dao{
  29. client: bm.NewClient(c.HTTPClient),
  30. }
  31. return
  32. }
  33. // GetMainHistory 获取直播历史记录
  34. func (d *Dao) GetMainHistory(c context.Context, mid int32) (data []*model.HistoryData, err error) {
  35. var (
  36. params = url.Values{}
  37. ip = metadata.String(c, metadata.RemoteIP)
  38. )
  39. params.Set("mid", fmt.Sprint(mid))
  40. params.Set("business", "live")
  41. params.Set("pn", fmt.Sprint(1))
  42. params.Set("ps", fmt.Sprint(_historyPageSize))
  43. params.Set("appkey", conf.APPKey)
  44. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  45. var res struct {
  46. Code int `json:"code"`
  47. Msg string `json:"msg"`
  48. Data []*model.HistoryData `json:"data"`
  49. }
  50. if d.client.Get(c, conf.MainInnerHostHTTP+_historyResourceURI, ip, params, &res); err != nil {
  51. err = errors.WithMessage(err, "调用主站获取观看历史错误")
  52. log.Error("call_history_resource_error:httpCode=%d params=%s", res.Code, params)
  53. return
  54. }
  55. if int(res.Code) != ecode.OK.Code() {
  56. log.Error("call_history_resource_error:%d", res.Code)
  57. }
  58. log.Info("call_history_resource_info:param:%v,%v", mid, res.Data)
  59. data = res.Data
  60. return
  61. }
  62. // DelHistory 删除直播历史记录
  63. func (d *Dao) DelHistory(c context.Context, mid int64) (data int32, err error) {
  64. var (
  65. params = url.Values{}
  66. ip = metadata.String(c, metadata.RemoteIP)
  67. )
  68. params.Set("appkey", conf.APPKey)
  69. params.Set("mid", fmt.Sprint(mid))
  70. params.Set("business", "live")
  71. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  72. var res struct {
  73. Code int32 `json:"code"`
  74. }
  75. if d.client.Post(c, conf.MainInnerHostHTTP+_historyDeleteURI, ip, params, &res); err != nil {
  76. err = errors.WithMessage(err, "调用主站删除观看历史错误")
  77. log.Error("call_history_delete_error:%s", err)
  78. return
  79. }
  80. if int(res.Code) != ecode.OK.Code() {
  81. log.Error("call_history_delete_code_error:%d", res.Code)
  82. }
  83. data = res.Code
  84. return
  85. }