common-tabbar.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="tabbar" v-if="tabbarList.length > 0">
  3. <view
  4. class="tabbar-item"
  5. :class="{ active: currentTab === item.tabKey }"
  6. v-for="item in tabbarList"
  7. :key="item.id"
  8. @click="switchTab(item)"
  9. >
  10. <text class="tabbar-icon">{{ currentTab === item.tabKey ? (item.iconSelected || item.icon) : item.icon }}</text>
  11. <text class="tabbar-text">{{ item.name }}</text>
  12. <!-- 角标 -->
  13. <view v-if="item.badgeType === 'dot'" class="tabbar-badge-dot"></view>
  14. <view v-else-if="item.badgeType === 'number' && getBadgeValue(item.badgeKey) > 0" class="tabbar-badge">
  15. {{ getBadgeValue(item.badgeKey) > 99 ? '99+' : getBadgeValue(item.badgeKey) }}
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import api from '@/utils/api.js'
  22. export default {
  23. name: 'CommonTabbar',
  24. props: {
  25. // 当前选中的tab
  26. current: {
  27. type: String,
  28. default: ''
  29. },
  30. // 客户端类型:user-用户端,matchmaker-红娘端
  31. clientType: {
  32. type: String,
  33. default: 'user'
  34. },
  35. // 角标数据(key-value形式)
  36. badgeData: {
  37. type: Object,
  38. default: () => ({})
  39. }
  40. },
  41. data() {
  42. return {
  43. tabbarList: [],
  44. currentTab: '',
  45. defaultTabbar: [
  46. ]
  47. }
  48. },
  49. watch: {
  50. current: {
  51. immediate: true,
  52. handler(val) {
  53. this.currentTab = val
  54. }
  55. }
  56. },
  57. created() {
  58. this.loadTabbarConfig()
  59. },
  60. methods: {
  61. // 加载导航配置
  62. async loadTabbarConfig() {
  63. try {
  64. // 先尝试从缓存获取
  65. const cacheKey = `tabbar_config_${this.clientType}`
  66. const cached = uni.getStorageSync(cacheKey)
  67. const cacheTime = uni.getStorageSync(cacheKey + '_time')
  68. // 缓存有效期5分钟
  69. if (cached && cacheTime && (Date.now() - cacheTime < 5 * 60 * 1000)) {
  70. this.tabbarList = cached
  71. return
  72. }
  73. // 从服务器获取
  74. const list = await api.tabbar.getTabbarConfig(this.clientType)
  75. if (list && list.length > 0) {
  76. this.tabbarList = list
  77. // 缓存配置
  78. uni.setStorageSync(cacheKey, list)
  79. uni.setStorageSync(cacheKey + '_time', Date.now())
  80. } else {
  81. // 使用默认配置
  82. this.tabbarList = this.defaultTabbar
  83. }
  84. } catch (e) {
  85. console.error('加载导航配置失败:', e)
  86. // 使用默认配置
  87. this.tabbarList = this.defaultTabbar
  88. }
  89. },
  90. // 切换tab
  91. switchTab(item) {
  92. if (this.currentTab === item.tabKey) {
  93. return
  94. }
  95. this.currentTab = item.tabKey
  96. this.$emit('change', item)
  97. // 跳转页面
  98. uni.switchTab({
  99. url: item.path,
  100. fail: () => {
  101. // switchTab失败时尝试navigateTo
  102. uni.navigateTo({
  103. url: item.path,
  104. fail: () => {
  105. uni.redirectTo({
  106. url: item.path
  107. })
  108. }
  109. })
  110. }
  111. })
  112. },
  113. // 获取角标值
  114. getBadgeValue(key) {
  115. if (!key) return 0
  116. return this.badgeData[key] || 0
  117. },
  118. // 刷新配置(供外部调用)
  119. refreshConfig() {
  120. const cacheKey = `tabbar_config_${this.clientType}`
  121. uni.removeStorageSync(cacheKey)
  122. uni.removeStorageSync(cacheKey + '_time')
  123. this.loadTabbarConfig()
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped lang="scss">
  129. .tabbar {
  130. position: fixed;
  131. bottom: 0;
  132. left: 0;
  133. right: 0;
  134. height: 100rpx;
  135. background: #FFFFFF;
  136. display: flex;
  137. align-items: center;
  138. justify-content: space-around;
  139. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  140. padding-bottom: constant(safe-area-inset-bottom);
  141. padding-bottom: env(safe-area-inset-bottom);
  142. z-index: 999;
  143. .tabbar-item {
  144. flex: 1;
  145. display: flex;
  146. flex-direction: column;
  147. align-items: center;
  148. justify-content: center;
  149. position: relative;
  150. padding: 10rpx 0;
  151. .tabbar-icon {
  152. font-size: 44rpx;
  153. margin-bottom: 4rpx;
  154. }
  155. .tabbar-text {
  156. font-size: 22rpx;
  157. color: #999999;
  158. }
  159. &.active {
  160. .tabbar-text {
  161. color: #E91E63;
  162. font-weight: 500;
  163. }
  164. }
  165. // 数字角标
  166. .tabbar-badge {
  167. position: absolute;
  168. top: 2rpx;
  169. right: 50%;
  170. transform: translateX(30rpx);
  171. min-width: 32rpx;
  172. height: 32rpx;
  173. line-height: 32rpx;
  174. padding: 0 8rpx;
  175. background: #FF4757;
  176. color: #FFFFFF;
  177. font-size: 20rpx;
  178. border-radius: 16rpx;
  179. text-align: center;
  180. }
  181. // 红点角标
  182. .tabbar-badge-dot {
  183. position: absolute;
  184. top: 8rpx;
  185. right: 50%;
  186. transform: translateX(24rpx);
  187. width: 16rpx;
  188. height: 16rpx;
  189. background: #FF4757;
  190. border-radius: 50%;
  191. }
  192. }
  193. }
  194. </style>