shopro-tabbar.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="shopro-tabbar-wrap" v-if="tabbarList && tabbarList.length && showTabbar">
  3. <view class="tabbar-box" :style="{ background: tabbarData.bgcolor || '#fff' }">
  4. <view class="tabbar-item" v-for="(tab, index) in tabbarList" :key="tab.name" @tap="switchTabbar(tab, index)">
  5. <view class="img-box">
  6. <image
  7. class="tabbar-icon"
  8. v-if="tabbarData.style == 1 || tabbarData.style == 2"
  9. :src="currentPath == tab.path ? tab.activeImage : tab.image"
  10. mode="aspectFill"
  11. ></image>
  12. <!-- 购物车角标 -->
  13. <view v-if="tab.path == '/pages/index/cart' && cartNum" class="badge">{{ cartNum }}</view>
  14. </view>
  15. <view
  16. class="tabbar-text"
  17. v-if="tabbarData.style == 1 || tabbarData.style == 3"
  18. :style="{ color: currentPath == tab.path ? tabbarData.activeColor : tabbarData.color }"
  19. >
  20. {{ tab.name }}
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. /**
  28. * 自定义底部导航
  29. * @property {Array} tabbarList - vuex初始化的底部导航数据
  30. * @property {String} currentPath -computed解析当前页面路径。
  31. */
  32. import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
  33. import { router } from '@/shopro/router';
  34. export default {
  35. name: 'shoproTabbar',
  36. components: {},
  37. data() {
  38. return {
  39. currentPath: '' //当前页面路径
  40. };
  41. },
  42. mounted() {
  43. this.currentPath = this.$Route.path === '/pages/index/index' ? this.$Route.path : this.$Route.fullPath;
  44. },
  45. computed: {
  46. ...mapGetters(['cartNum', 'tabbarData']),
  47. // 底部导航栏列表
  48. tabbarList() {
  49. if (this.tabbarData) {
  50. return this.tabbarData.list;
  51. }
  52. },
  53. // 后台tabbarList数据中必需含有'/pages/index/index',不然逻辑混乱
  54. showTabbar() {
  55. if (this.tabbarData && this.tabbarData.list) {
  56. let arr = ['/pages/index/index'];
  57. let path = '';
  58. for (let item of this.tabbarData.list) {
  59. arr.push(item.path);
  60. }
  61. return arr.includes(this.currentPath);
  62. }
  63. }
  64. },
  65. methods: {
  66. // 切换tabbar
  67. switchTabbar(tab, index) {
  68. this.$tools.routerTo(tab.path, true);
  69. }
  70. }
  71. };
  72. </script>
  73. <style lang="scss">
  74. .shopro-tabbar-wrap {
  75. height: calc(100rpx + env(safe-area-inset-bottom) / 2);
  76. padding-bottom: calc(env(safe-area-inset-bottom) / 2);
  77. position: relative;
  78. width: 100%;
  79. z-index: 70;
  80. .tabbar-box {
  81. position: fixed;
  82. display: flex;
  83. align-items: center;
  84. width: 100%;
  85. height: calc(100rpx + env(safe-area-inset-bottom) / 2);
  86. border-top: 1rpx solid #eeeeee;
  87. background-color: #fff;
  88. z-index: 998;
  89. bottom: 0;
  90. padding-bottom: calc(env(safe-area-inset-bottom) / 2);
  91. .tabbar-item {
  92. height: 100%;
  93. display: flex;
  94. flex-direction: column;
  95. align-items: center;
  96. justify-content: center;
  97. flex: 1;
  98. .img-box {
  99. position: relative;
  100. .badge {
  101. position: absolute;
  102. /* #ifndef APP-NVUE */
  103. display: inline-flex;
  104. /* #endif */
  105. justify-content: center;
  106. align-items: center;
  107. line-height: 24rpx;
  108. padding: 4rpx 10rpx;
  109. border-radius: 100rpx;
  110. color: #fff;
  111. font-size: 24rpx;
  112. z-index: 9;
  113. background-color: $u-type-error;
  114. transform: scale(0.8);
  115. transform-origin: center center;
  116. top: 0;
  117. left: 46rpx;
  118. white-space: nowrap;
  119. }
  120. }
  121. .tabbar-icon {
  122. width: 50rpx;
  123. height: 50rpx;
  124. display: block;
  125. }
  126. .tabbar-text {
  127. font-size: 20rpx;
  128. margin-top: 4rpx;
  129. }
  130. }
  131. }
  132. }
  133. </style>