cpm.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package cpm
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. locmdl "go-common/app/service/main/location/model"
  8. "go-common/app/service/main/resource/model"
  9. "go-common/library/log"
  10. )
  11. // CpmsAPP get ads from cpm platform.
  12. func (d *Dao) CpmsAPP(c context.Context, aid, mid int64, build int, resource, mobiApp, device, buvid, network, openEvent, adExtra string, ip *locmdl.Info) (adr *model.ADRequest, err error) {
  13. params := url.Values{}
  14. params.Set("mid", strconv.FormatInt(mid, 10))
  15. params.Set("build", strconv.Itoa(build))
  16. params.Set("buvid", buvid)
  17. params.Set("resource", resource)
  18. params.Set("mobi_app", mobiApp)
  19. params.Set("ip", ip.Addr)
  20. if aid != 0 {
  21. params.Set("aid", strconv.FormatInt(aid, 10))
  22. }
  23. if device != "" {
  24. params.Set("device", device)
  25. }
  26. if ip.Country != "" {
  27. params.Set("country", ip.Country)
  28. }
  29. if ip.Province != "" {
  30. params.Set("province", ip.Province)
  31. }
  32. if ip.City != "" {
  33. params.Set("city", ip.City)
  34. }
  35. if network != "" {
  36. params.Set("network", network)
  37. }
  38. if openEvent != "" {
  39. params.Set("open_event", openEvent)
  40. }
  41. if adExtra != "" {
  42. params.Set("ad_extra", adExtra)
  43. }
  44. var res struct {
  45. Code int `json:"code"`
  46. Data *model.ADRequest `json:"data"`
  47. Msg string `json:"message"`
  48. }
  49. if err = d.httpClient.Get(c, d.cpmAppURL, ip.Addr, params, &res); err != nil {
  50. log.Error("CpmsAPP url(%s) error(%v)", d.cpmAppURL+"?"+params.Encode(), err)
  51. return
  52. }
  53. if res.Code != 0 {
  54. err = fmt.Errorf("CpmsAPP api failed(%d)", res.Code)
  55. log.Error("CpmsApp url(%s) res code(%d) or res.data(%v)", d.cpmAppURL+"?"+params.Encode(), res.Code, res.Data)
  56. return
  57. }
  58. adr = res.Data
  59. return
  60. }