dao.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package ad
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/app-card/model/card/cm"
  8. "go-common/app/interface/main/app-feed/conf"
  9. "go-common/library/ecode"
  10. httpx "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _bce = "/bce/api/bce/wise"
  17. )
  18. // Dao is ad dao.
  19. type Dao struct {
  20. // http client
  21. client *httpx.Client
  22. // ad
  23. bce string
  24. }
  25. // New new a ad dao.
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. // http client
  29. client: httpx.NewClient(c.HTTPAd),
  30. bce: c.Host.Ad + _bce,
  31. }
  32. return
  33. }
  34. func (d *Dao) Ad(c context.Context, mid int64, build int, buvid string, resource []int64, country, province, city, network, mobiApp, device, openEvent, adExtra string, style int, now time.Time) (advert *cm.Ad, err error) {
  35. ip := metadata.String(c, metadata.RemoteIP)
  36. params := url.Values{}
  37. params.Set("mid", strconv.FormatInt(mid, 10))
  38. params.Set("buvid", buvid)
  39. params.Set("resource", xstr.JoinInts(resource))
  40. params.Set("ip", ip)
  41. params.Set("country", country)
  42. params.Set("province", province)
  43. params.Set("city", city)
  44. params.Set("network", network)
  45. params.Set("build", strconv.Itoa(build))
  46. params.Set("mobi_app", mobiApp)
  47. params.Set("device", device)
  48. params.Set("open_event", openEvent)
  49. params.Set("ad_extra", adExtra)
  50. // 老接口做兼容
  51. if style != 0 {
  52. if style != 2 {
  53. style = 1
  54. }
  55. params.Set("style", strconv.Itoa(style))
  56. }
  57. var res struct {
  58. Code int `json:"code"`
  59. Data *cm.Ad `json:"data"`
  60. }
  61. if err = d.client.Get(c, d.bce, ip, params, &res); err != nil {
  62. return
  63. }
  64. if res.Code != ecode.OK.Code() {
  65. err = errors.Wrap(ecode.Int(res.Code), d.bce+"?"+params.Encode())
  66. return
  67. }
  68. if res.Data != nil {
  69. res.Data.ClientIP = ip
  70. }
  71. advert = res.Data
  72. return
  73. }