AppHeader.vue 1.59 KB
<!--
 * @Date: 2025-08-27 17:45:12
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-08-27 18:05:42
 * @FilePath: /lls_program/src/components/AppHeader.vue
 * @Description: 文件描述
-->
<template>
  <view class="relative px-4 py-5 flex items-center justify-center bg-white bg-opacity-70 backdrop-blur-md border-b border-gray-100">
    <button
      v-if="showBack && !isRootPath"
      @click="goBack"
      class="absolute left-4 p-2 rounded-full hover:bg-gray-100 transition-colors"
      aria-label="返回"
    >
      <Left size="24" />
    </button>
    <h1 class="text-xl font-medium">{{ title }}</h1>
    <button
      v-if="showHome && !isRootPath"
      @click="goHome"
      class="absolute right-4 p-2 rounded-full hover:bg-gray-100 transition-colors"
      aria-label="首页"
    >
      <Home size="24" />
    </button>
  </view>
</template>

<script setup>
import { computed } from 'vue';
import Taro from '@tarojs/taro';
import { Left, Home } from '@nutui/icons-vue-taro';

defineProps({
  title: {
    type: String,
    required: true
  },
  showBack: {
    type: Boolean,
    default: true
  },
  showHome: {
    type: Boolean,
    default: false
  }
});

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

const isRootPath = computed(() => {
  return currentPage.value === '/pages/index/index' || currentPage.value === '/pages/Dashboard/index';
});

const goBack = () => {
  Taro.navigateBack();
};

const goHome = () => {
  Taro.switchTab({ url: '/pages/index/index' });
};
</script>