activity.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package activity
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/interface/main/app-channel/conf"
  6. "go-common/app/interface/main/app-channel/model/activity"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/metadata"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _activitys = "/activity/pages"
  15. )
  16. // Dao is activity dao.
  17. type Dao struct {
  18. // http client
  19. client *bm.Client
  20. // activitys
  21. activitys string
  22. }
  23. // New new a activity dao.
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. // http client
  27. client: bm.NewClient(c.HTTPClient),
  28. activitys: c.Host.Activity + _activitys,
  29. }
  30. return d
  31. }
  32. // Activitys activity or tpoci
  33. func (d *Dao) Activitys(c context.Context, ids []int64) (actm map[int64]*activity.Activity, err error) {
  34. ip := metadata.String(c, metadata.RemoteIP)
  35. params := url.Values{}
  36. params.Set("pids", xstr.JoinInts(ids))
  37. params.Set("http", "2")
  38. params.Set("platform", "pegasus")
  39. var res struct {
  40. Code int `json:"code"`
  41. Data struct {
  42. List []*activity.Activity `json:"list"`
  43. } `json:"data"`
  44. }
  45. if err = d.client.Get(c, d.activitys, ip, params, &res); err != nil {
  46. return
  47. }
  48. if res.Code != ecode.OK.Code() {
  49. err = errors.Wrap(ecode.Int(res.Code), d.activitys+"?"+params.Encode())
  50. return
  51. }
  52. actm = make(map[int64]*activity.Activity, len(res.Data.List))
  53. for _, act := range res.Data.List {
  54. actm[act.ID] = act
  55. }
  56. return
  57. }