ad.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package ad
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-show/conf"
  8. "go-common/app/interface/main/app-show/model/ad"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. )
  12. const (
  13. _adURL = "/bce/api/bce/wise"
  14. )
  15. // Dao is advertising dao.
  16. type Dao struct {
  17. client *httpx.Client
  18. adURL string
  19. }
  20. // New advertising dao.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. client: httpx.NewClient(conf.Conf.HTTPClient),
  24. adURL: c.Host.Ad + _adURL,
  25. }
  26. return
  27. }
  28. // ADRequest Banners
  29. func (d *Dao) ADRequest(c context.Context, mid int64, build int, buvid, resource, ip, country, province, city, network, mobiApp, device string, isPost bool) (adr *ad.ADRequest, err error) {
  30. params := url.Values{}
  31. params.Set("mid", strconv.FormatInt(mid, 10))
  32. params.Set("build", strconv.Itoa(build))
  33. params.Set("buvid", buvid)
  34. params.Set("resource", resource)
  35. params.Set("mobi_app", mobiApp)
  36. params.Set("ip", ip)
  37. if device != "" {
  38. params.Set("device", device)
  39. }
  40. if country != "" {
  41. params.Set("country", country)
  42. }
  43. if province != "" {
  44. params.Set("province", province)
  45. }
  46. if city != "" {
  47. params.Set("city", city)
  48. }
  49. if network != "" {
  50. params.Set("network", network)
  51. }
  52. var res struct {
  53. Code int `json:"code"`
  54. Data *ad.ADRequest `json:"data"`
  55. }
  56. if isPost {
  57. if err = d.client.Post(c, d.adURL, ip, params, &res); err != nil {
  58. log.Error("ad url(%s) error(%v)", d.adURL+"?"+params.Encode(), err)
  59. return
  60. }
  61. } else {
  62. if err = d.client.Get(c, d.adURL, ip, params, &res); err != nil {
  63. log.Error("ad url(%s) error(%v)", d.adURL+"?"+params.Encode(), err)
  64. return
  65. }
  66. }
  67. if res.Code != 0 {
  68. err = fmt.Errorf("ad api failed(%d)", res.Code)
  69. log.Error("url(%s) res code(%d) or res.data(%v)", d.adURL+"?"+params.Encode(), res.Code, res.Data)
  70. return
  71. }
  72. adr = res.Data
  73. return
  74. }