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

168 lines
4.4 KiB
Vue

<template>
<view v-show="i === props.index">
<mescroll-body
@init="mescrollInit"
top="0"
:down="downOption"
@down="downCallback"
:up="upOption"
@up="upCallback"
@emptyclick="emptyClick"
v-if="i === props.index"
>
<view class="flex-row wrap w100">
<view class="cardItem" v-for="(v, i) in goods" :key="i">
<card_goods :data="v"></card_goods>
</view>
</view>
</mescroll-body>
</view>
</template>
<script setup>
import { ref, watch } from 'vue';
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js';
import SpuApi from '@/sheep/api/product/spu';
import { getAiProduct } from '@/sheep/api/ai';
import card_goods from './card_goods.vue';
let goods = ref([]);
const column = ref(2);
const columnChange = ref(false);
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(); // 调用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();
}
}
};
const upCallback = (mescroll) => {
const tab = props.tabs?.[props.i] || {};
const tabName = String(tab?.name || '').trim();
const isMemberPreferred = tabName === '会员优选';
if (isMemberPreferred) {
getAiProduct({
pageNo: mescroll.num,
pageSize: mescroll.size,
})
.then((res) => {
const list = res?.data?.list || [];
const next = Array.isArray(list) ? list : [];
if (mescroll.num == 1) goods.value = [];
goods.value = goods.value.concat(next);
mescroll.endSuccess(next.length);
})
.catch(() => {
mescroll.endErr();
});
return;
}
SpuApi.getSpuPage({
pageNo: mescroll.num,
pageSize: mescroll.size,
tagTypeId: tab.id,
})
.then((res) => {
const list = res.data.list || []; // 当前页数据
if (mescroll.num == 1) goods.value = []; //如果是第一页需手动制空列表
goods.value = goods.value.concat(list); //追加新数据
mescroll.endSuccess(list.length); // 请求成功, 结束加载
})
.catch(() => {
mescroll.endErr(); // 请求失败, 结束加载
});
};
//点击空布局按钮的回调
const emptyClick = () => {
uni.showToast({
title: '点击了按钮,具体逻辑自行实现',
});
};
</script>
<style lang="scss" scoped>
.cardItem {
width: calc(50% - 8rpx);
margin-right: 16rpx;
&:nth-child(2n) {
margin-right: 0;
}
}
</style>