custom-tabbar.uvue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="custom-tabbar">
  3. <view v-for="(item, index) in tabList" :key="index" class="tab-item" @click="handleTabClick(index)">
  4. <image class="tab-icon" :src="currentIndex == index ? item.selectedIconPath : item.iconPath" mode="aspectFit"></image>
  5. <text :class="['tab-text', currentIndex == index ? 'tab-text-active' : '']">
  6. {{ item.text }}
  7. </text>
  8. </view>
  9. </view>
  10. </template>
  11. <script setup lang="uts">
  12. import { ref } from 'vue'
  13. // Props
  14. type Props = {
  15. current?: number
  16. hide0:number
  17. hide1:number
  18. hide2:number
  19. hide3:number
  20. }
  21. const props = withDefaults(defineProps<Props>(), {
  22. current: 0,
  23. hide0:1,
  24. hide1:1,
  25. hide2:1,
  26. hide3:1
  27. })
  28. const hide0 = ref<number>(props.hide0)
  29. const hide1 = ref<number>(props.hide1)
  30. const hide2 = ref<number>(props.hide2)
  31. const hide3 = ref<number>(props.hide3)
  32. // Tab 列表配置
  33. type TabItem = {
  34. pagePath: string
  35. text: string
  36. iconPath: string
  37. selectedIconPath: string
  38. }
  39. const tabList: TabItem[] = [];
  40. const tabList1: TabItem[] = [
  41. {
  42. pagePath: '/pages/index/index',
  43. text: '首页',
  44. iconPath: '/static/images/custom-tabbar/11.png',
  45. selectedIconPath: '/static/images/custom-tabbar/22.png'
  46. },
  47. {
  48. pagePath: '/pages/order/index',
  49. text: '工单',
  50. iconPath: '/static/images/custom-tabbar/33.png',
  51. selectedIconPath: '/static/images/custom-tabbar/44.png'
  52. },
  53. {
  54. pagePath: '/pages/worktime/index',
  55. text: '工时',
  56. iconPath: '/static/images/custom-tabbar/77.png',
  57. selectedIconPath: '/static/images/custom-tabbar/88.png'
  58. },
  59. {
  60. pagePath: '/pages/score/index',
  61. text: '工分',
  62. iconPath: '/static/images/custom-tabbar/55.png',
  63. selectedIconPath: '/static/images/custom-tabbar/66.png'
  64. }
  65. ]
  66. const hideValues = [props.hide0, props.hide1, props.hide2, props.hide3]
  67. for (let i = 0; i < tabList1.length; i++) {
  68. if (i < hideValues.length && hideValues[i] === 1) {
  69. // hide 值为 1 时显示,添加到 tabList
  70. tabList.push(tabList1[i])
  71. }
  72. }
  73. // 当前选中索引
  74. const currentIndex = ref<number>(props.current)
  75. // Tab 点击事件
  76. const handleTabClick = (index: number): void => {
  77. if (index == currentIndex.value) {
  78. return
  79. }
  80. currentIndex.value = index
  81. const item = tabList[index]
  82. uni.redirectTo({
  83. url: item.pagePath
  84. })
  85. }
  86. </script>
  87. <style lang="scss">
  88. .custom-tabbar {
  89. position: fixed;
  90. bottom: 0;
  91. left: 0;
  92. right: 0;
  93. z-index: 1000;
  94. height: 150rpx;
  95. background-color: #ffffff;
  96. border-top: 1rpx solid #e5e5e5;
  97. flex-direction: row;
  98. justify-content: space-around;
  99. align-items: center;
  100. padding-bottom: env(safe-area-inset-bottom);
  101. }
  102. .tab {
  103. &-item {
  104. flex: 1;
  105. justify-content: center;
  106. align-items: center;
  107. }
  108. &-icon {
  109. width: 48rpx;
  110. height: 48rpx;
  111. margin-bottom: 8rpx;
  112. }
  113. &-text {
  114. font-size: 24rpx;
  115. color: #666666;
  116. &-active {
  117. color: #0081ff;
  118. }
  119. }
  120. }
  121. </style>