index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="wrap" :class="{'wrap-shadow': shadow}">
  3. <StatusBar />
  4. <view class="nav-bar-wrap" :style="[getNavWrapStyle]">
  5. <!-- 状态栏高度 -->
  6. <view class="nav-bar flex">
  7. <view class="nav-bar_left" v-if="!hideLeft">
  8. <slot name='left'>
  9. <view class="left-btns" :class="{ 'left-btns_home': !getIsHome}">
  10. <view class="back" hover-class="hover-active" @click="go('back')">
  11. <image src="https://cdn.aliyinba.com/UploadFiles/shop2/number/nav_bar/nav-back.png" mode="aspectFill"></image>
  12. </view>
  13. <view class="home" hover-class="hover-active" @click="go('home')">
  14. <image src="https://cdn.aliyinba.com/UploadFiles/shop2/number/nav_bar/nav-home.png" mode="aspectFill"></image>
  15. </view>
  16. </view>
  17. </slot>
  18. </view>
  19. <view class="nav-bar_content flex flex-center flex-1 w-0" :class="{'right-slot': ifShowRight}" :style="[getNavBarContent]">
  20. <view class="nav-bar_content_title flex-1 w-0 ellipsis">
  21. <slot name="title">{{ title }}</slot>
  22. </view>
  23. <view class="nav-bar_content_right" v-if="ifShowRight">
  24. <slot name="right">
  25. <text>右侧插槽</text>
  26. <!-- <image class="warn-icon" :src="getFileUrl('/fault.png')" mode="aspectFill"></image> -->
  27. </slot>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script setup>
  35. import { computed, unref, useSlots } from 'vue'
  36. import StatusBar from "./StatusBar.vue"
  37. import { getFileUrl } from "@/utils/file.js";
  38. const systemInfo = uni.getWindowInfo();
  39. let menuButtonInfo = {}
  40. // #ifdef MP-WEIXIN
  41. menuButtonInfo = uni.getMenuButtonBoundingClientRect() // 右上角胶囊信息
  42. // #endif
  43. const props = defineProps({
  44. title: { type: String },
  45. hideLeft: { type: Boolean, default: false },
  46. warn: { type: Boolean, default: false }, // 警告
  47. shadow: { type: Boolean, default: false }, // 警告
  48. })
  49. const slots = useSlots()
  50. // 是否显示返回首页
  51. const getIsHome = computed(() => {
  52. const pages = getCurrentPages(); // 获取路由栈
  53. return pages.length >= 2
  54. })
  55. // 是否显示右侧按钮
  56. const ifShowRight = computed(() => slots.right || props.warn)
  57. //
  58. const statusBarHeight = computed(() => systemInfo.statusBarHeight) // 状态栏高度
  59. const getNavWrapStyle = computed(() => {
  60. const { windowWidth } = systemInfo
  61. const { top, right, height } = menuButtonInfo
  62. return {
  63. padding: `${top - unref(statusBarHeight)}px ${windowWidth - right}px`,
  64. height: `${height + (top - unref(statusBarHeight)) * 2}px`
  65. }
  66. })
  67. const getNavBarContent = computed(() => {
  68. const { width } = menuButtonInfo
  69. const style = {}
  70. if(unref(ifShowRight)){
  71. style.paddingRight = width + 'px'
  72. }else{
  73. style.position = 'absolute'
  74. style.width = '100%'
  75. style.top = '0'
  76. style.bottom = '0'
  77. style.zIndex = '-1'
  78. style.padding = `0 ${width + 10}px`
  79. }
  80. return style
  81. })
  82. function go(type){
  83. switch (type){
  84. case 'back':
  85. uni.navigateBack()
  86. break;
  87. case 'home':
  88. uni.reLaunch({
  89. url: '/pages/index/index'
  90. })
  91. break;
  92. default:
  93. uni.navigateTo({ url: type })
  94. break;
  95. }
  96. }
  97. </script>
  98. <style scoped lang="scss">
  99. .wrap{
  100. position: sticky;
  101. z-index: 101;
  102. &-shadow{
  103. box-shadow: 0 1rpx 0 2rpx #F4F6FA;
  104. }
  105. .nav-bar-wrap{
  106. padding:4px 8px;
  107. height:40px;
  108. .nav-bar{
  109. height: 100%;
  110. position: relative;
  111. &_left{
  112. margin-right: 30rpx;
  113. flex: none;
  114. .left-btns{
  115. border-radius: 99rpx;
  116. background: #DDDDDD80;
  117. width: 182rpx;
  118. height: 100%;
  119. display: flex;
  120. align-items: center;
  121. &_home{
  122. width: 90rpx;
  123. .back{
  124. display: none !important;
  125. }
  126. }
  127. .back, .home{
  128. flex: 1;
  129. width: 0;
  130. height: 100%;
  131. display: flex;
  132. justify-content: center;
  133. align-items: center;
  134. }
  135. .back{
  136. position: relative;
  137. &::after{
  138. position: absolute;
  139. right: 0;
  140. top: 50%;
  141. transform: translateY(-50%);
  142. content: ' ';
  143. display: block;
  144. clear: both;
  145. background: #CECECE80;
  146. width: 2rpx;
  147. height: 34rpx;
  148. }
  149. image{
  150. width: 18rpx;
  151. height: 32rpx;
  152. }
  153. }
  154. .home{
  155. image{
  156. width: 36rpx;
  157. height: 32rpx;
  158. }
  159. }
  160. }
  161. }
  162. &_content{
  163. &.right-slot{
  164. .nav-bar_content_title{
  165. text-align: left !important;
  166. }
  167. }
  168. &_title{
  169. text-align: center;
  170. font-size: 36rpx;
  171. color: #fff;
  172. // background: red;
  173. }
  174. &_right{
  175. flex: none;
  176. display: flex;
  177. align-items: center;
  178. margin: 0 26rpx;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. </style>