Skip to content

Commit 57424f9

Browse files
committed
🐞 fix: 修复歌手页面数据加载问题
1 parent 32b9368 commit 57424f9

File tree

1 file changed

+70
-15
lines changed

1 file changed

+70
-15
lines changed

src/renderer/views/artist/detail.vue

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@play="handlePlay"
3434
/>
3535
<div v-if="songLoading" class="loading-more">{{ t('common.loading') }}</div>
36+
<div v-else-if="songPage.hasMore" ref="songsLoadMoreRef" class="load-more-trigger"></div>
3637
</div>
3738
</div>
3839
</n-tab-pane>
@@ -54,6 +55,7 @@
5455
}"
5556
/>
5657
<div v-if="albumLoading" class="loading-more">{{ t('common.loading') }}</div>
58+
<div v-else-if="albumPage.hasMore" ref="albumsLoadMoreRef" class="load-more-trigger"></div>
5759
</div>
5860
</div>
5961
</n-tab-pane>
@@ -114,6 +116,12 @@ const albumPage = ref({
114116
hasMore: true
115117
});
116118
119+
// 无限滚动引用
120+
const songsLoadMoreRef = ref<HTMLElement | null>(null);
121+
const albumsLoadMoreRef = ref<HTMLElement | null>(null);
122+
let songsObserver: IntersectionObserver | null = null;
123+
let albumsObserver: IntersectionObserver | null = null;
124+
117125
// 加载歌手信息
118126
const loadArtistInfo = async () => {
119127
if (!artistId.value) return;
@@ -138,12 +146,12 @@ const loadArtistInfo = async () => {
138146
const resetPagination = () => {
139147
songPage.value = {
140148
page: 1,
141-
pageSize: 30,
149+
pageSize: 50,
142150
hasMore: true
143151
};
144152
albumPage.value = {
145153
page: 1,
146-
pageSize: 30,
154+
pageSize: 50,
147155
hasMore: true
148156
};
149157
songs.value = [];
@@ -181,6 +189,8 @@ const loadSongs = async () => {
181189
songs.value = page === 1 ? newSongs : [...songs.value, ...newSongs];
182190
songPage.value.hasMore = newSongs.length === pageSize;
183191
songPage.value.page++;
192+
} else {
193+
songPage.value.hasMore = false;
184194
}
185195
} catch (error) {
186196
console.error('加载歌曲失败:', error);
@@ -207,6 +217,8 @@ const loadAlbums = async () => {
207217
albums.value = page === 1 ? newAlbums : [...albums.value, ...newAlbums];
208218
albumPage.value.hasMore = newAlbums.length === pageSize;
209219
albumPage.value.page++;
220+
} else {
221+
albumPage.value.hasMore = false;
210222
}
211223
} catch (error) {
212224
console.error('加载专辑失败:', error);
@@ -229,29 +241,64 @@ const handlePlay = () => {
229241
);
230242
};
231243
232-
// 添加滚动处理函数
233-
const handleScroll = useThrottleFn(() => {
234-
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
235-
const windowHeight = window.innerHeight;
236-
const documentHeight = document.documentElement.scrollHeight;
244+
// 设置无限滚动观察器
245+
const setupIntersectionObservers = () => {
246+
// 清除现有的观察器
247+
if (songsObserver) songsObserver.disconnect();
248+
if (albumsObserver) albumsObserver.disconnect();
237249
238-
if (documentHeight - (scrollTop + windowHeight) < 100) {
239-
if (activeTab.value === 'songs') {
250+
// 创建歌曲观察器
251+
songsObserver = new IntersectionObserver((entries) => {
252+
if (entries[0].isIntersecting && !songLoading.value && songPage.value.hasMore) {
240253
loadSongs();
241-
} else if (activeTab.value === 'albums') {
254+
}
255+
}, { threshold: 0.1 });
256+
257+
// 创建专辑观察器
258+
albumsObserver = new IntersectionObserver((entries) => {
259+
if (entries[0].isIntersecting && !albumLoading.value && albumPage.value.hasMore) {
242260
loadAlbums();
243261
}
244-
}
245-
}, 200);
262+
}, { threshold: 0.1 });
263+
264+
// 监听标签页更改,重新设置观察器
265+
watch(activeTab, (newTab) => {
266+
nextTick(() => {
267+
if (newTab === 'songs' && songsLoadMoreRef.value && songPage.value.hasMore) {
268+
songsObserver?.observe(songsLoadMoreRef.value);
269+
} else if (newTab === 'albums' && albumsLoadMoreRef.value && albumPage.value.hasMore) {
270+
albumsObserver?.observe(albumsLoadMoreRef.value);
271+
}
272+
});
273+
});
274+
275+
// 监听引用元素的变化
276+
watch(songsLoadMoreRef, (el) => {
277+
if (el && activeTab.value === 'songs' && songPage.value.hasMore) {
278+
songsObserver?.observe(el);
279+
}
280+
});
281+
282+
watch(albumsLoadMoreRef, (el) => {
283+
if (el && activeTab.value === 'albums' && albumPage.value.hasMore) {
284+
albumsObserver?.observe(el);
285+
}
286+
});
287+
};
246288
247-
// 监听页面滚动
248289
onMounted(() => {
249290
loadArtistInfo();
250-
window.addEventListener('scroll', handleScroll);
291+
292+
// 添加nextTick以确保DOM已更新
293+
nextTick(() => {
294+
setupIntersectionObservers();
295+
});
251296
});
252297
253298
onUnmounted(() => {
254-
window.removeEventListener('scroll', handleScroll);
299+
// 清理观察器
300+
if (songsObserver) songsObserver.disconnect();
301+
if (albumsObserver) albumsObserver.disconnect();
255302
});
256303
257304
// 监听路由参数变化
@@ -260,6 +307,10 @@ watch(
260307
(newId) => {
261308
if (newId) {
262309
loadArtistInfo();
310+
// 添加nextTick以确保DOM已更新
311+
nextTick(() => {
312+
setupIntersectionObservers();
313+
});
263314
}
264315
}
265316
);
@@ -325,6 +376,10 @@ watch(
325376
@apply text-center py-4 text-gray-500 dark:text-gray-400;
326377
}
327378
379+
.load-more-trigger {
380+
@apply h-4 w-full;
381+
}
382+
328383
.artist-description {
329384
.description-content {
330385
@apply text-sm leading-relaxed whitespace-pre-wrap;

0 commit comments

Comments
 (0)