ad.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package ad
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-resource/conf"
  8. "go-common/app/interface/main/app-resource/model/splash"
  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. )
  14. const (
  15. _splashListURL = "/bce/api/splash/list"
  16. _splashShowURL = "/bce/api/splash/show"
  17. )
  18. // Dao is advertising dao.
  19. type Dao struct {
  20. client *httpx.Client
  21. splashListURL string
  22. splashShowURL string
  23. }
  24. // New advertising dao.
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(conf.Conf.HTTPClient),
  28. splashListURL: c.Host.Ad + _splashListURL,
  29. splashShowURL: c.Host.Ad + _splashShowURL,
  30. }
  31. return
  32. }
  33. // SplashList ad splash list
  34. func (d *Dao) SplashList(c context.Context, mobiApp, device, buvid, birth, adExtra string, height, width, build int, mid int64) (res []*splash.List, config *splash.CmConfig, err error) {
  35. ip := metadata.String(c, metadata.RemoteIP)
  36. params := url.Values{}
  37. params.Set("build", strconv.Itoa(build))
  38. params.Set("buvid", buvid)
  39. params.Set("mobi_app", mobiApp)
  40. params.Set("device", device)
  41. params.Set("ip", ip)
  42. params.Set("height", strconv.Itoa(height))
  43. params.Set("width", strconv.Itoa(width))
  44. params.Set("mid", strconv.FormatInt(mid, 10))
  45. if birth != "" {
  46. params.Set("birth", birth)
  47. }
  48. if adExtra != "" {
  49. params.Set("ad_extra", adExtra)
  50. }
  51. var data struct {
  52. Code int `json:"code"`
  53. *splash.CmConfig
  54. RequestID string `json:"request_id"`
  55. Data []*splash.List `json:"data"`
  56. }
  57. if err = d.client.Get(c, d.splashListURL, ip, params, &data); err != nil {
  58. log.Error("cpm splash url(%s) error(%v)", d.splashListURL+"?"+params.Encode(), err)
  59. return
  60. }
  61. b, _ := json.Marshal(&data)
  62. log.Info("cpm splash list url(%s) response(%s)", d.splashListURL+"?"+params.Encode(), b)
  63. if data.Code != 0 {
  64. err = ecode.Int(data.Code)
  65. log.Error("cpm splash url(%s) code(%d)", d.splashListURL+"?"+params.Encode(), data.Code)
  66. return
  67. }
  68. for _, t := range data.Data {
  69. s := &splash.List{}
  70. *s = *t
  71. s.RequestID = data.RequestID
  72. s.ClientIP = ip
  73. s.IsAdLoc = true
  74. res = append(res, s)
  75. }
  76. config = data.CmConfig
  77. return
  78. }
  79. // SplashShow ad splash show
  80. func (d *Dao) SplashShow(c context.Context, mobiApp, device, buvid, birth, adExtra string, height, width, build int, mid int64) (res []*splash.Show, err error) {
  81. ip := metadata.String(c, metadata.RemoteIP)
  82. params := url.Values{}
  83. params.Set("build", strconv.Itoa(build))
  84. params.Set("buvid", buvid)
  85. params.Set("mobi_app", mobiApp)
  86. params.Set("device", device)
  87. params.Set("ip", ip)
  88. params.Set("height", strconv.Itoa(height))
  89. params.Set("width", strconv.Itoa(width))
  90. params.Set("mid", strconv.FormatInt(mid, 10))
  91. if birth != "" {
  92. params.Set("birth", birth)
  93. }
  94. if adExtra != "" {
  95. params.Set("ad_extra", adExtra)
  96. }
  97. var data struct {
  98. Code int `json:"code"`
  99. Data []*splash.Show `json:"data"`
  100. }
  101. if err = d.client.Get(c, d.splashShowURL, ip, params, &data); err != nil {
  102. log.Error("cpm splash url(%s) error(%v)", d.splashShowURL+"?"+params.Encode(), err)
  103. return
  104. }
  105. b, _ := json.Marshal(&data)
  106. log.Info("cpm splash show url(%s) response(%s)", d.splashShowURL+"?"+params.Encode(), b)
  107. if data.Code != 0 {
  108. err = ecode.Int(data.Code)
  109. log.Error("cpm splash url(%s) code(%d)", d.splashShowURL+"?"+params.Encode(), data.Code)
  110. return
  111. }
  112. res = data.Data
  113. return
  114. }