index.vue 4.9 KB

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