index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view
  3. class="navbar"
  4. :class="{'gradientBgc': !noBgColor}"
  5. :style="{
  6. 'height': height + 'px',
  7. 'line-height': height + 'px',
  8. }"
  9. >
  10. <view class="navbar-box" :style="{'paddingTop': statusBarHeight + 'px'}">
  11. <image
  12. v-if="showLogo"
  13. src="https://minio.menduner.com/dev/fe9890be9b1176f84f2aa282b0f6adce300b133f65eb3d7b45ae057aa5698324.png"
  14. class="navbar-box-logo"
  15. :style="{'height': defaultLogoHeight + 'px'}"
  16. ></image>
  17. <view v-else class="nav-box-title" :style="{'height': height ? (height - statusBarHeight) + 'px' : '100%'}">
  18. <uni-icons v-if="!textLeft" :type="pages && pages.length > 1 ? 'left' : 'home'" size="23" class="ss-m-l-20" color="#0E100F" @tap="goBack"></uni-icons>
  19. <view class="title-text" :class="[{'text-left': textLeft}, {'text-center': !textLeft}]">{{ title }}</view>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script setup>
  25. import { ref } from 'vue'
  26. import { onLoad } from '@dcloudio/uni-app'
  27. defineProps({
  28. title: {
  29. type: String,
  30. default: '门墩儿'
  31. },
  32. showLogo: {
  33. type: Boolean,
  34. default: false
  35. },
  36. // 是否需要渐变背景
  37. noBgColor: {
  38. type: Boolean,
  39. default: false
  40. },
  41. // 标题所在位置
  42. textLeft: {
  43. type: Boolean,
  44. default: true
  45. }
  46. })
  47. const defaultLogoHeight = 40
  48. const navbarHeight = ref(0)
  49. const statusBarHeight = ref(0)
  50. const height = ref(0)
  51. const pages = ref([])
  52. onLoad(async () => {
  53. pages.value = getCurrentPages() // 获取当前页面栈的实例数组
  54. try {
  55. const systemInfo = uni.getSystemInfoSync()
  56. statusBarHeight.value = systemInfo.statusBarHeight
  57. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  58. // 计算导航栏高度
  59. navbarHeight.value = menuButtonInfo.height + 2 * (menuButtonInfo.top - statusBarHeight.value) + statusBarHeight.value
  60. // 确保导航栏高度不小于 logo 高度 + 10
  61. if (navbarHeight.value < defaultLogoHeight + 10) {
  62. navbarHeight.value = defaultLogoHeight + 10
  63. }
  64. height.value = navbarHeight.value < defaultLogoHeight ? (defaultLogoHeight + 10) : navbarHeight.value + 10
  65. await new Promise((resolve, reject) => {
  66. uni.setStorage({
  67. key: 'navbarHeight',
  68. data: height.value,
  69. success: resolve,
  70. fail: reject
  71. })
  72. })
  73. } catch (error) {
  74. console.error('获取系统信息或菜单按钮信息时出错:', error)
  75. }
  76. })
  77. // 返回上一页或首页
  78. const goBack = () => {
  79. if (pages.value.length > 1) {
  80. uni.navigateBack({
  81. delta: 1,
  82. fail: () => {
  83. console.error('返回上一页失败,尝试跳转到首页')
  84. uni.reLaunch({
  85. url: '/pages/index/position',
  86. fail: (err) => {
  87. console.error('跳转到首页也失败:', err)
  88. uni.showToast({
  89. title: '导航失败,请稍后重试',
  90. icon: 'none'
  91. })
  92. }
  93. })
  94. }
  95. })
  96. } else {
  97. uni.reLaunch({
  98. url: '/pages/index/position',
  99. fail: (err) => {
  100. console.error('重新加载首页失败:', err)
  101. uni.showToast({
  102. title: '重新加载首页失败',
  103. icon: 'none'
  104. })
  105. }
  106. })
  107. }
  108. }
  109. </script>
  110. <style scoped lang="scss">
  111. .gradientBgc {
  112. background: linear-gradient(180deg, #9bfece, #BCFEDE);
  113. }
  114. .navbar {
  115. position: fixed;
  116. top: 0;
  117. left: 0;
  118. width: 100%;
  119. z-index: 999;
  120. &-box {
  121. position: relative;
  122. width: 100%;
  123. height: 100%;
  124. &-logo {
  125. position: absolute;
  126. left: 10px;
  127. width: 95px;
  128. }
  129. }
  130. }
  131. .nav-box-title {
  132. position: relative;
  133. width: 100%;
  134. display: flex;
  135. align-items: center;
  136. .title-text {
  137. position: absolute;
  138. width: 158px;
  139. height: 51px;
  140. line-height: 51px;
  141. color: #0E100F;
  142. font-style: normal;
  143. text-transform: none;
  144. }
  145. }
  146. .text-left {
  147. left: 15px;
  148. font-family: MiSans-Semibold;
  149. font-weight: 600;
  150. font-size: 38rpx;
  151. }
  152. .text-center {
  153. left: 50%;
  154. transform: translateX(-50%);
  155. font-family: MiSans-Medium;
  156. font-size: 33rpx;
  157. }
  158. </style>