mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-14 14:50:50 +08:00
✨ feat: 优化历史歌曲获取功能, 分离网易云音乐和B站视频处理逻辑
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
import { getBilibiliProxyUrl, getBilibiliVideoDetail } from '@/api/bilibili';
|
||||
import { getMusicDetail } from '@/api/music';
|
||||
import SongItem from '@/components/common/SongItem.vue';
|
||||
import { useMusicHistory } from '@/hooks/MusicHistoryHook';
|
||||
@@ -71,27 +72,89 @@ const getHistorySongs = async () => {
|
||||
const endIndex = startIndex + pageSize;
|
||||
const currentPageItems = musicList.value.slice(startIndex, endIndex);
|
||||
|
||||
const currentIds = currentPageItems.map((item) => item.id as number);
|
||||
const res = await getMusicDetail(currentIds);
|
||||
// 分离网易云音乐和B站视频
|
||||
const neteaseItems = currentPageItems.filter((item) => item.source !== 'bilibili');
|
||||
const bilibiliItems = currentPageItems.filter((item) => item.source === 'bilibili');
|
||||
|
||||
if (res.data.songs) {
|
||||
const newSongs = res.data.songs.map((song: SongResult) => {
|
||||
const historyItem = currentPageItems.find((item) => item.id === song.id);
|
||||
return {
|
||||
...song,
|
||||
picUrl: song.al?.picUrl || '',
|
||||
count: historyItem?.count || 0
|
||||
};
|
||||
});
|
||||
|
||||
if (currentPage.value === 1) {
|
||||
displayList.value = newSongs;
|
||||
} else {
|
||||
displayList.value = [...displayList.value, ...newSongs];
|
||||
// 处理网易云音乐
|
||||
let neteaseSongs: SongResult[] = [];
|
||||
if (neteaseItems.length > 0) {
|
||||
const currentIds = neteaseItems.map((item) => item.id as number);
|
||||
const res = await getMusicDetail(currentIds);
|
||||
if (res.data.songs) {
|
||||
neteaseSongs = res.data.songs.map((song: SongResult) => {
|
||||
const historyItem = neteaseItems.find((item) => item.id === song.id);
|
||||
return {
|
||||
...song,
|
||||
picUrl: song.al?.picUrl || '',
|
||||
count: historyItem?.count || 0,
|
||||
source: 'netease'
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
noMore.value = displayList.value.length >= musicList.value.length;
|
||||
}
|
||||
|
||||
// 处理B站视频
|
||||
const bilibiliSongs: SongResult[] = [];
|
||||
for (const item of bilibiliItems) {
|
||||
try {
|
||||
const bvid = item.bilibiliData?.bvid;
|
||||
if (!bvid) continue;
|
||||
|
||||
const res = await getBilibiliVideoDetail(bvid);
|
||||
const videoDetail = res.data;
|
||||
|
||||
// 找到对应的分P
|
||||
const page = videoDetail.pages.find((p) => p.cid === item.bilibiliData?.cid);
|
||||
if (!page) continue;
|
||||
|
||||
bilibiliSongs.push({
|
||||
id: `${videoDetail.aid}--${page.cid}`,
|
||||
name: `${page.part || ''} - ${videoDetail.title}`,
|
||||
picUrl: getBilibiliProxyUrl(videoDetail.pic),
|
||||
ar: [
|
||||
{
|
||||
name: videoDetail.owner.name,
|
||||
id: videoDetail.owner.mid
|
||||
}
|
||||
],
|
||||
al: {
|
||||
name: videoDetail.title,
|
||||
picUrl: getBilibiliProxyUrl(videoDetail.pic)
|
||||
},
|
||||
source: 'bilibili',
|
||||
count: item.count || 0,
|
||||
bilibiliData: {
|
||||
bvid,
|
||||
cid: page.cid
|
||||
}
|
||||
} as SongResult);
|
||||
} catch (error) {
|
||||
console.error('获取B站视频详情失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 合并两种来源的数据,并保持原有顺序
|
||||
const newSongs = currentPageItems
|
||||
.map((item) => {
|
||||
if (item.source === 'bilibili') {
|
||||
return bilibiliSongs.find(
|
||||
(song) =>
|
||||
song.bilibiliData?.bvid === item.bilibiliData?.bvid &&
|
||||
song.bilibiliData?.cid === item.bilibiliData?.cid
|
||||
);
|
||||
}
|
||||
return neteaseSongs.find((song) => song.id === item.id);
|
||||
})
|
||||
.filter((song): song is SongResult => !!song);
|
||||
|
||||
if (currentPage.value === 1) {
|
||||
displayList.value = newSongs;
|
||||
} else {
|
||||
displayList.value = [...displayList.value, ...newSongs];
|
||||
}
|
||||
|
||||
noMore.value = displayList.value.length >= musicList.value.length;
|
||||
} catch (error) {
|
||||
console.error(t('history.getHistoryFailed'), error);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user