dao.go 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package ticket
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-interface/conf"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/metadata"
  10. "github.com/pkg/errors"
  11. )
  12. const _favCount = "/api/ticket/user/favcountinner"
  13. type Dao struct {
  14. client *bm.Client
  15. favCount string
  16. }
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. client: bm.NewClient(c.HTTPClient),
  20. favCount: c.Host.Show + _favCount,
  21. }
  22. return
  23. }
  24. func (d *Dao) FavCount(c context.Context, mid int64) (count int32, err error) {
  25. ip := metadata.String(c, metadata.RemoteIP)
  26. params := url.Values{}
  27. params.Set("mid", strconv.FormatInt(mid, 10))
  28. var res struct {
  29. Code int `json:"errno"`
  30. Data int32 `json:"data"`
  31. }
  32. if err = d.client.Get(c, d.favCount, ip, params, &res); err != nil {
  33. return
  34. }
  35. if res.Code != ecode.OK.Code() {
  36. err = errors.Wrap(ecode.Int(res.Code), d.favCount+"?"+params.Encode())
  37. return
  38. }
  39. count = res.Data
  40. return
  41. }