diff --git a/src/components/PhotoSquare.vue b/src/components/PhotoSquare.vue index 71d97fc..6ebbf15 100644 --- a/src/components/PhotoSquare.vue +++ b/src/components/PhotoSquare.vue @@ -47,13 +47,13 @@ const fetchImages = async () => { images.value = data.data if (data.my_published_photo) { displayMyPublishPhoto.value = true; + username.value = data.my_published_photo.user_name; + likesCount.value = data.my_published_photo.likes_count; + userImg.value = data.my_published_photo.avatar; + ranking.value = data.my_published_photo.ranking; } else { displayMyPublishPhoto.value = false; } - username.value = data.my_published_photo.user_name; - likesCount.value = data.my_published_photo.likes_count; - userImg.value = data.my_published_photo.avatar; - ranking.value = data.my_published_photo.ranking; } else { ElMessage.error(data.message); emit('update:show', false); @@ -70,8 +70,66 @@ watch(() => props.show, async (newVal) => { fetchImages(); }, {immediate: true}) -// 图片数据 +// 图片数据与分页 const images = ref([]); +const page = ref(1); +const perPage = 20; +const loading = ref(false); +const finished = ref(false); + +const loadMoreImages = async () => { + if (loading.value || finished.value) return; + loading.value = true; + try { + const url = new URL('https://huodong2.lzlj.com/api/faceFamily/face/square'); + url.searchParams.append('my_only', '0'); + url.searchParams.append('page', page.value); + url.searchParams.append('per_page', perPage); + const response = await fetch(url.toString(), { + method: 'GET', + headers: { + 'Authorization': `Bearer ${Storage.get("userinfos").api_token}` + } + }); + const data = await response.json(); + if (response.status == 200 || response.status == 201) { + if (data.data.length < perPage) finished.value = true; + images.value.push(...data.data); + page.value++; + if (data.my_published_photo) { + displayMyPublishPhoto.value = true; + username.value = data.my_published_photo.user_name; + likesCount.value = data.my_published_photo.likes_count; + userImg.value = data.my_published_photo.avatar; + ranking.value = data.my_published_photo.ranking; + } else { + displayMyPublishPhoto.value = false; + } + } else { + ElMessage.error(data.message); + emit('update:show', false); + } + } catch (error) { + console.error('Error:', error); + } + loading.value = false; +}; + +const handleScroll = (e) => { + const el = e.target; + if (el.scrollHeight - el.scrollTop - el.clientHeight < 100) { + loadMoreImages(); + } +}; + +watch(() => props.show, async (newVal) => { + if (!newVal) return; + // 重置分页 + images.value = []; + page.value = 1; + finished.value = false; + await loadMoreImages(); +}, {immediate: true}); const userinfos = Storage.get("userinfos") let mergeId = ''; @@ -191,52 +249,30 @@ const getBackgroundImage = (item) => { @click.stop="$emit('go-my-photo')">
打榜活动截止至10月3日23:59:59结束,中奖信息将于10月4日早10点推送至获奖会员。
-
-