index.vue 4.1 KB

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