set-tab-bar.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const defaultTabBarStyle = {
  2. color: '#7A7E83',
  3. selectedColor: '#3cc51f',
  4. backgroundColor: '#ffffff',
  5. }
  6. const darkDefaultTabBarStyle = {
  7. color: '#FFFFFF',
  8. selectedColor: '#51A937',
  9. backgroundColor: '#1F1F1F',
  10. }
  11. const defaultItemName = '接口'
  12. Component({
  13. data: {
  14. hasSetTabBarBadge: false,
  15. hasShownTabBarRedDot: false,
  16. hasCustomedStyle: false,
  17. hasCustomedItem: false,
  18. hasHiddenTabBar: false,
  19. theme: 'light'
  20. },
  21. attached() {
  22. this.setData({
  23. theme: wx.getSystemInfoSync().theme || 'light'
  24. })
  25. if (wx.onThemeChange) {
  26. wx.onThemeChange(({theme}) => {
  27. this.setData({theme})
  28. if (this.data.theme == 'dark') {
  29. wx.setTabBarStyle(darkDefaultTabBarStyle)
  30. } else {
  31. wx.setTabBarStyle(defaultTabBarStyle)
  32. }
  33. })
  34. }
  35. wx.pageScrollTo({
  36. scrollTop: 0,
  37. duration: 0
  38. })
  39. },
  40. detached() {
  41. this.removeTabBarBadge()
  42. this.hideTabBarRedDot()
  43. this.showTabBar()
  44. this.removeCustomStyle()
  45. this.removeCustomItem()
  46. },
  47. methods: {
  48. navigateBack() {
  49. this.triggerEvent('unmount')
  50. },
  51. setTabBarBadge() {
  52. if (this.data.hasSetTabBarBadge) {
  53. this.removeTabBarBadge()
  54. return
  55. }
  56. this.setData({
  57. hasSetTabBarBadge: true
  58. })
  59. wx.setTabBarBadge({
  60. index: 1,
  61. text: '1',
  62. })
  63. },
  64. removeTabBarBadge() {
  65. this.setData({
  66. hasSetTabBarBadge: false
  67. })
  68. wx.removeTabBarBadge({
  69. index: 1,
  70. })
  71. },
  72. showTabBarRedDot() {
  73. if (this.data.hasShownTabBarRedDot) {
  74. this.hideTabBarRedDot()
  75. return
  76. }
  77. this.setData({
  78. hasShownTabBarRedDot: true
  79. })
  80. wx.showTabBarRedDot({
  81. index: 1
  82. })
  83. },
  84. hideTabBarRedDot() {
  85. this.setData({
  86. hasShownTabBarRedDot: false
  87. })
  88. wx.hideTabBarRedDot({
  89. index: 1
  90. })
  91. },
  92. showTabBar() {
  93. this.setData({hasHiddenTabBar: false})
  94. wx.showTabBar()
  95. },
  96. hideTabBar() {
  97. if (this.data.hasHiddenTabBar) {
  98. this.showTabBar()
  99. return
  100. }
  101. this.setData({hasHiddenTabBar: true})
  102. wx.hideTabBar()
  103. },
  104. customStyle() {
  105. if (this.data.hasCustomedStyle) {
  106. this.removeCustomStyle()
  107. return
  108. }
  109. this.setData({hasCustomedStyle: true})
  110. wx.setTabBarStyle({
  111. color: '#FFF',
  112. selectedColor: '#1AAD19',
  113. backgroundColor: '#000000',
  114. })
  115. },
  116. removeCustomStyle() {
  117. this.setData({hasCustomedStyle: false})
  118. if (this.data.theme == 'dark') {
  119. wx.setTabBarStyle(darkDefaultTabBarStyle)
  120. } else {
  121. wx.setTabBarStyle(defaultTabBarStyle)
  122. }
  123. },
  124. customItem() {
  125. if (this.data.hasCustomedItem) {
  126. this.removeCustomItem()
  127. return
  128. }
  129. this.setData({hasCustomedItem: true})
  130. wx.setTabBarItem({
  131. index: 1,
  132. text: 'API'
  133. })
  134. },
  135. removeCustomItem() {
  136. this.setData({hasCustomedItem: false})
  137. wx.setTabBarItem({
  138. index: 1,
  139. text: defaultItemName
  140. })
  141. }
  142. }
  143. })