Files
kangyuan-mobile/components/goodsItem/goodsItem.vue
T
2026-05-26 15:52:30 +08:00

201 lines
5.5 KiB
Vue

<template>
<view class="goodsItem-container">
<view v-show="i === props.index">
<!-- top="120"下拉布局往下偏移,防止被悬浮菜单遮住 -->
<mescroll-uni
@init="mescrollInit"
top="0"
:down="downOption"
@down="downCallback"
:up="upOption"
@up="upCallback"
@emptyclick="emptyClick"
v-if="i === props.index"
:height="height + 'px'"
>
<!-- 数据列表 -->
<!-- <good-list :list="goods"></good-list> -->
<!-- <view v-for="(v,i) in goods" :key="i">
{{ v.picUrl }}
</view> -->
<view class="flex-row wrap w100" ref="cardRef">
<view class="cardItem" v-for="(v, i) in goods" :key="i">
<card_goods :data="v"></card_goods>
</view>
</view>
</mescroll-uni>
</view>
</view>
</template>
<script setup>
import { ref, watch, getCurrentInstance } from 'vue';
import { onPageScroll, onReachBottom } from '@dcloudio/uni-app';
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js';
import SpuApi from '@/sheep/api/product/spu';
import card_goods from './card_goods.vue';
const { proxy } = getCurrentInstance();
let goods = ref([]);
const column = ref(2);
const columnChange = ref(false);
const height = ref(0);
const props = defineProps({
i: Number, // 每个tab页的专属下标
index: {
// 当前tab的下标
type: Number,
default() {
return 0;
},
},
tabs: {
// 为了请求数据,演示用,可根据自己的项目判断是否要传
type: Array,
default() {
return [];
},
},
column: {
type: Number,
default() {
return 2;
},
},
});
watch(
() => props.column,
(val) => {
if (val !== column.value) {
columnChange.value = true;
setTimeout(() => {
columnChange.value = false;
}, 100);
column.value = val;
}
},
);
const { mescrollInit, downCallback, getMescroll } = useMescroll(onPageScroll, onReachBottom); // 调用mescroll的hook
defineExpose({ getMescroll }); // 使父组件可以通过ref调用到getMescroll方法 (必须)
const isAutoInit = props.i === props.index; // 自动加载当前tab的数据
const downOption = {
auto: isAutoInit, // 自动加载当前tab的数据
};
const upOption = {
use: true, // 必须明确启用上拉加载功能
auto: true, // 不自动加载
noMoreSize: 1, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
empty: {
tip: '~ 空空如也 ~', // 提示
},
page: {
num: 0, // 当前页码,默认0,回调之前会加1
size: 10, // 每页数据的数量
},
offset: 150, // 距底部100px时触发上拉加载
};
const isInit = ref(isAutoInit); // 当前tab是否已初始化
// 监听下标的变化
watch(
() => props.index,
(val) => {
if (props.i === val && !isInit.value) mescrollTrigger();
},
);
// 主动触发加载
const mescrollTrigger = () => {
isInit.value = true; // 标记为true
const mescroll = getMescroll();
if (mescroll) {
if (mescroll.optDown.use) {
mescroll.triggerDownScroll();
} else {
mescroll.triggerUpScroll();
}
}
};
let cardRef = ref(null);
// 是否有下一页
let hasMore = ref(true);
const upCallback = (mescroll) => {
SpuApi.getSpuPage({
pageNo: mescroll.num,
pageSize: mescroll.size,
tagTypeId: props.tabs[props.i].id,
})
.then((res) => {
const list = res.data.list || []; // 当前页数据
// if(list.length){
// list.forEach(e=>{
// e.hide = true
// })
// }
if (mescroll.num == 1) goods.value = []; //如果是第一页需手动制空列表
goods.value = goods.value.concat(list); //追加新数据
hasMore.value = goods.value.length < res.data.total;
// 计算容器高度
getHeight(mescroll.num);
mescroll.endSuccess(list.length); // 请求成功, 结束加载
})
.catch(() => {
mescroll.endErr(); // 请求失败, 结束加载
});
};
//点击空布局按钮的回调
const emptyClick = () => {
uni.showToast({
title: '点击了按钮,具体逻辑自行实现',
});
};
const getHeight = (num) => {
if (!goods.value.length) {
// 541
height.value = 10
return
}
// 相除向上取整
let cardHeight = Math.ceil(goods.value.length / 2) * (541)
let sysInfo = uni.getSystemInfoSync()
const windowHeight = sysInfo.windowHeight;
const value1 = windowHeight - sysInfo.statusBarHeight - uni.upx2px(88) - uni.upx2px(120)
let value2 = uni.upx2px(cardHeight) + 55 + uni.upx2px(12)
// if(hasMore){
// value2 = uni.upx2px(cardHeight) + uni.upx2px(12)
// }
const isGreater = value1 > value2;
if(isGreater){
height.value = value2
}else{
height.value = value1
}
// console.error(windowHeight)
// console.log("最大高"+value1)
// console.log("内容高"+value2)
// console.error(height.value)
};
</script>
<style lang="scss" scoped>
.goodsItem-container {
width: 100%;
height: 100%;
overflow: hidden;
}
.cardItem {
width: calc(50% - 8rpx);
margin-right: 16rpx;
&:nth-child(2n) {
margin-right: 0;
}
}
</style>