dao.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package creative
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-view/conf"
  7. "go-common/app/interface/main/app-view/model/creative"
  8. "go-common/library/ecode"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _special = "/x/internal/uper/special"
  15. _follow = "/x/internal/uper/switch"
  16. _bgm = "/x/internal/creative/archive/bgm"
  17. _points = "/x/internal/creative/video/viewpoints"
  18. )
  19. // Dao is space dao
  20. type Dao struct {
  21. client *httpx.Client
  22. special string
  23. follow string
  24. bgm string
  25. points string
  26. }
  27. // New initial space dao
  28. func New(c *conf.Config) (d *Dao) {
  29. d = &Dao{
  30. client: httpx.NewClient(c.HTTPClient),
  31. special: c.Host.APICo + _special,
  32. follow: c.Host.APICo + _follow,
  33. bgm: c.Host.APICo + _bgm,
  34. points: c.Host.APICo + _points,
  35. }
  36. return
  37. }
  38. // FollowSwitch get auto follow switch .
  39. func (d *Dao) FollowSwitch(c context.Context, vmid int64) (s *creative.FollowSwitch, err error) {
  40. params := url.Values{}
  41. params.Set("mid", strconv.FormatInt(vmid, 10))
  42. params.Set("from", "0")
  43. var res struct {
  44. Code int `json:"code"`
  45. Data *creative.FollowSwitch `json:"data"`
  46. }
  47. ip := metadata.String(c, metadata.RemoteIP)
  48. if err = d.client.Get(c, d.follow, ip, params, &res); err != nil {
  49. return
  50. }
  51. if res.Code != ecode.OK.Code() {
  52. err = errors.Wrap(ecode.Int(res.Code), d.follow+"?"+params.Encode())
  53. return
  54. }
  55. s = res.Data
  56. return
  57. }
  58. // Bgm get archive bgm
  59. func (d *Dao) Bgm(c context.Context, aid, cid int64) (bgm []*creative.Bgm, err error) {
  60. params := url.Values{}
  61. params.Set("aid", strconv.FormatInt(aid, 10))
  62. params.Set("cid", strconv.FormatInt(cid, 10))
  63. var res struct {
  64. Code int `json:"code"`
  65. Data []*creative.Bgm `json:"data"`
  66. }
  67. ip := metadata.String(c, metadata.RemoteIP)
  68. if err = d.client.Get(c, d.bgm, ip, params, &res); err != nil {
  69. return
  70. }
  71. if res.Code != ecode.OK.Code() {
  72. err = errors.Wrap(ecode.Int(res.Code), d.bgm+"?"+params.Encode())
  73. return
  74. }
  75. bgm = res.Data
  76. return
  77. }
  78. // Points get video points
  79. func (d *Dao) Points(c context.Context, aid, cid int64) (points []*creative.Points, err error) {
  80. params := url.Values{}
  81. params.Set("aid", strconv.FormatInt(aid, 10))
  82. params.Set("cid", strconv.FormatInt(cid, 10))
  83. var res struct {
  84. Code int `json:"code"`
  85. Data struct {
  86. Points []*creative.Points `json:"points"`
  87. } `json:"data"`
  88. }
  89. ip := metadata.String(c, metadata.RemoteIP)
  90. if err = d.client.Get(c, d.points, ip, params, &res); err != nil {
  91. return
  92. }
  93. if res.Code != ecode.OK.Code() {
  94. err = errors.Wrap(ecode.Int(res.Code), d.points+"?"+params.Encode())
  95. return
  96. }
  97. points = res.Data.Points
  98. return
  99. }
  100. // Special is
  101. func (d *Dao) Special(c context.Context) (midsM map[int64]struct{}, err error) {
  102. params := url.Values{}
  103. params.Set("group_id", "20")
  104. var res struct {
  105. Code int `json:"code"`
  106. Data []struct {
  107. Mid int64 `json:"mid"`
  108. } `json:"data"`
  109. }
  110. if err = d.client.Get(c, d.special, "", params, &res); err != nil {
  111. return
  112. }
  113. if res.Code != ecode.OK.Code() {
  114. err = errors.Wrap(ecode.Int(res.Code), d.special+"?"+params.Encode())
  115. return
  116. }
  117. midsM = make(map[int64]struct{})
  118. for _, l := range res.Data {
  119. midsM[l.Mid] = struct{}{}
  120. }
  121. return
  122. }