234 lines
5.1 KiB
Vue
234 lines
5.1 KiB
Vue
<template>
|
|
<view class="tabbar-wrap">
|
|
<view class="tabbar safe-bottom">
|
|
<image class="tabbar-bg" :src="bgSrc" mode="scaleToFill" />
|
|
<view class="tabbar-inner">
|
|
<view
|
|
v-for="item in items"
|
|
:key="item.pagePath"
|
|
class="tabbar-item flex1 flex-center flex-column"
|
|
:class="{ 'tabbar-item--mid': item.isMid }"
|
|
@click="onTap(item)"
|
|
>
|
|
<view :class="{ iconBox: item.isMid }">
|
|
<image
|
|
class="icon"
|
|
:class="{ iconMid: item.isMid }"
|
|
:src="getIconSrc(item)"
|
|
mode="aspectFit"
|
|
/>
|
|
</view>
|
|
<view v-if="item.text" class="s_20 text" :class="{ textActive: isActive(item) }">
|
|
{{ item.text }}
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
<view v-if="props.placeholder" class="tabbar-placeholder safe-bottom"></view>
|
|
</view>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { computed, ref, watch } from 'vue';
|
|
import { onShow } from '@dcloudio/uni-app';
|
|
|
|
const props = defineProps({
|
|
path: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
placeholder: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
});
|
|
|
|
const currentPath = ref('');
|
|
|
|
const normalizePath = (p) => {
|
|
if (!p) return '';
|
|
return p.startsWith('/') ? p : `/${p}`;
|
|
};
|
|
|
|
const updateCurrentPath = () => {
|
|
const pages = getCurrentPages?.() || [];
|
|
const current = pages[pages.length - 1];
|
|
const route = current?.route || '';
|
|
currentPath.value = normalizePath(route);
|
|
};
|
|
|
|
const hideNativeTabBar = () => {
|
|
try {
|
|
uni.hideTabBar({
|
|
fail: () => {},
|
|
});
|
|
} catch (e) {}
|
|
};
|
|
|
|
onShow(() => {
|
|
hideNativeTabBar();
|
|
updateCurrentPath();
|
|
});
|
|
|
|
watch(
|
|
() => props.path,
|
|
(val) => {
|
|
if (val) currentPath.value = normalizePath(val);
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
const items = computed(() => [
|
|
{
|
|
pagePath: '/pages/index/index',
|
|
text: '首页',
|
|
iconPath: '/static/index/1.png',
|
|
selectedIconPath: '/static/index/blue_icon/1_1.png',
|
|
},
|
|
{
|
|
pagePath: '/pages/index/aiStore',
|
|
text: 'AI店铺',
|
|
iconPath: '/static/index/2.png',
|
|
selectedIconPath: '/static/index/blue_icon/2_1.png',
|
|
},
|
|
{
|
|
pagePath: '/pages/home/seed',
|
|
text: '',
|
|
iconPath: '/static/index/jinzhongzi.png',
|
|
selectedIconPath: '/static/index/jinzhongzi.png',
|
|
isMid: true,
|
|
},
|
|
{
|
|
pagePath: '/pages/cart/cart',
|
|
text: '购物车',
|
|
iconPath: '/static/index/3.png',
|
|
selectedIconPath: '/static/index/blue_icon/3_1.png',
|
|
},
|
|
{
|
|
pagePath: '/pages/index/user',
|
|
text: '我的',
|
|
iconPath: '/static/index/4.png',
|
|
selectedIconPath: '/static/index/blue_icon/4_1.png',
|
|
},
|
|
]);
|
|
|
|
const isActive = (item) => {
|
|
return normalizePath(item.pagePath) === normalizePath(currentPath.value);
|
|
};
|
|
|
|
const getIconSrc = (item) => {
|
|
return isActive(item) ? item.selectedIconPath : item.iconPath;
|
|
};
|
|
|
|
const onTap = (item) => {
|
|
if (item.isMid) {
|
|
hideNativeTabBar();
|
|
uni.navigateTo({ url: item.pagePath });
|
|
return;
|
|
}
|
|
if (isActive(item)) return;
|
|
hideNativeTabBar();
|
|
uni.switchTab({
|
|
url: item.pagePath,
|
|
fail: () => {
|
|
uni.reLaunch({ url: item.pagePath });
|
|
},
|
|
});
|
|
};
|
|
|
|
const bgSrc = computed(() => {
|
|
const svg = `
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 750 150" preserveAspectRatio="none">
|
|
<path fill="#ffffff" d="M40 0 H295 Q316 0 316 21 A59 109 0 0 0 434 21 Q434 0 455 0 H710 Q750 0 750 40 V150 H0 V40 Q0 0 40 0 Z"/>
|
|
</svg>
|
|
`;
|
|
return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;
|
|
});
|
|
</script>
|
|
|
|
<style lang="scss">
|
|
.tabbar-wrap {
|
|
width: 100%;
|
|
}
|
|
|
|
.tabbar {
|
|
position: fixed;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
z-index: 80;
|
|
// background: var(--tabbar-bg, #ffffff);
|
|
overflow: visible;
|
|
}
|
|
|
|
.tabbar-bg {
|
|
position: absolute;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: env(safe-area-inset-bottom);
|
|
width: 100%;
|
|
height: 100rpx;
|
|
filter: drop-shadow(0 -6rpx 24rpx rgba(0, 0, 0, 0.12));
|
|
pointer-events: none;
|
|
}
|
|
|
|
.tabbar-inner {
|
|
height: 100rpx;
|
|
display: flex;
|
|
align-items: flex-end;
|
|
padding: 0 12rpx;
|
|
position: relative;
|
|
z-index: 1;
|
|
}
|
|
|
|
.tabbar-item {
|
|
min-width: 0;
|
|
padding-bottom: 10rpx;
|
|
}
|
|
|
|
.tabbar-item--mid {
|
|
padding-bottom: 0;
|
|
transform: translateY(-25rpx);
|
|
}
|
|
|
|
.icon {
|
|
width: 44rpx;
|
|
height: 44rpx;
|
|
}
|
|
|
|
.iconBox {
|
|
height: 150rpx;
|
|
display: flex;
|
|
align-items: flex-end;
|
|
justify-content: center;
|
|
}
|
|
|
|
.iconMid {
|
|
width: 100rpx;
|
|
height: 100rpx;
|
|
border-radius: 50%;
|
|
border-radius: 50%;
|
|
box-shadow: 0 10rpx 22rpx rgba(38, 107, 246, 0.8);
|
|
background: rgb(38, 107, 246);
|
|
}
|
|
|
|
.text {
|
|
margin-top: 6rpx;
|
|
font-size: 22rpx;
|
|
line-height: 1;
|
|
color: var(--tabbar-text, #979797);
|
|
}
|
|
|
|
.textActive {
|
|
color: #3b5efb;
|
|
}
|
|
|
|
.safe-bottom {
|
|
padding-bottom: env(safe-area-inset-bottom);
|
|
}
|
|
|
|
.tabbar-placeholder {
|
|
height: 100rpx;
|
|
}
|
|
</style>
|