Files
2026-10-09 16:16:32 +08:00

280 lines
6.3 KiB
Vue

<template>
<view class="tabbar-wrap">
<view class="tabbar safe-bottom">
<!-- :src="bgSrc" -->
<image class="tabbar-bg" 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';
import * as AiApi from '@/sheep/api/ai/index';
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',//'/pages/AI/member',//
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 aiShopStatus = ref({
value: null,
checkedAt: 0,
});
const ensureAiShopOpened = async () => {
const now = Date.now();
if (aiShopStatus.value.value !== null && now - aiShopStatus.value.checkedAt < 60 * 1000) {
return Boolean(aiShopStatus.value.value);
}
const res = await AiApi.checkShopStatus({});
if (res?.code !== 0) {
throw new Error('checkShopStatus_failed');
}
const opened = Boolean(res?.data);
aiShopStatus.value = {
value: opened ? 1 : 0,
checkedAt: now,
};
return opened;
};
const onTap = async (item) => {
if (item.isMid) {
hideNativeTabBar();
uni.navigateTo({ url: item.pagePath });
return;
}
if (isActive(item)) return;
if (normalizePath(item.pagePath) === '/pages/index/aiStore') {
try {
const opened = await ensureAiShopOpened();
if (!opened) {
uni.showToast({
title: '您尚未开通AI店铺,无法查看购物车。请先开通AI店铺后再进行购买。',
icon: 'none',
duration: 2500,
});
return;
}
} catch (e) {
uni.showToast({
title: '系统繁忙,请稍后重试',
icon: 'none',
});
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;
background: #ffffff;
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>