show.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package show
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "go-common/app/interface/main/app-resource/model"
  8. "go-common/app/interface/main/app-resource/model/abtest"
  9. "go-common/app/interface/main/app-resource/model/show"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. farm "github.com/dgryski/go-farm"
  13. )
  14. const (
  15. _initTabKey = "tab_%d_%s"
  16. _initVersion = "showtab_version"
  17. _defaultLanguageHans = "hans"
  18. _defaultLanguageHant = "hant"
  19. )
  20. var (
  21. _showAbtest = map[string]string{
  22. "bilibili://pegasus/hottopic": "home_tabbar_server_1",
  23. }
  24. _deafaultTab = map[string]*show.Tab{
  25. "bilibili://pegasus/promo": &show.Tab{
  26. DefaultSelected: 1,
  27. },
  28. }
  29. )
  30. // Tabs show tabs
  31. func (s *Service) Tabs(c context.Context, plat int8, build int, buvid, ver, mobiApp, language string, mid int64) (res map[string][]*show.Tab, version string, a *abtest.List, err error) {
  32. if key := fmt.Sprintf(_initTabKey, plat, language); len(s.tabCache[fmt.Sprintf(key)]) == 0 || language == "" {
  33. if model.IsOverseas(plat) {
  34. var key = fmt.Sprintf(_initTabKey, plat, _defaultLanguageHant)
  35. if len(s.tabCache[fmt.Sprintf(key)]) > 0 {
  36. language = _defaultLanguageHant
  37. } else {
  38. language = _defaultLanguageHans
  39. }
  40. } else {
  41. language = _defaultLanguageHans
  42. }
  43. }
  44. var (
  45. key = fmt.Sprintf(_initTabKey, plat, language)
  46. tmptabs = []*show.Tab{}
  47. )
  48. res = map[string][]*show.Tab{}
  49. if tabs, ok := s.tabCache[key]; ok {
  50. LOOP:
  51. for _, v := range tabs {
  52. for _, l := range s.limitsCahce[v.ID] {
  53. if model.InvalidBuild(build, l.Build, l.Condition) {
  54. continue LOOP
  55. }
  56. }
  57. if !s.c.ShowHotAll {
  58. if ab, ok := s.abtestCache[v.Group]; ok {
  59. if _, ok := s.showTabMids[mid]; !ab.AbTestIn(buvid) && !ok {
  60. continue LOOP
  61. }
  62. a = &abtest.List{}
  63. a.ListChange(ab)
  64. }
  65. }
  66. tmptabs = append(tmptabs, v)
  67. }
  68. }
  69. if !s.auditTab(mobiApp, build, plat) {
  70. if menus := s.menus(plat, build); len(menus) > 0 {
  71. tmptabs = append(tmptabs, menus...)
  72. }
  73. }
  74. for _, v := range tmptabs {
  75. t := &show.Tab{}
  76. *t = *v
  77. t.Pos = len(res[v.ModuleStr]) + 1
  78. res[v.ModuleStr] = append(res[v.ModuleStr], t)
  79. }
  80. if version = s.hash(res); version == ver {
  81. err = ecode.NotModified
  82. res = nil
  83. }
  84. return
  85. }
  86. func (s *Service) menus(plat int8, build int) (res []*show.Tab) {
  87. memuCache := s.menuCache
  88. LOOP:
  89. for _, m := range memuCache {
  90. if vs, ok := m.Versions[model.PlatAPPBuleChange(plat)]; ok {
  91. for _, v := range vs {
  92. if model.InvalidBuild(build, v.Build, v.Condition) {
  93. continue LOOP
  94. }
  95. }
  96. t := &show.Tab{}
  97. t.TabMenuChange(m)
  98. res = append(res, t)
  99. }
  100. }
  101. return
  102. }
  103. func (s *Service) hash(v map[string][]*show.Tab) string {
  104. bs, err := json.Marshal(v)
  105. if err != nil {
  106. log.Error("json.Marshal error(%v)", err)
  107. return _initVersion
  108. }
  109. return strconv.FormatUint(farm.Hash64(bs), 10)
  110. }