callback.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package callback
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "go-common/app/interface/main/app-wall/conf"
  12. "go-common/app/interface/main/app-wall/model"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. httpx "go-common/library/net/http/blademaster"
  16. "github.com/pkg/errors"
  17. )
  18. const (
  19. _iis = "/iis?clkid=%s"
  20. )
  21. type Dao struct {
  22. client *httpx.Client
  23. iisURL string
  24. }
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(conf.Conf.HTTPActive),
  28. iisURL: c.Host.Dotin + _iis,
  29. }
  30. return
  31. }
  32. func (d *Dao) GdtCallback(c context.Context, appID, appType, aderID string, idfa, cb string, now time.Time) (err error) {
  33. key, ok := model.ChannelGdt[aderID]
  34. if !ok {
  35. return
  36. }
  37. encrypt := []byte(key.Encrypt)
  38. signKey := key.Sign
  39. uri := fmt.Sprintf("http://t.gdt.qq.com/conv/app/%s/conv?", appID)
  40. // sign v
  41. queryS := fmt.Sprintf("muid=%s&conv_time=%d&click_id=%s", idfa, now.Unix(), cb)
  42. page := signKey + "&GET&" + url.QueryEscape(uri+queryS)
  43. bs := md5.Sum([]byte(page))
  44. sign := hex.EncodeToString(bs[:])
  45. queryS = queryS + "&sign=" + sign
  46. queryBs := []byte(queryS)
  47. i := 0
  48. bss := []byte{}
  49. for _, b := range queryBs {
  50. bss = append(bss, b^encrypt[i])
  51. i = i + 1
  52. i = i % len(encrypt)
  53. }
  54. baseS := base64.StdEncoding.EncodeToString(bss)
  55. baseS = strings.Replace(baseS, "\n", "", -1)
  56. // finish uri
  57. furi := uri + "v=" + url.QueryEscape(baseS) + fmt.Sprintf("&conv_type=MOBILEAPP_ACTIVITE&app_type=%s&advertiser_id=%s", appType, aderID)
  58. var res struct {
  59. Ret int `json:"ret"`
  60. Msg string `json:"msg"`
  61. }
  62. for i := 0; i < 5; i++ {
  63. if err = d.client.Get(c, furi, "", nil, &res); err == nil {
  64. break
  65. }
  66. }
  67. if err != nil {
  68. return
  69. }
  70. if !ecode.Int(res.Ret).Equal(ecode.OK) {
  71. err = errors.Wrapf(ecode.Int(res.Ret), furi)
  72. if res.Ret == -1 {
  73. log.Error("%+v", err)
  74. err = nil
  75. }
  76. return
  77. }
  78. log.Info("callback gdt furi(%s) idfa(%s) cb(%s) success ret(%d) msg(%s)", furi, idfa, cb, res.Ret, res.Msg)
  79. return
  80. }
  81. func (d *Dao) ShikeCallback(c context.Context, idfa, cb string, now time.Time) (err error) {
  82. var res struct {
  83. Success string `json:"success"`
  84. Message string `json:"message"`
  85. }
  86. if err = d.client.Get(c, cb, "", nil, &res); err != nil {
  87. return
  88. }
  89. log.Info("callback shike idfa(%s) cb(%s) success ret(%s) msg(%s)", idfa, cb, res.Success, res.Message)
  90. return
  91. }
  92. func (d *Dao) DontinCallback(c context.Context, idfa, clickid string) (err error) {
  93. urlStr := fmt.Sprintf(d.iisURL, clickid)
  94. if err = d.client.Get(c, urlStr, "", nil, nil); err != nil {
  95. return
  96. }
  97. log.Info("callback dontin idfa(%s) clickid(%s) success", idfa, clickid)
  98. return
  99. }
  100. func (d *Dao) ToutiaoCallback(c context.Context, cb string, eventType string) (err error) {
  101. if cb == "" {
  102. return
  103. }
  104. cbURL := strings.TrimSpace(cb + "&event_type=" + eventType)
  105. var res struct {
  106. Ret int `json:"ret"`
  107. }
  108. if err = d.client.Get(c, cbURL, "", nil, &res); err != nil {
  109. return
  110. }
  111. if !ecode.Int(res.Ret).Equal(ecode.OK) {
  112. err = errors.Wrap(ecode.Int(res.Ret), cbURL)
  113. if res.Ret == -1 {
  114. log.Error("%+v", err)
  115. err = nil
  116. }
  117. return
  118. }
  119. log.Info("callback toutiao cb(%s) eventType(%s) success", cb, eventType)
  120. return
  121. }