文件
NHCshopro/uniapp/components/shopro-tabbar/shopro-tabbar.vue
wikiboss f141d35f1c Add 'uniapp/' from commit '096e31448ca089004c78af69f2006c2b0324d8a4'
git-subtree-dir: uniapp
git-subtree-mainline: b87880e247
git-subtree-split: 096e31448c
2026-11-20 09:08:51 +08:00

256 行
5.8 KiB
Vue

<template>
<view class="shopro-tabbar-wrap" v-if="displayTabbar">
<view class="tabbar-box" :style="{ background: tabbarConfig.bgcolor || '#fff' }">
<view class="tabbar-item" v-for="(tab, index) in tabbarList" :key="tab.name + index"
@tap.stop="handleTabTap(index)">
<view class="img-box" v-if="showIcon">
<image class="tabbar-icon" :src="iconSrc(tab)" mode="aspectFit"
@error="onIconError(tab)"></image>
<view v-if="tab.path == '/pages/index/cart' && cartNum" class="badge">{{ cartNum }}</view>
</view>
<view class="tabbar-text" v-if="showText"
:style="{ color: isActive(tab) ? (tabbarConfig.activeColor || '#4C7AF3') : (tabbarConfig.color || '#333') }">
{{ tab.name }}
</view>
</view>
</view>
</view>
</template>
<script>
import { mapGetters } from 'vuex';
const DEFAULT_TABBAR = {
bgcolor: '#ffffff',
color: '#333333',
activeColor: '#4C7AF3',
style: 1,
list: [{
name: '首页',
path: '/pages/index/index',
image: '/static/images/tabbar/tabbar_home.png',
activeImage: '/static/images/tabbar/tabbar_home1.png'
},
{
name: '分类',
path: '/pages/index/category',
image: '/static/images/tabbar/tabbar_category.png',
activeImage: '/static/images/tabbar/tabbar_category1.png'
},
{
name: '购物车',
path: '/pages/index/cart',
image: '/static/images/tabbar/tabbar_cart.png',
activeImage: '/static/images/tabbar/tabbar_cart1.png'
},
{
name: '我的',
path: '/pages/index/user',
image: '/static/images/tabbar/tabbar_personal.png',
activeImage: '/static/images/tabbar/tabbar_personal1.png'
}
]
};
export default {
name: 'shoproTabbar',
data() {
return {
currentPath: '',
activeIconFallback: {}
};
},
computed: {
...mapGetters(['cartNum', 'tabbarData']),
useCustomTabbarData() {
return !!(this.tabbarData && this.tabbarData.list && this.tabbarData.list.length);
},
tabbarConfig() {
if (this.useCustomTabbarData) {
return this.tabbarData;
}
return DEFAULT_TABBAR;
},
tabbarList() {
return (this.tabbarConfig.list || []).filter(item => item && item.path);
},
tabbarStyle() {
return Number(this.tabbarConfig.style) || 1;
},
showIcon() {
return this.tabbarStyle === 1 || this.tabbarStyle === 2;
},
showText() {
return this.tabbarStyle === 1 || this.tabbarStyle === 3;
},
displayTabbar() {
if (!this.tabbarList.length) {
return false;
}
const current = this.normalizePath(this.currentPath);
const paths = this.tabbarList.map(item => this.normalizePath(item.path));
return paths.includes(current);
}
},
mounted() {
this.refreshTabbar();
},
// #ifdef MP-WEIXIN
pageLifetimes: {
show() {
this.updateCurrentPath();
}
},
// #endif
methods: {
normalizePath(path = '') {
return path.split('?')[0];
},
isActive(tab) {
if (!tab || !tab.path) {
return false;
}
return this.normalizePath(this.currentPath) === this.normalizePath(tab.path);
},
isLikelyIconUrl(url = '') {
if (!url || typeof url !== 'string') {
return false;
}
const value = url.trim();
if (!value || /^#([0-9a-fA-F]{3,8})$/.test(value)) {
return false;
}
return true;
},
resolveIconUrl(url = '') {
if (!this.isLikelyIconUrl(url)) {
return '';
}
const value = url.trim();
if (!this.useCustomTabbarData) {
return value;
}
return this.$tools.formatTabIconUrl(value);
},
iconSrc(tab) {
if (!tab) {
return '';
}
const pathKey = this.normalizePath(tab.path);
const normalIcon = this.resolveIconUrl(tab.image);
if (!this.isActive(tab) || this.activeIconFallback[pathKey]) {
return normalIcon;
}
const activeRaw = tab.activeImage || tab.active_image || '';
const activeIcon = this.resolveIconUrl(activeRaw);
if (activeIcon && activeIcon !== normalIcon) {
return activeIcon;
}
return normalIcon;
},
onIconError(tab) {
if (!tab || !tab.path || !this.isActive(tab)) {
return;
}
const pathKey = this.normalizePath(tab.path);
if (!this.activeIconFallback[pathKey]) {
this.$set(this.activeIconFallback, pathKey, true);
}
},
updateCurrentPath() {
const route = this.$Route || {};
this.currentPath = route.path === '/pages/index/index' ? route.path : (route.fullPath || route.path || '');
},
refreshTabbar() {
this.updateCurrentPath();
},
handleTabTap(index) {
const tab = this.tabbarList[index];
if (!tab || !tab.path) {
return;
}
this.$tools.routerTo(tab.path, true);
}
}
};
</script>
<style lang="scss">
.shopro-tabbar-wrap {
width: 100%;
height: calc(100rpx + env(safe-area-inset-bottom));
flex-shrink: 0;
}
.tabbar-box {
position: fixed;
left: 0;
right: 0;
bottom: 0;
z-index: 998;
display: flex;
align-items: stretch;
width: 100%;
min-height: 100rpx;
padding-bottom: env(safe-area-inset-bottom);
border-top: 1rpx solid #eeeeee;
box-sizing: border-box;
.tabbar-item {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 8rpx 0 6rpx;
box-sizing: border-box;
overflow: visible;
.img-box {
position: relative;
width: 50rpx;
height: 50rpx;
flex-shrink: 0;
overflow: visible;
.badge {
position: absolute;
display: inline-flex;
justify-content: center;
align-items: center;
min-width: 28rpx;
height: 28rpx;
line-height: 1;
padding: 0 8rpx;
border-radius: 100rpx;
color: #fff;
font-size: 20rpx;
z-index: 9;
background-color: #fa3534;
top: -10rpx;
right: -18rpx;
white-space: nowrap;
box-sizing: border-box;
}
}
.tabbar-icon {
width: 50rpx;
height: 50rpx;
display: block;
}
.tabbar-text {
font-size: 20rpx;
line-height: 28rpx;
margin-top: 4rpx;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
</style>