mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-05-17 10:27:30 +08:00
feat: 历史记录页面 添加本地和云端两种记录支持,支持歌曲、歌单、专辑
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { useLocalStorage } from '@vueuse/core';
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
// 专辑历史记录类型
|
||||
export interface AlbumHistoryItem {
|
||||
id: number;
|
||||
name: string;
|
||||
picUrl?: string;
|
||||
size?: number; // 歌曲数量
|
||||
artist?: {
|
||||
name: string;
|
||||
id: number;
|
||||
};
|
||||
count?: number; // 播放次数
|
||||
lastPlayTime?: number; // 最后播放时间
|
||||
}
|
||||
|
||||
export const useAlbumHistory = () => {
|
||||
const albumHistory = useLocalStorage<AlbumHistoryItem[]>('albumHistory', []);
|
||||
|
||||
const addAlbum = (album: AlbumHistoryItem) => {
|
||||
const index = albumHistory.value.findIndex((item) => item.id === album.id);
|
||||
const now = Date.now();
|
||||
|
||||
if (index !== -1) {
|
||||
// 如果已存在,更新播放次数和时间,并移到最前面
|
||||
albumHistory.value[index].count = (albumHistory.value[index].count || 0) + 1;
|
||||
albumHistory.value[index].lastPlayTime = now;
|
||||
albumHistory.value.unshift(albumHistory.value.splice(index, 1)[0]);
|
||||
} else {
|
||||
// 如果不存在,添加新记录
|
||||
albumHistory.value.unshift({
|
||||
...album,
|
||||
count: 1,
|
||||
lastPlayTime: now
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const delAlbum = (album: AlbumHistoryItem) => {
|
||||
const index = albumHistory.value.findIndex((item) => item.id === album.id);
|
||||
if (index !== -1) {
|
||||
albumHistory.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
const albumList = ref(albumHistory.value);
|
||||
|
||||
watch(
|
||||
() => albumHistory.value,
|
||||
() => {
|
||||
albumList.value = albumHistory.value;
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
return {
|
||||
albumHistory,
|
||||
albumList,
|
||||
addAlbum,
|
||||
delAlbum
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
import { useLocalStorage } from '@vueuse/core';
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
// 歌单历史记录类型
|
||||
export interface PlaylistHistoryItem {
|
||||
id: number;
|
||||
name: string;
|
||||
coverImgUrl?: string;
|
||||
picUrl?: string; // 兼容字段
|
||||
trackCount?: number;
|
||||
playCount?: number;
|
||||
creator?: {
|
||||
nickname: string;
|
||||
userId: number;
|
||||
};
|
||||
count?: number; // 播放次数
|
||||
lastPlayTime?: number; // 最后播放时间
|
||||
}
|
||||
|
||||
export const usePlaylistHistory = () => {
|
||||
const playlistHistory = useLocalStorage<PlaylistHistoryItem[]>('playlistHistory', []);
|
||||
|
||||
const addPlaylist = (playlist: PlaylistHistoryItem) => {
|
||||
const index = playlistHistory.value.findIndex((item) => item.id === playlist.id);
|
||||
const now = Date.now();
|
||||
|
||||
if (index !== -1) {
|
||||
// 如果已存在,更新播放次数和时间,并移到最前面
|
||||
playlistHistory.value[index].count = (playlistHistory.value[index].count || 0) + 1;
|
||||
playlistHistory.value[index].lastPlayTime = now;
|
||||
playlistHistory.value.unshift(playlistHistory.value.splice(index, 1)[0]);
|
||||
} else {
|
||||
// 如果不存在,添加新记录
|
||||
playlistHistory.value.unshift({
|
||||
...playlist,
|
||||
count: 1,
|
||||
lastPlayTime: now
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const delPlaylist = (playlist: PlaylistHistoryItem) => {
|
||||
const index = playlistHistory.value.findIndex((item) => item.id === playlist.id);
|
||||
if (index !== -1) {
|
||||
playlistHistory.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
const playlistList = ref(playlistHistory.value);
|
||||
|
||||
watch(
|
||||
() => playlistHistory.value,
|
||||
() => {
|
||||
playlistList.value = playlistHistory.value;
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
return {
|
||||
playlistHistory,
|
||||
playlistList,
|
||||
addPlaylist,
|
||||
delPlaylist
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user