cwg-header.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <uni-nav-bar :leftWidth="0" :rightWidth="0" :statusBar="true" :fixed="true" :height="60"
  3. :backgroundColor="props.backgrounds || '#fff'" :border="false" :a="props.backgrounds">
  4. <view class="pages-header">
  5. <view class="header">
  6. <view v-if="showBack" class="back">
  7. <cwg-icon name="dropdown" class="back-icon" color="#000" @click="handleBack" />
  8. </view>
  9. <view>{{ headerTitleReady ? headerTitle : '' }}</view>
  10. </view>
  11. <view class="header-r">
  12. <slot></slot>
  13. </view>
  14. </view>
  15. </uni-nav-bar>
  16. </template>
  17. <script setup lang="ts">
  18. import useRouter from "@/hooks/useRouter";
  19. import useRoute from '@/hooks/useRoute'
  20. import { computed } from 'vue'
  21. import { useI18n } from 'vue-i18n'
  22. const { t } = useI18n()
  23. const router = useRouter()
  24. const route = useRoute()
  25. const showBack = computed(() => {
  26. // 不显示返回按钮的页面
  27. const noBackPages = [
  28. '/',
  29. '/pages/customer/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.mine.info'
  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. backgrounds: Record<string, any>;
  66. }>();
  67. function handleBack() {
  68. router.back();
  69. }
  70. </script>
  71. <style scoped lang="scss">
  72. @import "@/uni.scss";
  73. .pages-header {
  74. width: 100%;
  75. z-index: 1111;
  76. display: flex;
  77. align-items: center;
  78. justify-content: space-between;
  79. box-sizing: border-box;
  80. }
  81. .header {
  82. position: relative;
  83. text-align: left;
  84. font-size: var(--font-size-22);
  85. color: var(--white);
  86. font-weight: 700;
  87. margin: px2rpx(10) 0;
  88. height: px2rpx(40);
  89. display: flex;
  90. align-items: center;
  91. line-height: px2rpx(40);
  92. .back {
  93. display: flex;
  94. align-items: center;
  95. justify-content: center;
  96. width: px2rpx(24);
  97. height: px2rpx(24);
  98. border-radius: 50%;
  99. margin-right: px2rpx(20);
  100. }
  101. .back-icon {
  102. line-height: px2rpx(71);
  103. font-size: var(--font-size-20);
  104. cursor: pointer;
  105. }
  106. }
  107. .wallet-header {
  108. .header {
  109. color: #fff;
  110. }
  111. }
  112. </style>