feat: 收藏功能改为接口对接

This commit is contained in:
alger
2025-01-04 16:58:08 +08:00
parent 11ced6b418
commit 43c64b1b43
3 changed files with 47 additions and 10 deletions

View File

@@ -24,3 +24,13 @@ export const getParsingMusicUrl = (id: number) => {
} }
return requestMusic.get<any>('/music', { params: { id } }); return requestMusic.get<any>('/music', { params: { id } });
}; };
// 收藏歌曲
export const likeSong = (id: number, like: boolean = true) => {
return request.get('/like', { params: { id, like } });
};
// 获取用户喜欢的音乐列表
export const getLikedList = () => {
return request.get('/likelist');
};

View File

@@ -5,6 +5,7 @@ import homeRouter from '@/router/home';
import type { SongResult } from '@/type/music'; import type { SongResult } from '@/type/music';
import { applyTheme, getCurrentTheme, ThemeType } from '@/utils/theme'; import { applyTheme, getCurrentTheme, ThemeType } from '@/utils/theme';
import { isElectron } from '@/utils'; import { isElectron } from '@/utils';
import { likeSong, getLikedList } from '@/api/music';
// 默认设置 // 默认设置
const defaultSettings = { const defaultSettings = {
@@ -102,15 +103,25 @@ const mutations = {
localStorage.setItem('appSettings', JSON.stringify(setData)); localStorage.setItem('appSettings', JSON.stringify(setData));
} }
}, },
addToFavorite(state: State, songId: number) { async addToFavorite(state: State, songId: number) {
if (!state.favoriteList.includes(songId)) { try {
state.favoriteList = [songId, ...state.favoriteList]; await likeSong(songId, true);
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList)); if (!state.favoriteList.includes(songId)) {
state.favoriteList = [songId, ...state.favoriteList];
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
}
} catch (error) {
console.error('收藏歌曲失败:', error);
} }
}, },
removeFromFavorite(state: State, songId: number) { async removeFromFavorite(state: State, songId: number) {
state.favoriteList = state.favoriteList.filter((id) => id !== songId); try {
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList)); await likeSong(songId, false);
state.favoriteList = state.favoriteList.filter((id) => id !== songId);
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
} catch (error) {
console.error('取消收藏歌曲失败:', error);
}
}, },
togglePlayMode(state: State) { togglePlayMode(state: State) {
state.playMode = (state.playMode + 1) % 3; state.playMode = (state.playMode + 1) % 3;
@@ -145,6 +156,22 @@ const actions = {
}, },
initializeTheme({ state }: { state: State }) { initializeTheme({ state }: { state: State }) {
applyTheme(state.theme); applyTheme(state.theme);
},
async initializeFavoriteList({ state }: { state: State }) {
try {
const res = await getLikedList();
if (res.data?.ids) {
state.favoriteList = res.data.ids.reverse();
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
}
} catch (error) {
console.error('获取收藏列表失败:', error);
// 如果获取失败,使用本地存储的数据
const localFavoriteList = localStorage.getItem('favoriteList');
if (localFavoriteList) {
state.favoriteList = JSON.parse(localFavoriteList);
}
}
} }
}; };

View File

@@ -54,7 +54,7 @@ const scrollbarRef = ref();
// 无限滚动相关 // 无限滚动相关
const pageSize = 16; const pageSize = 16;
const currentPage = ref(1); const currentPage = ref(1);
const props = defineProps({ const props = defineProps({
isComponent: { isComponent: {
type: Boolean, type: Boolean,
@@ -64,10 +64,9 @@ const props = defineProps({
// 获取当前页的收藏歌曲ID // 获取当前页的收藏歌曲ID
const getCurrentPageIds = () => { const getCurrentPageIds = () => {
const reversedList = [...favoriteList.value];
const startIndex = (currentPage.value - 1) * pageSize; const startIndex = (currentPage.value - 1) * pageSize;
const endIndex = startIndex + pageSize; const endIndex = startIndex + pageSize;
return reversedList.slice(startIndex, endIndex); return favoriteList.value.slice(startIndex, endIndex);
}; };
// 获取收藏歌曲详情 // 获取收藏歌曲详情
@@ -120,6 +119,7 @@ const handleScroll = (e: any) => {
}; };
onMounted(() => { onMounted(() => {
store.dispatch('initializeFavoriteList');
getFavoriteSongs(); getFavoriteSongs();
}); });