tab.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package operate
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "strconv"
  6. "go-common/app/interface/main/app-card/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. type Menu struct {
  11. TabID int64 `json:"tab_id,omitempty"`
  12. Name string `json:"name,omitempty"`
  13. Img string `json:"img,omitempty"`
  14. Icon string `json:"icon,omitempty"`
  15. Color string `json:"color,omitempty"`
  16. ID int64 `json:"id,omitempty"`
  17. Plat int `json:"-"`
  18. CType int `json:"-"`
  19. CValue string `json:"-"`
  20. PlatVersion json.RawMessage `json:"-"`
  21. STime xtime.Time `json:"-"`
  22. ETime xtime.Time `json:"-"`
  23. Status int `json:"-"`
  24. Badge string `json:"-"`
  25. Versions map[int8][]*Version `json:"-"`
  26. }
  27. type Version struct {
  28. PlatStr string `json:"plat,omitempty"`
  29. BuildStr string `json:"build,omitempty"`
  30. Condition string `json:"conditions,omitempty"`
  31. Plat int8 `json:"-"`
  32. Build int `json:"-"`
  33. }
  34. func (m *Menu) Change() {
  35. m.Icon = m.Badge
  36. var vs []*Version
  37. if err := json.Unmarshal(m.PlatVersion, &vs); err != nil {
  38. log.Error("json.Unmarshal(%s) error(%v)", m.PlatVersion, err)
  39. return
  40. }
  41. vm := make(map[int8][]*Version, len(vs))
  42. for _, v := range vs {
  43. if v.PlatStr == "" || v.BuildStr == "" {
  44. continue
  45. }
  46. if plat, err := strconv.ParseInt(v.PlatStr, 10, 8); err != nil {
  47. log.Error("strconv.ParseInt(%s,10,8) error(%v)", v.PlatStr, err)
  48. continue
  49. } else {
  50. v.Plat = int8(plat)
  51. }
  52. if build, err := strconv.Atoi(v.BuildStr); err != nil {
  53. log.Error("strconv.Atoi(%s) error(%v)", v.BuildStr, err)
  54. continue
  55. } else {
  56. v.Build = build
  57. }
  58. vm[v.Plat] = append(vm[v.Plat], v)
  59. }
  60. m.Versions = vm
  61. if m.CType == 1 {
  62. var err error
  63. if m.ID, err = strconv.ParseInt(m.CValue, 10, 64); err != nil {
  64. log.Error("strconv.ParseInt(%s) error(%v)", m.CValue, err)
  65. return
  66. }
  67. }
  68. }
  69. type Active struct {
  70. ID int64 `json:"id,omitempty"`
  71. ParentID int64 `json:"parent_id,omitempty"`
  72. Name string `json:"name,omitempty"`
  73. Background string `json:"background,omitempty"`
  74. Type string `json:"type,omitempty"`
  75. Content json.RawMessage `json:"content,omitempty"`
  76. // Extra
  77. Pid int64 `json:"pid,omitempty"`
  78. Title string `json:"title,omitempty"`
  79. Subtitle string `json:"subtitle,omitempty"`
  80. Desc string `json:"desc,omitempty"`
  81. Param string `json:"param,omitempty"`
  82. Goto model.Gt `json:"goto,omitempty"`
  83. Cover string `json:"cover,omitempty"`
  84. Limit int `json:"limit,omitempty"`
  85. Items []*Active `json:"items,omitempty"`
  86. }
  87. type Actives []*Active
  88. func (is Actives) Len() int { return len(is) }
  89. func (is Actives) Less(i, j int) bool { return is[i].ID < is[j].ID }
  90. func (is Actives) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
  91. type CardItem struct {
  92. Title string `json:"title,omitempty"`
  93. Cover string `json:"cover,omitempty"`
  94. LinkType string `json:"link_type,omitempty"`
  95. LinkValue string `json:"link_value,omitempty"`
  96. Weight int `json:"weight,omitempty"`
  97. }
  98. type CardItems []*CardItem
  99. func (is CardItems) Len() int { return len(is) }
  100. func (is CardItems) Less(i, j int) bool { return is[i].Weight < is[j].Weight }
  101. func (is CardItems) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
  102. func (a *Active) Change() {
  103. switch a.Type {
  104. case "archive":
  105. a.Type = "player"
  106. case "live":
  107. a.Type = "player_live"
  108. case "basic":
  109. a.Type = "content_rcmd"
  110. case "shortcut":
  111. a.Type = "entrance"
  112. case "common":
  113. a.Type = "background"
  114. case "tag":
  115. a.Type = "tag_rcmd"
  116. }
  117. switch a.Type {
  118. // 基本类型
  119. case "player_live", "converge", "special", "article_s", "player":
  120. var id int64
  121. if err := json.Unmarshal(a.Content, &id); err != nil {
  122. log.Error("%+v", err)
  123. return
  124. }
  125. if id > 0 {
  126. a.Pid = id
  127. }
  128. // 新增类型
  129. case "content_rcmd":
  130. var basic struct {
  131. Type string `json:"type,omitempty"`
  132. Title string `json:"title,omitempty"`
  133. Subtitle string `json:"subtitle,omitempty"`
  134. Sublink string `json:"sublink,omitempty"`
  135. Content []*struct {
  136. LinkType string `json:"link_type,omitempty"`
  137. LinkValue string `json:"link_value,omitempty"`
  138. } `json:"content,omitempty"`
  139. }
  140. if err := json.Unmarshal(a.Content, &basic); err != nil {
  141. log.Error("%+v", err)
  142. return
  143. }
  144. ris := make([]*Active, 0, len(basic.Content))
  145. for _, c := range basic.Content {
  146. typ, _ := strconv.Atoi(c.LinkType)
  147. id, _ := strconv.ParseInt(c.LinkValue, 10, 64)
  148. ri := &Active{Goto: model.OperateType[typ], Pid: id}
  149. if ri.Goto != "" {
  150. ris = append(ris, ri)
  151. }
  152. }
  153. if len(ris) != 0 {
  154. a.Items = ris
  155. a.Title = basic.Title
  156. a.Subtitle = basic.Subtitle
  157. a.Param = basic.Sublink
  158. }
  159. case "entrance", "banner":
  160. var card struct {
  161. Type string `json:"type,omitempty"`
  162. CardItem []*CardItem `json:"card_item,omitempty"`
  163. }
  164. if err := json.Unmarshal(a.Content, &card); err != nil {
  165. log.Error("%+v", err)
  166. return
  167. }
  168. ris := make([]*Active, 0, len(card.CardItem))
  169. sort.Sort(CardItems(card.CardItem))
  170. for _, item := range card.CardItem {
  171. typ, _ := strconv.Atoi(item.LinkType)
  172. id, _ := strconv.ParseInt(item.LinkValue, 10, 64)
  173. ri := &Active{Goto: model.OperateType[typ], Pid: id, Param: item.LinkValue, Title: item.Title, Cover: item.Cover}
  174. if ri.Goto != "" {
  175. ris = append(ris, ri)
  176. }
  177. }
  178. if len(ris) != 0 {
  179. a.Items = ris
  180. }
  181. case "tag_rcmd":
  182. var tag struct {
  183. AidStr string `json:"aid,omitempty"`
  184. Type string `json:"type,omitempty"`
  185. NumberStr string `json:"number,omitempty"`
  186. Tid int64 `json:"-"`
  187. Number int `json:"-"`
  188. }
  189. if err := json.Unmarshal(a.Content, &tag); err != nil {
  190. log.Error("%+v", err)
  191. return
  192. }
  193. tag.Tid, _ = strconv.ParseInt(tag.AidStr, 10, 64)
  194. tag.Number, _ = strconv.Atoi(tag.NumberStr)
  195. if tag.Tid != 0 {
  196. a.Pid = tag.Tid
  197. a.Limit = tag.Number
  198. }
  199. case "background":
  200. a.Title = a.Name
  201. a.Cover = a.Background
  202. case "news":
  203. var news struct {
  204. Title string `json:"title,omitempty"`
  205. Body string `json:"body,omitempty"`
  206. Link string `json:"link,omitempty"`
  207. }
  208. if err := json.Unmarshal(a.Content, &news); err != nil {
  209. log.Error("%+v", err)
  210. return
  211. }
  212. if news.Body != "" {
  213. a.Title = news.Title
  214. a.Desc = news.Body
  215. a.Param = news.Link
  216. }
  217. }
  218. }