mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-05-17 10:27:30 +08:00
refactor: 重构历史记录
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
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
|
||||
};
|
||||
};
|
||||
@@ -1,38 +0,0 @@
|
||||
import { useLocalStorage } from '@vueuse/core';
|
||||
|
||||
import type { SongResult } from '@/types/music';
|
||||
|
||||
export const useMusicHistory = () => {
|
||||
const musicHistory = useLocalStorage<SongResult[]>('musicHistory', []);
|
||||
|
||||
const addMusic = (music: SongResult) => {
|
||||
const index = musicHistory.value.findIndex((item) => item.id === music.id);
|
||||
if (index !== -1) {
|
||||
musicHistory.value[index].count = (musicHistory.value[index].count || 0) + 1;
|
||||
musicHistory.value.unshift(musicHistory.value.splice(index, 1)[0]);
|
||||
} else {
|
||||
musicHistory.value.unshift({ ...music, count: 1 });
|
||||
}
|
||||
};
|
||||
|
||||
const delMusic = (music: SongResult) => {
|
||||
const index = musicHistory.value.findIndex((item) => item.id === music.id);
|
||||
if (index !== -1) {
|
||||
musicHistory.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
const musicList = ref(musicHistory.value);
|
||||
watch(
|
||||
() => musicHistory.value,
|
||||
() => {
|
||||
musicList.value = musicHistory.value;
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
musicHistory,
|
||||
musicList,
|
||||
addMusic,
|
||||
delMusic
|
||||
};
|
||||
};
|
||||
@@ -1,65 +0,0 @@
|
||||
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
|
||||
};
|
||||
};
|
||||
@@ -1,50 +0,0 @@
|
||||
import { useLocalStorage } from '@vueuse/core';
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
import type { DjProgram } from '@/types/podcast';
|
||||
|
||||
export const usePodcastHistory = () => {
|
||||
const podcastHistory = useLocalStorage<DjProgram[]>('podcastHistory', []);
|
||||
|
||||
const addPodcast = (program: DjProgram) => {
|
||||
const index = podcastHistory.value.findIndex((item) => item.id === program.id);
|
||||
if (index !== -1) {
|
||||
podcastHistory.value.unshift(podcastHistory.value.splice(index, 1)[0]);
|
||||
} else {
|
||||
podcastHistory.value.unshift(program);
|
||||
}
|
||||
|
||||
if (podcastHistory.value.length > 100) {
|
||||
podcastHistory.value.pop();
|
||||
}
|
||||
};
|
||||
|
||||
const delPodcast = (program: DjProgram) => {
|
||||
const index = podcastHistory.value.findIndex((item) => item.id === program.id);
|
||||
if (index !== -1) {
|
||||
podcastHistory.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
const clearPodcastHistory = () => {
|
||||
podcastHistory.value = [];
|
||||
};
|
||||
|
||||
const podcastList = ref(podcastHistory.value);
|
||||
|
||||
watch(
|
||||
() => podcastHistory.value,
|
||||
() => {
|
||||
podcastList.value = podcastHistory.value;
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
return {
|
||||
podcastHistory,
|
||||
podcastList,
|
||||
addPodcast,
|
||||
delPodcast,
|
||||
clearPodcastHistory
|
||||
};
|
||||
};
|
||||
@@ -1,66 +0,0 @@
|
||||
import { useLocalStorage } from '@vueuse/core';
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
export type PodcastRadioHistoryItem = {
|
||||
id: number;
|
||||
name: string;
|
||||
picUrl: string;
|
||||
desc?: string;
|
||||
dj?: {
|
||||
nickname: string;
|
||||
userId: number;
|
||||
};
|
||||
count?: number;
|
||||
lastPlayTime?: number;
|
||||
type?: string;
|
||||
};
|
||||
|
||||
export const usePodcastRadioHistory = () => {
|
||||
const podcastRadioHistory = useLocalStorage<PodcastRadioHistoryItem[]>('podcastRadioHistory', []);
|
||||
|
||||
const addPodcastRadio = (radio: PodcastRadioHistoryItem) => {
|
||||
const index = podcastRadioHistory.value.findIndex((item) => item.id === radio.id);
|
||||
const now = Date.now();
|
||||
|
||||
if (index !== -1) {
|
||||
const existing = podcastRadioHistory.value.splice(index, 1)[0];
|
||||
existing.count = (existing.count || 0) + 1;
|
||||
existing.lastPlayTime = now;
|
||||
podcastRadioHistory.value.unshift(existing);
|
||||
} else {
|
||||
podcastRadioHistory.value.unshift({
|
||||
...radio,
|
||||
count: 1,
|
||||
lastPlayTime: now
|
||||
});
|
||||
}
|
||||
|
||||
if (podcastRadioHistory.value.length > 100) {
|
||||
podcastRadioHistory.value.pop();
|
||||
}
|
||||
};
|
||||
|
||||
const delPodcastRadio = (radio: PodcastRadioHistoryItem) => {
|
||||
const index = podcastRadioHistory.value.findIndex((item) => item.id === radio.id);
|
||||
if (index !== -1) {
|
||||
podcastRadioHistory.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
const podcastRadioList = ref(podcastRadioHistory.value);
|
||||
|
||||
watch(
|
||||
() => podcastRadioHistory.value,
|
||||
() => {
|
||||
podcastRadioList.value = podcastRadioHistory.value;
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
return {
|
||||
podcastRadioHistory,
|
||||
podcastRadioList,
|
||||
addPodcastRadio,
|
||||
delPodcastRadio
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user