index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="tab-bar">
  3. <view
  4. v-for="(tab, index) in tabs"
  5. :key="index"
  6. class="tab-item"
  7. @click="switchTab(tab)"
  8. >
  9. <view class="tab-inner" :class="{ active: current === index }">
  10. <AppIcon
  11. :name="tab.icon"
  12. :size="current === index ? 22 : 22"
  13. :color="current === index ? activeColor : inactiveColor"
  14. />
  15. <text
  16. class="tab-text"
  17. :class="{ active: current === index }"
  18. >{{ tab.text }}</text>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script setup>
  24. import { ref, watch, onMounted } from 'vue'
  25. const props = defineProps({
  26. selected: {
  27. type: Number,
  28. default: 0
  29. }
  30. })
  31. const activeColor = '#C6171E'
  32. const inactiveColor = '#999999'
  33. const tabs = [
  34. { pagePath: '/pages/index/index', text: '首页', icon: 'home' },
  35. { pagePath: '/pages/order/list', text: '订单', icon: 'clipboard' },
  36. { pagePath: '/pages/device/list', text: '设备', icon: 'monitor' },
  37. { pagePath: '/pages/finance/index', text: '财务', icon: 'dollar' },
  38. ]
  39. const current = ref(props.selected)
  40. watch(() => props.selected, (val) => {
  41. current.value = val
  42. })
  43. onMounted(() => {
  44. const pages = getCurrentPages()
  45. if (pages.length > 0) {
  46. const route = '/' + pages[pages.length - 1].route
  47. const idx = tabs.findIndex(t => t.pagePath === route)
  48. if (idx !== -1) current.value = idx
  49. }
  50. })
  51. const switchTab = (tab) => {
  52. uni.switchTab({ url: tab.pagePath })
  53. }
  54. </script>
  55. <style scoped>
  56. .tab-bar {
  57. position: fixed;
  58. bottom: 0;
  59. left: 0;
  60. right: 0;
  61. display: flex;
  62. align-items: stretch;
  63. background: #FFFFFF;
  64. padding: 8rpx 12rpx;
  65. padding-bottom: calc(8rpx + env(safe-area-inset-bottom));
  66. box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.06);
  67. border-top: 1px solid #F0F0F0;
  68. z-index: 999;
  69. }
  70. .tab-item {
  71. flex: 1;
  72. display: flex;
  73. align-items: center;
  74. justify-content: center;
  75. }
  76. .tab-inner {
  77. display: flex;
  78. flex-direction: column;
  79. align-items: center;
  80. justify-content: center;
  81. gap: 4rpx;
  82. padding: 10rpx 24rpx;
  83. border-radius: 20rpx;
  84. transition: background 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  85. }
  86. .tab-inner.active {
  87. background: rgba(198, 23, 30, 0.08);
  88. }
  89. .tab-text {
  90. font-size: 20rpx;
  91. font-weight: 500;
  92. color: #999999;
  93. transition: color 0.25s;
  94. }
  95. .tab-text.active {
  96. color: #C6171E;
  97. font-weight: 600;
  98. }
  99. </style>