dao.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package ad
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-view/conf"
  8. "go-common/app/interface/main/app-view/model/ad"
  9. "go-common/library/ecode"
  10. bm "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. _adURL = "/bce/api/bce/wise"
  17. _monitorInfoURL = "/up-openapi/api/v1/av_monitor_info/%d"
  18. )
  19. // Dao dao.
  20. type Dao struct {
  21. client *bm.Client
  22. adURL string
  23. monitorInfoURL string
  24. }
  25. // New account dao.
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. client: bm.NewClient(conf.Conf.HTTPAD),
  29. adURL: c.Host.AD + _adURL,
  30. monitorInfoURL: c.Host.AD + _monitorInfoURL,
  31. }
  32. return
  33. }
  34. // Ad ad request.
  35. func (d *Dao) Ad(c context.Context, mobiApp, device, buvid string, build int, mid, upperID, aid int64, rid int32, tids []int64, resource []int64, network, adExtra string) (advert *ad.Ad, err error) {
  36. ip := metadata.String(c, metadata.RemoteIP)
  37. params := url.Values{}
  38. params.Set("mid", strconv.FormatInt(mid, 10))
  39. params.Set("aid", strconv.FormatInt(aid, 10))
  40. params.Set("build", strconv.Itoa(build))
  41. params.Set("device", device)
  42. params.Set("buvid", buvid)
  43. params.Set("resource", xstr.JoinInts(resource))
  44. params.Set("mobi_app", mobiApp)
  45. params.Set("ip", ip)
  46. params.Set("av_rid", strconv.FormatInt(int64(rid), 10))
  47. params.Set("av_tid", xstr.JoinInts(tids))
  48. params.Set("av_up_id", strconv.FormatInt(upperID, 10))
  49. if network != "" {
  50. params.Set("network", network)
  51. }
  52. if adExtra != "" {
  53. params.Set("ad_extra", adExtra)
  54. }
  55. var res struct {
  56. Code int `json:"code"`
  57. Data *ad.Ad `json:"data"`
  58. }
  59. if err = d.client.Get(c, d.adURL, ip, params, &res); err != nil {
  60. return
  61. }
  62. code := ecode.Int(res.Code)
  63. if !code.Equal(ecode.OK) {
  64. err = errors.Wrap(code, d.adURL+"?"+params.Encode())
  65. return
  66. }
  67. if res.Data != nil {
  68. res.Data.ClientIP = ip
  69. }
  70. advert = res.Data
  71. return
  72. }
  73. // MonitorInfo ad aid monitor info
  74. func (d *Dao) MonitorInfo(c context.Context, aid int64) (minfo json.RawMessage, err error) {
  75. ip := metadata.String(c, metadata.RemoteIP)
  76. var res struct {
  77. Code int `json:"code"`
  78. Data json.RawMessage `json:"data"`
  79. }
  80. if err = d.client.RESTfulGet(c, d.monitorInfoURL, ip, nil, &res, aid); err != nil {
  81. return
  82. }
  83. code := ecode.Int(res.Code)
  84. if !code.Equal(ecode.OK) {
  85. err = errors.Wrap(code, d.monitorInfoURL)
  86. return
  87. }
  88. minfo = res.Data
  89. return
  90. }