cwg-header.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="pages-header" :style="{ paddingTop: statusBarHeight + 'px' }">
  3. <view class="header">
  4. <view v-if="showBack" class="back">
  5. <cwg-icon name="dropdown" class="back-icon" :color="color" @click="handleBack" />
  6. </view>
  7. <view>{{ headerTitleReady ? headerTitle : '' }}</view>
  8. </view>
  9. <view class="header-r">
  10. <slot></slot>
  11. </view>
  12. </view>
  13. </template>
  14. <script setup lang="ts">
  15. import useRouter from "@/hooks/useRouter";
  16. import useRoute from '@/hooks/useRoute'
  17. import { computed, onMounted } from 'vue'
  18. import { useI18n } from 'vue-i18n'
  19. import useGlobalStore from '@/stores/use-global-store'
  20. const { t } = useI18n()
  21. const router = useRouter()
  22. const route = useRoute()
  23. const globalStore = useGlobalStore()
  24. const statusBarHeight = computed(() => globalStore.statusBarHeight);
  25. const showBack = computed(() => {
  26. // 不显示返回按钮的页面
  27. const noBackPages = [
  28. '/',
  29. '/pages/card/index',
  30. '/pages/wallet/index',
  31. '/pages/mine/index',
  32. '/pages/login/index'
  33. ]
  34. return !noBackPages.includes(route.path)
  35. })
  36. function slashToDash(str: string) {
  37. return str.replace(/\//g, '.');
  38. }
  39. function getTranslationKey(): string {
  40. const path = route.path
  41. let key = ''
  42. if (!path) return ''
  43. if (path === '/') {
  44. key = 'pages.card.index'
  45. } else if (path.startsWith()) {
  46. key = path.slice(7)
  47. } else {
  48. key = path.slice(1)
  49. }
  50. return slashToDash(key)
  51. }
  52. const headerTitleReady = computed(() => {
  53. if (props.title) return true
  54. if (!route.path) return false
  55. const key = getTranslationKey()
  56. return !!t(key)
  57. })
  58. const headerTitle = computed(() => {
  59. if (props.title) return props.title
  60. const key = getTranslationKey()
  61. return t(key) || ''
  62. })
  63. const props = defineProps<{
  64. title: string;
  65. color: string;
  66. }>();
  67. function handleBack() {
  68. router.back();
  69. }
  70. </script>
  71. <style scoped lang="scss">
  72. @import "@/uni.scss";
  73. .pages-header {
  74. position: fixed;
  75. top: 0;
  76. left: 0;
  77. width: 100%;
  78. background-color: #fff;
  79. z-index: 1111;
  80. display: flex;
  81. align-items: center;
  82. justify-content: space-between;
  83. padding: 0 px2rpx(24);
  84. box-sizing: border-box;
  85. .header {
  86. position: relative;
  87. text-align: left;
  88. font-size: var(--font-size-22);
  89. color: var(--white);
  90. font-weight: 700;
  91. margin: px2rpx(10) 0;
  92. height: px2rpx(40);
  93. display: flex;
  94. align-items: center;
  95. line-height: px2rpx(40);
  96. .back {
  97. display: flex;
  98. align-items: center;
  99. justify-content: center;
  100. width: px2rpx(24);
  101. height: px2rpx(24);
  102. border-radius: 50%;
  103. margin-right: px2rpx(20);
  104. }
  105. .back-icon {
  106. line-height: px2rpx(71);
  107. font-size: var(--font-size-20);
  108. cursor: pointer;
  109. }
  110. }
  111. }
  112. .wallet-header {
  113. .header {
  114. color: #fff;
  115. }
  116. }
  117. </style>