banner.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package model
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. xtime "go-common/library/time"
  6. )
  7. // Banners struct
  8. type Banners struct {
  9. Banner map[int][]*Banner
  10. Version string
  11. }
  12. // Banner struct
  13. type Banner struct {
  14. ID int `json:"id"`
  15. ParentID int `json:"-"`
  16. Plat int8 `json:"-"`
  17. Module string `json:"-"`
  18. Position string `json:"-"`
  19. Title string `json:"title"`
  20. Image string `json:"image"`
  21. Hash string `json:"hash"`
  22. URI string `json:"uri"`
  23. Goto string `json:"-"`
  24. Value string `json:"-"`
  25. Param string `json:"-"`
  26. Channel string `json:"-"`
  27. Build int `json:"-"`
  28. Condition string `json:"-"`
  29. Area string `json:"-"`
  30. Rank int `json:"-"`
  31. Rule string `json:"-"`
  32. Type int8 `json:"-"`
  33. Start xtime.Time `json:"stime"`
  34. End xtime.Time `json:"-"`
  35. MTime xtime.Time `json:"-"`
  36. ResourceID int `json:"resource_id"`
  37. RequestId string `json:"request_id,omitempty"`
  38. CreativeId int `json:"creative_id,omitempty"`
  39. SrcId int `json:"src_id,omitempty"`
  40. IsAd bool `json:"is_ad"`
  41. IsAdReplace bool `json:"-"`
  42. IsAdLoc bool `json:"is_ad_loc,omitempty"`
  43. CmMark int `json:"cm_mark"`
  44. AdCb string `json:"ad_cb,omitempty"`
  45. ShowUrl string `json:"show_url,omitempty"`
  46. ClickUrl string `json:"click_url,omitempty"`
  47. ClientIp string `json:"client_ip,omitempty"`
  48. Index int `json:"index"`
  49. ServerType int `json:"server_type"`
  50. Extra json.RawMessage `json:"extra"`
  51. CreativeType int `json:"creative_type"`
  52. }
  53. // JSONBanner bilibili_assignment rule
  54. type JSONBanner struct {
  55. Area string `json:"area"`
  56. Hash string `json:"hash"`
  57. Build int `json:"build"`
  58. Condition string `json:"cond"`
  59. Channel string `json:"channel"`
  60. CreativeType int `json:"creative_type"`
  61. }
  62. // Limit limit
  63. type Limit struct {
  64. Rule string `json:"-"`
  65. }
  66. // JSONLimit limit
  67. type JSONLimit struct {
  68. Limit int `json:"limit"`
  69. Resrc []string `json:"resrc"`
  70. }
  71. // BannerChange change banner
  72. func (b *Banner) BannerChange() {
  73. var tmp *JSONBanner
  74. if err := json.Unmarshal([]byte(b.Rule), &tmp); err == nil {
  75. b.Area = tmp.Area
  76. b.Build = tmp.Build
  77. b.Condition = tmp.Condition
  78. if tmp.Channel == "" {
  79. b.Channel = "*"
  80. } else {
  81. b.Channel = tmp.Channel
  82. }
  83. b.Hash = tmp.Hash
  84. b.CreativeType = tmp.CreativeType
  85. }
  86. switch b.Plat {
  87. case 1: // resource iphone
  88. b.Plat = PlatIPhone
  89. case 2: // resource android
  90. b.Plat = PlatAndroid
  91. case 3: // resource pad
  92. b.Plat = PlatIPad
  93. case 4: // resource iphoneg
  94. b.Plat = PlatIPhoneI
  95. case 5: // resource androidg
  96. b.Plat = PlatAndroidG
  97. case 6: // resource padg
  98. b.Plat = PlatIPadI
  99. case 8: // resource androidi
  100. b.Plat = PlatAndroidI
  101. }
  102. if b.Value == "" {
  103. return
  104. }
  105. switch b.Type {
  106. case 7:
  107. if b.Plat == PlatIPhone || b.Plat == PlatAndroid || b.Plat == PlatIPad || b.Plat == PlatIPhoneI || b.Plat == PlatAndroidG || b.Plat == PlatIPadI || b.Plat == PlatAndroidI {
  108. b.URI = "bilibili://pegasus/channel/" + b.Value + "/"
  109. } else {
  110. b.URI = "http://www.bilibili.com/tag/" + b.Value
  111. }
  112. case 6:
  113. //GotoAv
  114. b.URI = "bilibili://video/" + b.Value
  115. case 4:
  116. //GotoLive
  117. if b.Plat == PlatIPad {
  118. b.URI = "bilibili://player/live/" + b.Value
  119. } else {
  120. b.URI = "bilibili://live/" + b.Value
  121. }
  122. case 3:
  123. //GotoBangumi
  124. b.URI = "bilibili://bangumi/season/" + b.Value
  125. case 5:
  126. //GotoGame
  127. b.URI = "bilibili://game/" + b.Value
  128. case 2:
  129. //GotoWeb
  130. b.URI = b.Value
  131. }
  132. }
  133. // LimitChange change limit
  134. func (l *Limit) LimitChange() (data map[int]int) {
  135. data = map[int]int{}
  136. var (
  137. tmp = &JSONLimit{}
  138. err error
  139. resid int
  140. )
  141. if err = json.Unmarshal([]byte(l.Rule), tmp); err != nil {
  142. return
  143. }
  144. l.Rule = ""
  145. for _, residstr := range tmp.Resrc {
  146. resid, err = strconv.Atoi(residstr)
  147. if err != nil {
  148. return
  149. }
  150. data[resid] = tmp.Limit
  151. }
  152. return
  153. }