index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <script setup lang="ts">
  2. // i-carbon-code
  3. import type { CustomTabBarItem } from './config'
  4. import { customTabbarEnable, needHideNativeTabbar, tabbarCacheEnable } from './config'
  5. import { tabbarList, tabbarStore } from './store'
  6. // #ifdef MP-WEIXIN
  7. // 将自定义节点设置成虚拟的(去掉自定义组件包裹层),更加接近Vue组件的表现,能更好的使用flex属性
  8. defineOptions({
  9. virtualHost: true,
  10. })
  11. // #endif
  12. /**
  13. * 中间的鼓包tabbarItem的点击事件
  14. */
  15. function handleClickBulge() {
  16. uni.showToast({
  17. title: '点击了中间的鼓包tabbarItem',
  18. icon: 'none',
  19. })
  20. }
  21. function handleClick(index: number) {
  22. // 点击原来的不做操作
  23. if (index === tabbarStore.curIdx) {
  24. return
  25. }
  26. if (tabbarList[index].isBulge) {
  27. handleClickBulge()
  28. return
  29. }
  30. const url = tabbarList[index].pagePath
  31. tabbarStore.setCurIdx(index)
  32. if (tabbarCacheEnable) {
  33. uni.switchTab({ url })
  34. }
  35. else {
  36. uni.navigateTo({ url })
  37. }
  38. }
  39. // #ifndef MP-WEIXIN
  40. // 因为有了 custom:true, 微信里面不需要多余的hide操作
  41. onLoad(() => {
  42. // 解决原生 tabBar 未隐藏导致有2个 tabBar 的问题
  43. needHideNativeTabbar
  44. && uni.hideTabBar({
  45. fail(err) {
  46. console.log('hideTabBar fail: ', err)
  47. },
  48. success(res) {
  49. // console.log('hideTabBar success: ', res)
  50. },
  51. })
  52. })
  53. // #endif
  54. const activeColor = 'var(--wot-color-theme, #1890ff)'
  55. const inactiveColor = '#666'
  56. function getColorByIndex(index: number) {
  57. return tabbarStore.curIdx === index ? activeColor : inactiveColor
  58. }
  59. function getImageByIndex(index: number, item: CustomTabBarItem) {
  60. if (!item.iconActive) {
  61. console.warn('image 模式下,需要配置 iconActive (高亮时的图片),否则无法切换高亮图片')
  62. return item.icon
  63. }
  64. return tabbarStore.curIdx === index ? item.iconActive : item.icon
  65. }
  66. </script>
  67. <template>
  68. <view v-if="customTabbarEnable" class="h-50px pb-safe">
  69. <view class="border-and-fixed bg-white" @touchmove.stop.prevent>
  70. <view class="h-50px flex items-center">
  71. <view
  72. v-for="(item, index) in tabbarList" :key="index"
  73. class="flex flex-1 flex-col items-center justify-center"
  74. :style="{ color: getColorByIndex(index) }"
  75. @click="handleClick(index)"
  76. >
  77. <view v-if="item.isBulge" class="relative">
  78. <!-- 中间一个鼓包tabbarItem的处理 -->
  79. <view class="bulge">
  80. <!-- TODO 2/2: 中间鼓包tabbarItem配置:通常是一个图片,或者icon,点击触发业务逻辑 -->
  81. <!-- 常见的是:扫描按钮、发布按钮、更多按钮等 -->
  82. <image class="mt-6rpx h-200rpx w-200rpx" src="/static/tabbar/scan.png" />
  83. </view>
  84. </view>
  85. <view v-else class="relative px-3 text-center">
  86. <template v-if="item.iconType === 'uiLib'">
  87. <!-- TODO: 以下内容请根据选择的UI库自行替换 -->
  88. <!-- 如:<wd-icon name="home" /> (https://wot-design-uni.cn/component/icon.html) -->
  89. <!-- 如:<uv-icon name="home" /> (https://www.uvui.cn/components/icon.html) -->
  90. <!-- 如:<sar-icon name="image" /> (https://sard.wzt.zone/sard-uniapp-docs/components/icon)(sar没有home图标^_^) -->
  91. <wd-icon :name="item.icon" size="20" />
  92. </template>
  93. <template v-if="item.iconType === 'unocss' || item.iconType === 'iconfont'">
  94. <view :class="item.icon" class="text-20px" />
  95. </template>
  96. <template v-if="item.iconType === 'image'">
  97. <image :src="getImageByIndex(index, item)" mode="scaleToFill" class="h-20px w-20px" />
  98. </template>
  99. <view class="mt-2px text-12px">
  100. {{ item.text }}
  101. </view>
  102. <!-- 角标显示 -->
  103. <view v-if="item.badge">
  104. <template v-if="item.badge === 'dot'">
  105. <view class="absolute right-0 top-0 h-2 w-2 rounded-full bg-#f56c6c" />
  106. </template>
  107. <template v-else>
  108. <view class="absolute top-0 box-border h-5 min-w-5 center rounded-full bg-#f56c6c px-1 text-center text-xs text-white -right-3">
  109. {{ item.badge > 99 ? '99+' : item.badge }}
  110. </view>
  111. </template>
  112. </view>
  113. </view>
  114. </view>
  115. </view>
  116. <view class="pb-safe" />
  117. </view>
  118. </view>
  119. </template>
  120. <style scoped lang="scss">
  121. .border-and-fixed {
  122. position: fixed;
  123. bottom: 0;
  124. left: 0;
  125. right: 0;
  126. border-top: 1px solid #eee;
  127. box-sizing: border-box;
  128. }
  129. // 中间鼓包的样式
  130. .bulge {
  131. position: absolute;
  132. top: -20px;
  133. left: 50%;
  134. transform-origin: top center;
  135. transform: translateX(-50%) scale(0.5) translateY(-33%);
  136. display: flex;
  137. justify-content: center;
  138. align-items: center;
  139. width: 250rpx;
  140. height: 250rpx;
  141. border-radius: 50%;
  142. background-color: #fff;
  143. box-shadow: inset 0 0 0 1px #fefefe;
  144. &:active {
  145. // opacity: 0.8;
  146. }
  147. }
  148. </style>