nav-bar.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view>
  3. <view class="nav-bar-wrapper left-0 top-0 width100" :class="fixed ? 'position-f' : 'position-a'"
  4. :style="{background: background, borderColor: borderColor}">
  5. <view class="status-bar" :style="{height: `${statusBarHeight}px`}"></view>
  6. <view class="position-r">
  7. <view v-if="!custom" class="height100 width100 flex aic jcc">
  8. <view v-if="iconType !== 'none'"
  9. class="position-a flex aic jcc icon-wrapper height100 left-0 text-center"
  10. @click='leftIconClick'>
  11. <re-icon :color="iconColor" v-if="iconName" :name="iconName"
  12. :customStyle="{ fontSize: '40rpx', fontWeight: 'bold' }"></re-icon>
  13. </view>
  14. <text class="bold ellipsis title" :style="{color: titleColor}">{{titleCom}}</text>
  15. </view>
  16. <slot v-else></slot>
  17. </view>
  18. </view>
  19. <view v-if="!noPlaceholder" class="placeholder" :style="{height: `${navBarHeight}px`}"></view>
  20. </view>
  21. </template>
  22. <script>
  23. import {
  24. toRpx
  25. } from '@/utils/calculate'
  26. import {
  27. isEmpty
  28. } from '@/utils/check-types.js'
  29. export default {
  30. name: "nav-bar",
  31. props: {
  32. fixed: { // 是否是fixed定位
  33. type: Boolean,
  34. default: true
  35. },
  36. title: String, // 标题
  37. custom: Boolean, // 自定义
  38. iconType: { // back 返回 home 主页 none 无icon
  39. type: String,
  40. default: ''
  41. },
  42. icon: String, // 自定义icon
  43. iconColor: {
  44. type: String,
  45. default: '#152129',
  46. },
  47. background: { // 背景色
  48. type: String,
  49. default: '#fff'
  50. },
  51. borderColor: {
  52. type: String,
  53. default: '#eee'
  54. },
  55. titleColor: {
  56. type: String,
  57. default: '#333'
  58. },
  59. noPlaceholder: Boolean // 没有占位符
  60. },
  61. data() {
  62. return {
  63. isNonePage: false, // 是否有上一个页面
  64. navBarHeight: uni.getStorageSync('navBarHeight') // 导航高度
  65. }
  66. },
  67. computed: {
  68. // icon类型
  69. iconName() {
  70. if (this.icon) {
  71. return this.icon
  72. }
  73. if (this.iconType === 'back') {
  74. return 'icon-fanhui'
  75. }
  76. if (this.iconType === 'home') {
  77. return 'icon-home'
  78. }
  79. if (!this.icontype) {
  80. const pages = getCurrentPages() //获取加载的页面
  81. const currentPage = pages[pages.length - 2] //获取上个页面的对象
  82. if (currentPage && !currentPage.route.includes('flashScreen')) {
  83. return 'icon-fanhui'
  84. } else {
  85. return 'icon-home'
  86. }
  87. }
  88. },
  89. // 设置title
  90. titleCom() {
  91. if (this.title) {
  92. if (this.title === 'none') {
  93. return ''
  94. }
  95. return this.title
  96. } else {
  97. let fullPath = this.$Route.path,
  98. thisOne = ROUTES.find((item) => item.path == fullPath)
  99. return thisOne.style.navigationBarTitleText
  100. }
  101. },
  102. // 状态高度
  103. statusBarHeight() {
  104. return this.$store.state.info.systemInfo?.statusBarHeight
  105. },
  106. // 胶囊高度-小程序
  107. menuButtonHeight() {
  108. if (this.$platform !== 'MP-WEIXIN') {
  109. return 44
  110. } else {
  111. let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  112. if (menuButtonInfo && menuButtonInfo.height) {
  113. return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2
  114. } else {
  115. return 44
  116. }
  117. }
  118. },
  119. },
  120. mounted() {
  121. let cacheHeight = null
  122. if (this.$platform === 'APP-PLUS') {
  123. cacheHeight = this.navBarHeight?.data
  124. } else {
  125. cacheHeight = this.placeholderHeigh
  126. }
  127. if (cacheHeight) {
  128. return
  129. } else {
  130. this.$nextTick(() => {
  131. const navBarHeight = this.menuButtonHeight + this.statusBarHeight + 1 // 1border高度
  132. uni.setStorageSync('navBarHeight', navBarHeight)
  133. this.navBarHeight = navBarHeight
  134. })
  135. }
  136. },
  137. methods: {
  138. // 左侧图标点击
  139. leftIconClick() {
  140. if (this.iconName === 'icon-fanhui') {
  141. this.$Router.back()
  142. } else if (this.iconName === 'icon-home') {
  143. this.$Router.replaceAll({
  144. name: 'bus'
  145. })
  146. } else {
  147. this.$emit('iconHandle')
  148. }
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .nav-bar-wrapper {
  155. color: $uni-text-color;
  156. z-index: 9999;
  157. border-bottom: 1rpx solid #eee;
  158. .title {
  159. max-width: 400rpx;
  160. }
  161. }
  162. .icon-wrapper {
  163. left: 0;
  164. color: #666;
  165. width: 80rpx;
  166. }
  167. </style>