cpm.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package ad
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. ad "go-common/app/interface/main/web-show/model/resource"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. // Cpms get ads from cpm platform
  12. func (d *Dao) Cpms(c context.Context, mid int64, ids []int64, sid, ip, country, province, city, buvid string) (advert *ad.Ad, err error) {
  13. params := url.Values{}
  14. params.Set("mid", strconv.FormatInt(mid, 10))
  15. params.Set("sid", sid)
  16. params.Set("buvid", buvid)
  17. params.Set("resource", xstr.JoinInts(ids))
  18. params.Set("ip", ip)
  19. params.Set("country", country)
  20. params.Set("province", province)
  21. params.Set("city", city)
  22. var res struct {
  23. Code int `json:"code"`
  24. Data *ad.Ad `json:"data"`
  25. }
  26. if err = d.httpClient.Get(c, d.cpmURL, "", params, &res); err != nil {
  27. log.Error("cpm url(%s) error(%v)", d.cpmURL+"?"+params.Encode(), err)
  28. return
  29. }
  30. if res.Code != 0 {
  31. err = fmt.Errorf("cpm api failed(%d)", res.Code)
  32. log.Error("url(%s) res code(%d) or res.data(%v)", d.cpmURL+"?"+params.Encode(), res.Code, res.Data)
  33. return
  34. }
  35. advert = res.Data
  36. return
  37. }