Files
kangyuan-mobile/components/goods/goods.vue
T

175 lines
4.8 KiB
Vue

<template>
<view>
<su-sticky offsetTop="0" zIndex="20" bgColor="#F2F4F7">
<scroll-view scroll-x show-scrollbar="false" class="scroll-x-wrapper p-x-32">
<view class="navs flex-center ju-flex-start nowrap" style="width: auto;">
<view
class="navItem flex-center flex-column flex1 shrink nowrap"
:class="{ tabIndex: i == tabIndex, c34: i == tabIndex }"
v-for="(v, i) in displayList"
:key="i"
@click="navChange(i)"
>
<view class="navText"> {{ v.name }} </view>
<!-- <image src="/static/index/active.png" mode="scaleToFill" /> -->
</view>
</view>
</scroll-view>
</su-sticky>
<view class="goodsPages">
<goods-item
v-for="(tab, i) in displayList"
:key="tab.id || i"
:ref="(el) => mescrollItems[i] && (mescrollItems[i].value = el)"
:i="i"
:index="tabIndex"
:tabs="displayList"
></goods-item>
</view>
</view>
</template>
<script setup>
import { ref, watch } from 'vue';
import goodsItem from '../goodsItem/goodsItem.vue';
import { onPageScroll, onReachBottom, onShow } from '@dcloudio/uni-app';
import useMescrollMore from '@/uni_modules/mescroll-uni/hooks/useMescrollMore.js';
import UserApi from '@/sheep/api/member/user';
let props = defineProps({
list: {
type: Array,
default: () => [],
},
defaultTabName: {
type: String,
default: '',
},
});
const hasPurchasedShop = ref(null);
const displayList = ref([]);
const hasAppliedDefaultTab = ref(false);
const computeDisplayList = () => {
const list = Array.isArray(props.list) ? props.list : [];
const hasVip = list.some((v) => String(v?.name || '').trim() === '会员优选');
if (hasVip && Number(hasPurchasedShop.value) === 1) {
return list.filter((v) => String(v?.name || '').trim() !== '会员优选');
}
return list;
};
// 动态生成与 tabs 同步的 refs 数组,供 useMescrollMore 使用
const mescrollItems = [];
const syncMescrollItems = (list) => {
const n = Array.isArray(list) ? list.length : 0;
while (mescrollItems.length < n) mescrollItems.push(ref(null));
if (mescrollItems.length > n) mescrollItems.splice(n);
};
const { tabIndex } = useMescrollMore(
mescrollItems,
onPageScroll,
onReachBottom,
);
const applyDefaultTabIfNeeded = (list) => {
const target = String(props.defaultTabName || '').trim();
if (!target) return;
if (hasAppliedDefaultTab.value) return;
const index = (Array.isArray(list) ? list : []).findIndex(
(v) => String(v?.name || '').trim() === target,
);
if (index >= 0) {
tabIndex.value = index;
hasAppliedDefaultTab.value = true;
}
};
const refreshDisplay = () => {
const next = computeDisplayList();
displayList.value = next;
syncMescrollItems(next);
applyDefaultTabIfNeeded(next);
if (tabIndex.value >= next.length) tabIndex.value = 0;
};
const loadShopStatus = async () => {
const list = Array.isArray(props.list) ? props.list : [];
const hasVip = list.some((v) => String(v?.name || '').trim() === '会员优选');
if (!hasVip) return;
if (hasPurchasedShop.value !== null && hasPurchasedShop.value !== undefined) return;
const res = await UserApi.getUserInfo().catch(() => ({ code: -1 }));
if (res?.code === 0) {
hasPurchasedShop.value = Number(res?.data?.hasPurchasedShop) || 0;
refreshDisplay();
}
};
watch(
() => props.list,
() => {
refreshDisplay();
if (hasPurchasedShop.value === null || hasPurchasedShop.value === undefined) {
loadShopStatus();
}
},
{ immediate: true, deep: true },
);
watch(
() => props.defaultTabName,
() => {
hasAppliedDefaultTab.value = false;
applyDefaultTabIfNeeded(displayList.value);
},
{ immediate: true },
);
// let tabIndex = ref(0);
const navChange = (i) => {
// scrollToLastY()
tabIndex.value = i;
};
onShow(() => {
loadShopStatus();
});
</script>
<style lang="scss" scoped>
.scroll-x-wrapper {
width: 100%;
white-space: nowrap;
}
.navs {
box-sizing: border-box;
height: 120rpx;
.navItem {
color: #777777;
font-size: 28rpx;
white-space: nowrap;
flex-shrink: 0; // 防止被压缩
image {
width: 40rpx;
height: 12rpx;
opacity: 0;
margin-top: 12rpx;
}
}
.tabIndex {
color: #777777;
font-weight: 500;
background: linear-gradient(135deg, #2C8CFA 0%, #3D5EFB 100%);
color: #ffffff !important;
border-radius: 12rpx;
image {
opacity: 1;
}
}
}
.goodsPages {
padding: 0 32rpx;
padding-top: 12rpx;
box-sizing: border-box;
}
</style>