dao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package topic
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-interface/conf"
  8. "go-common/app/interface/main/app-interface/model/topic"
  9. fav "go-common/app/service/main/favorite/api"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. httpx "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. "github.com/pkg/errors"
  15. )
  16. const _topic = "/x/internal/v2/fav/topic"
  17. // Dao is topic dao
  18. type Dao struct {
  19. client *httpx.Client
  20. topic string
  21. favRPC fav.FavoriteClient
  22. }
  23. // New initial topic dao
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. client: httpx.NewClient(c.HTTPClient),
  27. topic: c.Host.APICo + _topic,
  28. }
  29. var err error
  30. d.favRPC, err = fav.New(c.FavClient)
  31. if err != nil {
  32. panic(fmt.Sprintf("fav NewClient error(%v)", err))
  33. }
  34. return
  35. }
  36. // Topic get topic list from UGC api.
  37. func (d *Dao) Topic(c context.Context, accessKey, actionKey, device, mobiApp, platform string, build, ps, pn int, mid int64) (t *topic.Topic, err error) {
  38. ip := metadata.String(c, metadata.RemoteIP)
  39. params := url.Values{}
  40. params.Set("access_key", accessKey)
  41. params.Set("mid", strconv.FormatInt(mid, 10))
  42. params.Set("actionKey", actionKey)
  43. params.Set("build", strconv.Itoa(build))
  44. params.Set("device", device)
  45. params.Set("mobi_app", mobiApp)
  46. params.Set("platform", platform)
  47. params.Set("ps", strconv.Itoa(ps))
  48. params.Set("pn", strconv.Itoa(pn))
  49. params.Set("mid", strconv.FormatInt(mid, 10))
  50. var res struct {
  51. Code int `json:"code"`
  52. Data *topic.Topic `json:"data"`
  53. }
  54. if err = d.client.Get(c, d.topic, ip, params, &res); err != nil {
  55. return
  56. }
  57. if res.Code != ecode.OK.Code() {
  58. err = errors.Wrap(ecode.Int(res.Code), d.topic+"?"+params.Encode())
  59. return
  60. }
  61. t = res.Data
  62. return
  63. }
  64. // UserFolder is
  65. func (d *Dao) UserFolder(c context.Context, mid int64, typ int32) (userFolder *fav.UserFolderReply, err error) {
  66. if userFolder, err = d.favRPC.UserFolder(c, &fav.UserFolderReq{Typ: typ, Mid: mid}); err != nil {
  67. log.Error("d.favRPC.UserFolder error(%+v)", err)
  68. return
  69. }
  70. return
  71. }