cwg-header.vue 2.4 KB

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