文件
xionggeshop/xgsc.gynhc.com_wx/components/shopro-tabbar/shopro-tabbar.vue
2026-09-14 17:24:53 +08:00

287 行
7.4 KiB
Vue

<template>
<view class="shopro-tabbar-wrap" v-if="tabbarList && tabbarList.length && showTabbar">
<view class="tabbar-box" :style="{ background: themeTabbar.bgcolor }">
<view
class="tabbar-item"
:class="{ 'tabbar-item--active': isTabActive(tab) }"
v-for="(tab, index) in tabbarList"
:key="tab.name"
@tap="switchTabbar(tab, index)"
>
<view class="img-box">
<u-icon
class="tabbar-uicon"
:class="{ 'tabbar-uicon--active': isTabActive(tab) }"
:name="getTabUicon(tab)"
:color="isTabActive(tab) ? themeTabbar.activeColor : themeTabbar.color"
size="44"
></u-icon>
<view v-if="tab.path == '/pages/index/cart' && cartNum" class="badge">{{ cartNum }}</view>
</view>
<view class="tabbar-text-wrap" v-if="tabbarData.style == 1 || tabbarData.style == 3">
<view class="tabbar-text">{{ tab.name }}</view>
<view class="tabbar-dot"></view>
</view>
</view>
</view>
</view>
</template>
<script>
import { mapGetters } from 'vuex';
import theme from '@/shopro/config/theme.js';
import { hideNativeTabBar, DEFAULT_TABBAR_ICONS } from '@/shopro/utils/theme';
const TABBAR_UICON_MAP = {
'/pages/index/index': { default: 'home', active: 'home-fill' },
'/pages/index/category': { default: 'grid', active: 'grid-fill' },
'/pages/index/cart': { default: 'shopping-cart', active: 'shopping-cart-fill' },
'/pages/index/user': { default: 'account', active: 'account-fill' }
};
export default {
name: 'shoproTabbar',
data() {
return {
currentPath: '',
iconFallback: {},
iconFailed: {}
};
},
mounted() {
hideNativeTabBar();
this.syncCurrentPath();
},
watch: {
'$Route.path'() {
this.syncCurrentPath();
},
'$Route.fullPath'() {
this.syncCurrentPath();
},
tabbarList: {
handler() {
this.iconFallback = {};
this.iconFailed = {};
},
deep: true
}
},
computed: {
...mapGetters(['cartNum', 'tabbarData']),
themeTabbar() {
if (this.tabbarData?.list?.length) {
return this.tabbarData;
}
return {
activeColor: theme.tabbarActive,
color: theme.tabbarInactive,
bgcolor: theme.tabbarBg,
list: []
};
},
tabbarList() {
return this.themeTabbar.list || [];
},
showTabbar() {
const list = this.themeTabbar.list;
if (list && list.length) {
const current = this.normalizePath(this.currentPath);
const arr = list.map(item => this.normalizePath(item.path));
if (!arr.includes('/pages/index/index')) {
arr.unshift('/pages/index/index');
}
return arr.includes(current);
}
}
},
methods: {
syncCurrentPath() {
const route = this.$Route || {};
this.currentPath = route.path === '/pages/index/index' ? route.path : (route.fullPath || route.path || '');
},
normalizePath(path) {
if (!path) return '';
const basePath = String(path).split('?')[0];
return basePath.length > 1 && basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
},
isTabActive(tab) {
return this.normalizePath(this.currentPath) === this.normalizePath(tab.path);
},
getTabKey(tab) {
return tab.path || tab.name;
},
getTabDefaults(tab) {
return DEFAULT_TABBAR_ICONS[this.normalizePath(tab.path)] || {};
},
getTabIconKey(tab) {
return `${this.getTabKey(tab)}-${this.isTabActive(tab) ? 'active' : 'inactive'}`;
},
getTabIcon(tab) {
return this.isTabActive(tab) ? this.getActiveIcon(tab) : this.getInactiveIcon(tab);
},
getIconFromStore(key) {
if (this.iconFailed[key]) return '';
return this.iconFallback[key] || '';
},
getInactiveIcon(tab) {
const key = `${this.getTabKey(tab)}-inactive`;
const cached = this.getIconFromStore(key);
if (cached) return cached;
const defaults = this.getTabDefaults(tab);
return tab.image || defaults.image || tab.activeImage || defaults.activeImage || '';
},
getActiveIcon(tab) {
const key = `${this.getTabKey(tab)}-active`;
const cached = this.getIconFromStore(key);
if (cached) return cached;
const defaults = this.getTabDefaults(tab);
return tab.activeImage || tab.activeImageFallback || defaults.activeImage || tab.image || defaults.image || '';
},
getTabUicon(tab) {
const icons = TABBAR_UICON_MAP[this.normalizePath(tab.path)] || { default: 'home', active: 'home-fill' };
return this.isTabActive(tab) ? icons.active : icons.default;
},
onIconError(tab) {
if (this.isTabActive(tab)) {
this.onActiveIconError(tab);
} else {
this.onInactiveIconError(tab);
}
},
onInactiveIconError(tab) {
const key = `${this.getTabKey(tab)}-inactive`;
const defaults = this.getTabDefaults(tab);
const candidates = [defaults.image, tab.activeImage, defaults.activeImage].filter(
url => url && url !== tab.image && url !== this.iconFallback[key]
);
if (candidates.length) {
this.$set(this.iconFallback, key, candidates[0]);
return;
}
this.$set(this.iconFailed, key, true);
},
onActiveIconError(tab) {
const key = `${this.getTabKey(tab)}-active`;
const defaults = this.getTabDefaults(tab);
const current = this.iconFallback[key] || tab.activeImage || tab.activeImageFallback;
const candidates = [tab.activeImageFallback, defaults.activeImage, tab.image, defaults.image].filter(
url => url && url !== current
);
if (candidates.length) {
this.$set(this.iconFallback, key, candidates[0]);
return;
}
this.$set(this.iconFailed, key, true);
},
switchTabbar(tab, index) {
this.currentPath = tab.path;
this.$tools.routerTo(tab.path, true);
}
}
};
</script>
<style lang="scss">
.shopro-tabbar-wrap {
height: calc(100rpx + env(safe-area-inset-bottom) / 2);
padding-bottom: calc(env(safe-area-inset-bottom) / 2);
position: relative;
width: 100%;
z-index: 70;
.tabbar-box {
position: fixed;
display: flex;
align-items: center;
width: 100%;
height: calc(100rpx + env(safe-area-inset-bottom) / 2);
border-top: 1rpx solid $brand-border;
background-color: $brand-tabbar-bg;
box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.04);
z-index: 998;
bottom: 0;
padding-bottom: calc(env(safe-area-inset-bottom) / 2);
.tabbar-item {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
flex: 1;
.img-box {
position: relative;
.badge {
position: absolute;
/* #ifndef APP-NVUE */
display: inline-flex;
/* #endif */
justify-content: center;
align-items: center;
line-height: 24rpx;
padding: 4rpx 10rpx;
border-radius: 100rpx;
color: #fff;
font-size: 24rpx;
z-index: 9;
background-color: $u-type-error;
transform: scale(0.8);
transform-origin: center center;
top: 0;
left: 46rpx;
white-space: nowrap;
}
}
.tabbar-uicon {
opacity: 0.72;
}
.tabbar-uicon--active,
&--active .tabbar-uicon {
opacity: 1;
}
.tabbar-text-wrap {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 4rpx;
}
.tabbar-text {
font-size: 22rpx;
line-height: 30rpx;
color: $brand-tabbar-inactive;
font-weight: 400;
}
.tabbar-dot {
width: 8rpx;
height: 8rpx;
border-radius: 50%;
margin-top: 4rpx;
background: transparent;
}
&--active {
.tabbar-text {
color: $brand-tabbar-active;
font-size: 24rpx;
font-weight: 700;
}
.tabbar-dot {
background: $brand-tabbar-active-accent;
box-shadow: 0 0 8rpx rgba(22, 119, 255, 0.45);
}
}
}
}
}
</style>