feat: 修复未登录 收藏问题

This commit is contained in:
alger
2025-01-05 15:01:55 +08:00
parent 43c64b1b43
commit 17ce268da6

View File

@@ -105,7 +105,7 @@ const mutations = {
}, },
async addToFavorite(state: State, songId: number) { async addToFavorite(state: State, songId: number) {
try { try {
await likeSong(songId, true); state.user && localStorage.getItem('token') && await likeSong(songId, true);
if (!state.favoriteList.includes(songId)) { if (!state.favoriteList.includes(songId)) {
state.favoriteList = [songId, ...state.favoriteList]; state.favoriteList = [songId, ...state.favoriteList];
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList)); localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
@@ -116,7 +116,7 @@ const mutations = {
}, },
async removeFromFavorite(state: State, songId: number) { async removeFromFavorite(state: State, songId: number) {
try { try {
await likeSong(songId, false); state.user && localStorage.getItem('token') && await likeSong(songId, false);
state.favoriteList = state.favoriteList.filter((id) => id !== songId); state.favoriteList = state.favoriteList.filter((id) => id !== songId);
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList)); localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
} catch (error) { } catch (error) {