index.vue 5.2 KB

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