converge.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package converge
  2. import (
  3. "encoding/json"
  4. "go-common/library/log"
  5. "strconv"
  6. "go-common/app/interface/main/app-channel/model"
  7. )
  8. type Card struct {
  9. ID int64
  10. ReType int
  11. ReValue string
  12. Title string
  13. Cover string
  14. Content json.RawMessage
  15. Contents []*Content
  16. }
  17. type JSONContent struct {
  18. CType string `json:"ctype"`
  19. CTitle string `json:"ctitle"`
  20. CValue json.RawMessage `json:"cvalue"`
  21. }
  22. type Content struct {
  23. Goto string `json:"goto,omitempty"`
  24. ID int64 `json:"id,omitempty"`
  25. Title string `json:"title,omitempty"`
  26. }
  27. func (c *Card) Change() {
  28. var (
  29. contents []*Content
  30. jsonContents []*JSONContent
  31. err error
  32. )
  33. if err = json.Unmarshal(c.Content, &jsonContents); err != nil {
  34. log.Error("json.Unmarshal(%s) error(%v)", c.Content, err)
  35. return
  36. }
  37. contents = make([]*Content, 0, len(jsonContents))
  38. for _, js := range jsonContents {
  39. switch js.CType {
  40. case "0", "1", "2":
  41. content := &Content{Title: js.CTitle}
  42. if js.CType == "0" {
  43. content.Goto = model.GotoAv
  44. } else if js.CType == "1" {
  45. content.Goto = model.GotoLive
  46. } else if js.CType == "2" {
  47. content.Goto = model.GotoArticle
  48. } else {
  49. continue
  50. }
  51. var (
  52. idStr string
  53. id int64
  54. )
  55. if err = json.Unmarshal(js.CValue, &idStr); err != nil {
  56. log.Error("json.Unmarshal(%s) error(%v)", js.CValue, err)
  57. continue
  58. }
  59. if id, _ = strconv.ParseInt(idStr, 10, 64); id != 0 {
  60. content.ID = id
  61. contents = append(contents, content)
  62. }
  63. }
  64. }
  65. c.Contents = contents
  66. }