dao.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package topic
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/favorite/conf"
  7. "go-common/app/interface/main/favorite/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. )
  14. const _topic = "http://matsuri.bilibili.co/activity/pages"
  15. // Dao defeine fav Dao
  16. type Dao struct {
  17. httpClient *httpx.Client
  18. }
  19. // New return fav dao
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. httpClient: httpx.NewClient(c.HTTPClient),
  23. }
  24. return
  25. }
  26. // TopicMap return the user favorited topic's map data(all state).
  27. func (d *Dao) TopicMap(c context.Context, tpIDs []int64, isNomal bool, appInfo *model.AppInfo) (data map[int64]*model.Topic, err error) {
  28. params := url.Values{}
  29. params.Set("mold", "1")
  30. if !isNomal {
  31. params.Set("all", "isOne")
  32. }
  33. params.Set("pids", xstr.JoinInts(tpIDs))
  34. if appInfo != nil {
  35. params.Set("http", strconv.Itoa(model.HttpMode4Https))
  36. } else {
  37. params.Set("http", strconv.Itoa(model.HttpMode4Both))
  38. }
  39. res := new(model.TopicsResult)
  40. ip := metadata.String(c, metadata.RemoteIP)
  41. if err = d.httpClient.Get(c, _topic, ip, params, res); err != nil {
  42. log.Error("d.HTTPClient.Get(%s?%s) error(%v)", _topic, params.Encode())
  43. return
  44. }
  45. if res.Code != ecode.OK.Code() {
  46. log.Error("d.HTTPClient.Get(%s?%s) code:%d msg:%s", _topic, params.Encode(), res.Code)
  47. err = model.ErrTopicRequest
  48. return
  49. }
  50. data = make(map[int64]*model.Topic, len(res.Data.List))
  51. for _, r := range res.Data.List {
  52. data[r.ID] = r
  53. }
  54. return
  55. }