BottomNav.vue 5.45 KB
<!--
 * @Date: 2025-08-27 17:44:10
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-09-22 15:12:13
 * @FilePath: /map-demo/src/components/BottomNav.vue
 * @Description: 文件描述
-->
<template>
  <div v-if="isMiniProgramWebView" class="bottom-nav">
    <div
      v-for="item in navItems"
      :key="item.path"
      @click="navigate(item.path)"
      :class="['nav-item', isActive(item.path) ? 'nav-item-active' : 'nav-item-inactive']"
    >
      <img :src="isActive(item.path) ? item.activeIcon : item.icon" class="nav-icon" alt="" />
      <span class="nav-label">{{ item.label }}</span>
      <!-- <div v-if="isActive(item.path)" class="nav-indicator"></div> -->
    </div>
  </div>
</template>

<script setup>
import { computed, shallowRef } from 'vue';
import wx from 'weixin-js-sdk'

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';
/**
 * 检测是否在小程序web-view环境中
 * @returns {boolean} 是否在小程序环境
 */
const isMiniProgramWebView = computed(() => {
  return navigator.userAgent.includes('miniProgram');
});

const navItems = shallowRef([
  { path: '/pages/Dashboard/index', icon: homeIcon, activeIcon: homeIconActive, label: '首页' },
  { path: '/pages/ActivitiesCover/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 : '';
  // 默认选中活动页面
  return '/pages/ActivitiesCover/index';
});

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

const navigate = (path) => {
  wx.miniProgram.reLaunch({ url: path });
};
</script>

<style scoped>
/* 底部导航容器 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: white;
  border-top: 1px solid #f3f4f6;
  display: flex;
  justify-content: space-around;
  padding: 16rpx 0;
  z-index: 50;
  /* 基础高度适配 */
  min-height: 120rpx;
}

/* 导航项 */
.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16rpx 40rpx;
  position: relative;
  cursor: pointer;
  transition: all 0.2s ease;
}

/* 激活状态 */
.nav-item-active {
  color: #54ABAE;
}

/* 非激活状态 */
.nav-item-inactive {
  color: #9CA3AF;
}

/* 图标样式 */
.nav-icon {
  width: 52rpx;
  height: 52rpx;
  transition: transform 0.2s ease;
}

/* 标签样式 */
.nav-label {
  font-size: 26rpx;
  margin-top: 8rpx;
  line-height: 1.2;
}

/* 激活指示器 */
.nav-indicator {
  position: absolute;
  bottom: -8rpx;
  width: 80rpx;
  height: 8rpx;
  background-color: #2563eb;
  border-radius: 4rpx 4rpx 0 0;
}

/* 响应式适配 - 小屏设备 */
@media screen and (max-width: 480px) {
  .bottom-nav {
    padding: 8px 0;
    min-height: 60px;
  }

  .nav-item {
    padding: 8px 16px;
  }

  .nav-icon {
    width: 26px;
    height: 26px;
  }

  .nav-label {
    font-size: 13px;
    margin-top: 4px;
  }

  .nav-indicator {
    bottom: -4px;
    width: 40px;
    height: 4px;
    border-radius: 2px 2px 0 0;
  }
}

/* 响应式适配 - 中等屏幕设备 (平板竖屏) */
@media screen and (min-width: 481px) and (max-width: 768px) {
  .bottom-nav {
    padding: 12px 0;
    min-height: 80px;
  }

  .nav-item {
    padding: 12px 24px;
  }

  .nav-icon {
    width: 53px;
    height: 53px;
  }

  .nav-label {
    font-size: 24px;
    margin-top: 6px;
  }

  .nav-indicator {
    bottom: -6px;
    width: 48px;
    height: 5px;
    border-radius: 3px 3px 0 0;
  }
}

/* 响应式适配 - 大屏设备 (平板横屏/桌面) */
@media screen and (min-width: 769px) {
  .bottom-nav {
    padding: 16px 0;
    min-height: 100px;
  }

  .nav-item {
    padding: 16px 32px;
  }

  .nav-icon {
    width: 62px;
    height: 62px;
  }

  .nav-label {
    font-size: 27px;
    margin-top: 8px;
  }

  .nav-indicator {
    bottom: -8px;
    width: 60px;
    height: 6px;
    border-radius: 3px 3px 0 0;
  }
}

/* 微信小程序环境适配 */
@media screen and (min-device-width: 1024px) {
  .bottom-nav {
    padding: 20rpx 0;
    min-height: 140rpx;
  }

  .nav-item {
    padding: 20rpx 50rpx;
  }

  .nav-icon {
    width: 104rpx;
    height: 104rpx;
  }

  .nav-label {
    font-size: 55rpx;
    margin-top: 10rpx;
  }

  .nav-indicator {
    bottom: -10rpx;
    width: 90rpx;
    height: 10rpx;
    border-radius: 5rpx 5rpx 0 0;
  }
}

/* 悬浮效果 - 仅在非触摸设备上启用 */
@media (hover: hover) {
  .nav-item:hover {
    transform: translateY(-2px);
  }

  .nav-item:hover .nav-icon {
    transform: scale(1.1);
  }
}
</style>