diff --git a/src/router/home.ts b/src/router/home.ts index f99988e..545c857 100644 --- a/src/router/home.ts +++ b/src/router/home.ts @@ -44,24 +44,25 @@ const layoutRouter = [ }, component: () => import('@/views/mv/index.vue'), }, + // { + // path: '/history', + // name: 'history', + // meta: { + // title: '历史', + // icon: 'icon-a-TicketStar', + // keepAlive: true, + // }, + // component: () => import('@/views/history/index.vue'), + // }, { path: '/history', name: 'history', + component: () => import('@/views/historyAndFavorite/index.vue'), meta: { - title: '历史', + title: '我的收藏和历史', icon: 'icon-a-TicketStar', keepAlive: true, }, - component: () => import('@/views/history/index.vue'), - }, - { - path: '/favorite', - name: 'favorite', - component: () => import('@/views/favorite/index.vue'), - meta: { - title: '我的收藏', - icon: 'icon-likefill', - }, }, { path: '/user', diff --git a/src/views/favorite/index.vue b/src/views/favorite/index.vue index 3f43d3a..428b47b 100644 --- a/src/views/favorite/index.vue +++ b/src/views/favorite/index.vue @@ -1,45 +1,35 @@ @@ -58,12 +48,14 @@ const store = useStore(); const favoriteList = computed(() => store.state.favoriteList); const favoriteSongs = ref([]); const loading = ref(false); +const noMore = ref(false); +const scrollbarRef = ref(); -// 分页相关 +// 无限滚动相关 const pageSize = 16; const currentPage = ref(1); -defineProps({ +const props = defineProps({ isComponent: { type: Boolean, default: false, @@ -72,7 +64,6 @@ defineProps({ // 获取当前页的收藏歌曲ID const getCurrentPageIds = () => { - // 反转列表顺序,最新收藏的在前面 const reversedList = [...favoriteList.value]; const startIndex = (currentPage.value - 1) * pageSize; const endIndex = startIndex + pageSize; @@ -86,17 +77,29 @@ const getFavoriteSongs = async () => { return; } + if (props.isComponent && favoriteSongs.value.length >= 16) { + return; + } + loading.value = true; try { const currentIds = getCurrentPageIds(); const res = await getMusicDetail(currentIds); if (res.data.songs) { - favoriteSongs.value = res.data.songs.map((song: SongResult) => { - return { - ...song, - picUrl: song.al?.picUrl || '', - }; - }); + const newSongs = res.data.songs.map((song: SongResult) => ({ + ...song, + picUrl: song.al?.picUrl || '', + })); + + // 追加新数据而不是替换 + if (currentPage.value === 1) { + favoriteSongs.value = newSongs; + } else { + favoriteSongs.value = [...favoriteSongs.value, ...newSongs]; + } + + // 判断是否还有更多数据 + noMore.value = favoriteSongs.value.length >= favoriteList.value.length; } } catch (error) { console.error('获取收藏歌曲失败:', error); @@ -105,9 +108,15 @@ const getFavoriteSongs = async () => { } }; -// 处理页码变化 -const handlePageChange = () => { - getFavoriteSongs(); +// 处理滚动事件 +const handleScroll = (e: any) => { + const { scrollTop, scrollHeight, offsetHeight } = e.target; + const threshold = 100; // 距离底部多少像素时加载更多 + + if (!loading.value && !noMore.value && scrollHeight - (scrollTop + offsetHeight) < threshold) { + currentPage.value++; + getFavoriteSongs(); + } }; onMounted(() => { @@ -119,6 +128,7 @@ watch( favoriteList, () => { currentPage.value = 1; + noMore.value = false; getFavoriteSongs(); }, { deep: true, immediate: true }, @@ -129,8 +139,7 @@ const handlePlay = () => { }; const getItemAnimationDelay = (index: number) => { - const currentPageIndex = index % pageSize; - return setAnimationDelay(currentPageIndex, 30); + return setAnimationDelay(index, 30); }; const router = useRouter(); @@ -141,13 +150,13 @@ const handleMore = () => { diff --git a/src/views/historyAndFavorite/index.vue b/src/views/historyAndFavorite/index.vue new file mode 100644 index 0000000..4e51d2d --- /dev/null +++ b/src/views/historyAndFavorite/index.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/src/views/home/index.vue b/src/views/home/index.vue index 07d0549..ed17268 100644 --- a/src/views/home/index.vue +++ b/src/views/home/index.vue @@ -47,10 +47,13 @@ defineOptions({ :deep(.favorite-page) { @apply p-0 mx-4 h-[300px]; .favorite-header { - @apply mb-0; + @apply mb-0 px-0 !important; h2 { - @apply text-lg font-bold mb-4; + @apply text-lg font-bold; } } + .favorite-list { + @apply px-0 !important; + } }