shopping.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package shopping
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/interface/main/app-card/model/card/show"
  6. "go-common/app/interface/main/app-channel/conf"
  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. _getCard = "/api/ticket/project/getcard"
  15. )
  16. // Dao is shopping dao.
  17. type Dao struct {
  18. // http client
  19. client *bm.Client
  20. // live
  21. getCard string
  22. }
  23. // New new a shopping dao.
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. // http client
  27. client: bm.NewClient(c.HTTPShopping),
  28. getCard: c.Host.Shopping + _getCard,
  29. }
  30. return d
  31. }
  32. func (d *Dao) Card(c context.Context, ids []int64) (rs map[int64]*show.Shopping, err error) {
  33. ip := metadata.String(c, metadata.RemoteIP)
  34. params := url.Values{}
  35. params.Set("id", xstr.JoinInts(ids))
  36. params.Set("for", "1")
  37. params.Set("price", "1")
  38. var res struct {
  39. Code int `json:"errno"`
  40. Data []*show.Shopping `json:"data"`
  41. }
  42. if err = d.client.Get(c, d.getCard, ip, params, &res); err != nil {
  43. return
  44. }
  45. if res.Code != ecode.OK.Code() {
  46. err = errors.Wrap(err, d.getCard+"?"+params.Encode())
  47. return
  48. }
  49. rs = make(map[int64]*show.Shopping, len(res.Data))
  50. for _, r := range res.Data {
  51. rs[r.ID] = r
  52. }
  53. return
  54. }