index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 && !onlyBack, 'left-btns_only-back': onlyBack }">
  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. onlyBack: { type: Boolean, default: false }, // 仅显示返回键,不显示首页
  47. warn: { type: Boolean, default: false }, // 警告
  48. shadow: { type: Boolean, default: false }, // 警告
  49. })
  50. const slots = useSlots()
  51. // 是否显示返回首页
  52. const getIsHome = computed(() => {
  53. const pages = getCurrentPages(); // 获取路由栈
  54. return pages.length >= 2
  55. })
  56. // 是否显示右侧按钮
  57. const ifShowRight = computed(() => slots.right || props.warn)
  58. //
  59. const statusBarHeight = computed(() => systemInfo.statusBarHeight) // 状态栏高度
  60. const getNavWrapStyle = computed(() => {
  61. const { windowWidth } = systemInfo
  62. const { top, right, height } = menuButtonInfo
  63. return {
  64. padding: `${top - unref(statusBarHeight)}px ${windowWidth - right}px`,
  65. height: `${height + (top - unref(statusBarHeight)) * 2}px`
  66. }
  67. })
  68. const getNavBarContent = computed(() => {
  69. const { width } = menuButtonInfo
  70. const style = {}
  71. if(unref(ifShowRight)){
  72. style.paddingRight = width + 'px'
  73. }else{
  74. style.position = 'absolute'
  75. style.width = '100%'
  76. style.top = '0'
  77. style.bottom = '0'
  78. style.zIndex = '-1'
  79. style.padding = `0 ${width + 10}px`
  80. }
  81. return style
  82. })
  83. function go(type){
  84. switch (type){
  85. case 'back':
  86. uni.navigateBack()
  87. break;
  88. case 'home':
  89. uni.reLaunch({
  90. url: '/pages/index/index'
  91. })
  92. break;
  93. default:
  94. uni.navigateTo({ url: type })
  95. break;
  96. }
  97. }
  98. </script>
  99. <style scoped lang="scss">
  100. .wrap{
  101. position: sticky;
  102. z-index: 101;
  103. &-shadow{
  104. box-shadow: 0 1rpx 0 2rpx #F4F6FA;
  105. }
  106. .nav-bar-wrap{
  107. padding:4px 8px;
  108. height:40px;
  109. .nav-bar{
  110. height: 100%;
  111. position: relative;
  112. &_left{
  113. margin-right: 30rpx;
  114. flex: none;
  115. .left-btns{
  116. border-radius: 99rpx;
  117. background: #DDDDDD80;
  118. width: 182rpx;
  119. height: 100%;
  120. display: flex;
  121. align-items: center;
  122. &_home{
  123. width: 90rpx;
  124. .back{
  125. display: none !important;
  126. }
  127. }
  128. &_only-back{
  129. width: 90rpx;
  130. .home{
  131. display: none !important;
  132. }
  133. .back::after{
  134. display: none;
  135. }
  136. }
  137. .back, .home{
  138. flex: 1;
  139. width: 0;
  140. height: 100%;
  141. display: flex;
  142. justify-content: center;
  143. align-items: center;
  144. }
  145. .back{
  146. position: relative;
  147. &::after{
  148. position: absolute;
  149. right: 0;
  150. top: 50%;
  151. transform: translateY(-50%);
  152. content: ' ';
  153. display: block;
  154. clear: both;
  155. background: #CECECE80;
  156. width: 2rpx;
  157. height: 34rpx;
  158. }
  159. image{
  160. width: 18rpx;
  161. height: 32rpx;
  162. }
  163. }
  164. .home{
  165. image{
  166. width: 36rpx;
  167. height: 32rpx;
  168. }
  169. }
  170. }
  171. }
  172. &_content{
  173. &.right-slot{
  174. .nav-bar_content_title{
  175. text-align: left !important;
  176. }
  177. }
  178. &_title{
  179. text-align: center;
  180. font-size: 36rpx;
  181. color: #fff;
  182. // background: red;
  183. }
  184. &_right{
  185. flex: none;
  186. display: flex;
  187. align-items: center;
  188. margin: 0 26rpx;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. </style>