ad.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package ad
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "go-common/app/interface/main/app-view/model"
  6. )
  7. type Ad struct {
  8. RequestID string `json:"request_id,omitempty"`
  9. AdsControl json.RawMessage `json:"ads_control,omitempty"`
  10. AdsInfo map[int64]map[int64]*AdsInfo `json:"ads_info,omitempty"`
  11. ClientIP string `json:"-"`
  12. }
  13. type AdsInfo struct {
  14. Index int `json:"index,omitempty"`
  15. AdInfo *AdInfo `json:"ad_info,omitempty"`
  16. IsAd bool `json:"is_ad,omitempty"`
  17. CmMark int `json:"cm_mark,omitempty"`
  18. CardIndex int `json:"card_index,omitempty"`
  19. }
  20. type AdInfo struct {
  21. CreativeID int64 `json:"creative_id,omitempty"`
  22. CreativeType int64 `json:"creative_type,omitempty"`
  23. CreativeContent *struct {
  24. Title string `json:"title,omitempty"`
  25. Desc string `json:"description,omitempty"`
  26. ButtonTitle string `json:"button_title,omitempty"`
  27. VideoID int64 `json:"video_id,omitempty"`
  28. UserName string `json:"username,omitempty"`
  29. ImageURL string `json:"image_url,omitempty"`
  30. ImageMD5 string `json:"image_md5,omitempty"`
  31. LogURL string `json:"log_url,omitempty"`
  32. LogMD5 string `json:"log_md5,omitempty"`
  33. URL string `json:"url,omitempty"`
  34. ClickURL string `json:"click_url,omitempty"`
  35. ShowURL string `json:"show_url,omitempty"`
  36. } `json:"creative_content,omitempty"`
  37. AdCb string `json:"ad_cb,omitempty"`
  38. CardType int `json:"card_type,omitempty"`
  39. Extra json.RawMessage `json:"extra,omitempty"`
  40. Resource int64 `json:"-"`
  41. Source int64 `json:"-"`
  42. RequestID string `json:"-"`
  43. IsAd bool `json:"-"`
  44. CmMark int `json:"-"`
  45. Index int `json:"-"`
  46. IsAdLoc bool `json:"-"`
  47. CardIndex int `json:"-"`
  48. ClientIP string `json:"-"`
  49. // ad
  50. URI string `json:"-"`
  51. Param string `json:"-"`
  52. Goto string `json:"-"`
  53. View int `json:"-"`
  54. Danmaku int `json:"-"`
  55. }
  56. func (ad *Ad) Convert(resource int64) (ads []*AdInfo, aids []int64) {
  57. if ad == nil {
  58. return
  59. }
  60. if adsInfo, ok := ad.AdsInfo[resource]; ok {
  61. ads = make([]*AdInfo, 0, len(adsInfo))
  62. for source, info := range adsInfo {
  63. var adInfo *AdInfo
  64. if info != nil {
  65. if info.AdInfo != nil {
  66. adInfo = info.AdInfo
  67. adInfo.RequestID = ad.RequestID
  68. adInfo.Resource = resource
  69. adInfo.Source = source
  70. adInfo.IsAd = info.IsAd
  71. adInfo.IsAdLoc = true
  72. adInfo.CmMark = info.CmMark
  73. adInfo.Index = info.Index
  74. adInfo.CardIndex = info.CardIndex
  75. adInfo.ClientIP = ad.ClientIP
  76. // http://info.bilibili.co/pages/viewpage.action?pageId=6227100
  77. switch adInfo.CardType {
  78. case 6:
  79. adInfo.Goto = model.GotoAv
  80. if adInfo.CreativeContent != nil {
  81. adInfo.Param = strconv.FormatInt(int64(adInfo.CreativeContent.VideoID), 10)
  82. if adInfo.CreativeContent.VideoID > 0 {
  83. aids = append(aids, adInfo.CreativeContent.VideoID)
  84. }
  85. } else {
  86. adInfo.Param = "0"
  87. }
  88. adInfo.URI = model.FillURI(adInfo.Goto, adInfo.Param, nil)
  89. default:
  90. adInfo.Goto = model.GotoWeb
  91. if adInfo.CreativeContent != nil {
  92. adInfo.Param = adInfo.CreativeContent.URL
  93. }
  94. adInfo.URI = model.FillURI(adInfo.Goto, adInfo.Param, nil)
  95. }
  96. } else {
  97. adInfo = &AdInfo{RequestID: ad.RequestID, Resource: resource, Source: source, IsAdLoc: true, IsAd: info.IsAd, CmMark: info.CmMark, Index: info.Index, CardIndex: info.CardIndex, ClientIP: ad.ClientIP}
  98. }
  99. }
  100. if adInfo != nil {
  101. ads = append(ads, adInfo)
  102. }
  103. }
  104. }
  105. return
  106. }
  107. type AdInfos []*AdInfo
  108. func (a AdInfos) Len() int { return len(a) }
  109. func (a AdInfos) Less(i, j int) bool { return int64(a[i].Index) < int64(a[j].Index) }
  110. func (a AdInfos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }