dao.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package favorite
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-interface/conf"
  8. "go-common/app/interface/main/app-interface/model/favorite"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/net/metadata"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _folder = "/x/internal/v2/fav/folder"
  17. _folderVideo = "/x/internal/v2/fav/video"
  18. )
  19. // Dao is favorite dao
  20. type Dao struct {
  21. client *httpx.Client
  22. favor string
  23. favorVideo string
  24. }
  25. // New initial favorite dao
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. client: httpx.NewClient(c.HTTPClient),
  29. favor: c.Host.APICo + _folder,
  30. favorVideo: c.Host.APICo + _folderVideo,
  31. }
  32. return
  33. }
  34. // Folders get favorite floders from api.
  35. func (d *Dao) Folders(c context.Context, mid, vmid int64, mobiApp string, build int, mediaList bool) (fs []*favorite.Folder, err error) {
  36. ip := metadata.String(c, metadata.RemoteIP)
  37. params := url.Values{}
  38. params.Set("mid", strconv.FormatInt(mid, 10))
  39. params.Set("vmid", strconv.FormatInt(vmid, 10))
  40. params.Set("mobi_app", mobiApp)
  41. // params.Set("build", strconv.Itoa(build))
  42. if mediaList {
  43. params.Set("medialist", "1")
  44. }
  45. var res struct {
  46. Code int `json:"code"`
  47. Data []*favorite.Folder `json:"data"`
  48. }
  49. if err = d.client.Get(c, d.favor, ip, params, &res); err != nil {
  50. return
  51. }
  52. b, _ := json.Marshal(&res)
  53. log.Info("Folders url(%s) response(%s)", d.favor+"?"+params.Encode(), b)
  54. if res.Code != ecode.OK.Code() {
  55. err = errors.Wrap(ecode.Int(res.Code), d.favor+"?"+params.Encode())
  56. return
  57. }
  58. fs = res.Data
  59. return
  60. }
  61. // FolderVideo get favorite floders from UGC api.
  62. func (d *Dao) FolderVideo(c context.Context, accessKey, actionKey, device, mobiApp, platform, keyword, order string, build, tid, pn, ps int, mid, fid, vmid int64) (fav *favorite.Video, err error) {
  63. ip := metadata.String(c, metadata.RemoteIP)
  64. params := url.Values{}
  65. params.Set("access_key", accessKey)
  66. params.Set("actionKey", actionKey)
  67. params.Set("build", strconv.Itoa(build))
  68. params.Set("device", device)
  69. params.Set("mid", strconv.FormatInt(mid, 10))
  70. params.Set("fid", strconv.FormatInt(fid, 10))
  71. params.Set("tid", strconv.Itoa(tid))
  72. params.Set("keyword", keyword)
  73. params.Set("order", order)
  74. params.Set("pn", strconv.Itoa(pn))
  75. params.Set("ps", strconv.Itoa(ps))
  76. params.Set("mobi_app", mobiApp)
  77. params.Set("platform", platform)
  78. params.Set("vmid", strconv.FormatInt(vmid, 10))
  79. var res struct {
  80. Code int `json:"code"`
  81. Data *favorite.Video `json:"data"`
  82. }
  83. if err = d.client.Get(c, d.favorVideo, ip, params, &res); err != nil {
  84. return
  85. }
  86. if res.Code != ecode.OK.Code() {
  87. err = errors.Wrap(ecode.Int(res.Code), d.favorVideo+"?"+params.Encode())
  88. return
  89. }
  90. fav = res.Data
  91. return
  92. }