BottomNav.vue 2.95 KB
<!--
 * @Date: 2025-08-27 17:44:10
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-09-22 15:06:37
 * @FilePath: /lls_program/src/components/BottomNav.vue
 * @Description: 文件描述
-->
<template>
  <view
    class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 flex justify-around py-2 z-50"
  >
    <view
      v-for="item in navItems"
      :key="item.path"
      @click="navigate(item.path)"
      :class="[
        'flex flex-col items-center py-2 px-5',
        isActive(item.path) ? 'icon-text-color-active' : 'icon-text-color-default',
      ]"
    >
      <image :src="isActive(item.path) ? item.activeIcon : item.icon" class="w-6 h-6" />
      <span class="text-xs mt-1">{{ item.label }}</span>
      <!-- <view v-if="isActive(item.path)" class="absolute bottom-0 w-10 h-1 bg-blue-600 rounded-t-full"></view> -->
    </view>
  </view>
</template>

<script setup>
import { computed, shallowRef } from 'vue'
import Taro from '@tarojs/taro'
//
const homeIcon = 'https://cdn.ipadbiz.cn/lls_prog/icon/home.svg'
const homeIconActive = 'https://cdn.ipadbiz.cn/lls_prog/icon/home_active1.svg'
const rewardsIcon = 'https://cdn.ipadbiz.cn/lls_prog/icon/rewards.svg'
const rewardsIconActive = 'https://cdn.ipadbiz.cn/lls_prog/icon/rewards_active1.svg'
const activitiesIcon = 'https://cdn.ipadbiz.cn/lls_prog/icon/activities.svg'
const activitiesIconActive = 'https://cdn.ipadbiz.cn/lls_prog/icon/activities_active1.svg'
const meIcon = 'https://cdn.ipadbiz.cn/lls_prog/icon/me.svg'
const meIconActive = 'https://cdn.ipadbiz.cn/lls_prog/icon/me_active1.svg'

const navItems = shallowRef([
  { path: '/pages/Dashboard/index', icon: homeIcon, activeIcon: homeIconActive, label: '首页' },
  {
    path: '/pages/CheckinMap/index',
    icon: activitiesIcon,
    activeIcon: activitiesIconActive,
    label: '便民地图',
  },
  // { path: '/pages/RewardCategories/index', icon: rewardsIcon, activeIcon: rewardsIconActive, label: '兑换' },
  // TAG: 暂时写死以后可能会改变
  {
    path: '/pages/Rewards/index?id=health&category=health',
    icon: rewardsIcon,
    activeIcon: rewardsIconActive,
    label: '兑换',
  },
  { path: '/pages/Profile/index', icon: meIcon, activeIcon: meIconActive, label: '我的' },
])

const currentPage = computed(() => {
  const pages = Taro.getCurrentPages()
  return pages.length > 0 ? '/' + pages[pages.length - 1].route : ''
})

const isActive = path => {
  return path.includes(currentPage.value)
}

const navigate = path => {
  // 获取当前页面栈
  const pages = Taro.getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const currentRoute = currentPage?.route || ''
  // 在 Welcome 页面屏蔽掉跳转按钮
  if (currentRoute === 'pages/Welcome/index' && path === '/pages/Dashboard/index') {
    return
  } else {
    Taro.redirectTo({ url: path })
  }
}
</script>

<style lang="less">
.icon-text-color-active {
  color: var(--color-primary);
}
.icon-text-color-default {
  color: var(--color-text);
}
</style>