From 70677dfb1412982cef9bc223e53da9363b851b71 Mon Sep 17 00:00:00 2001 From: alger Date: Sat, 13 Sep 2025 23:58:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8D=E5=96=9C=E6=AC=A2=E7=9A=84?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=8F=AA=E6=9C=89=E6=AF=8F=E6=97=A5=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=E6=AD=8C=E6=9B=B2=E6=89=8D=E8=AF=B7=E6=B1=82=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E5=8E=BB=E9=99=A4=E4=B8=8D=E5=96=9C=E6=AC=A2?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/components/home/TopBanner.vue | 4 +- src/renderer/hooks/useSongItem.ts | 87 +++++++++++----------- src/renderer/store/index.ts | 2 +- src/renderer/store/modules/recommend.ts | 65 ++++++++-------- src/renderer/views/music/MusicListPage.vue | 43 +++++------ 5 files changed, 102 insertions(+), 99 deletions(-) diff --git a/src/renderer/components/home/TopBanner.vue b/src/renderer/components/home/TopBanner.vue index 884857f..89dce4e 100644 --- a/src/renderer/components/home/TopBanner.vue +++ b/src/renderer/components/home/TopBanner.vue @@ -136,7 +136,7 @@ import { getMusicDetail } from '@/api/music'; import { getUserPlaylist } from '@/api/user'; import { navigateToMusicList } from '@/components/common/MusicListNavigator'; import { useArtist } from '@/hooks/useArtist'; -import { usePlayerStore, useUserStore, useRecommendStore } from '@/store'; +import { usePlayerStore, useRecommendStore, useUserStore } from '@/store'; import { Playlist } from '@/types/list'; import type { IListDetail } from '@/types/listDetail'; import { SongResult } from '@/types/music'; @@ -161,7 +161,7 @@ const hotSingerData = ref(); const dayRecommendData = computed(() => { if (recommendStore.dailyRecommendSongs.length > 0) { return { - dailySongs: recommendStore.dailyRecommendSongs, + dailySongs: recommendStore.dailyRecommendSongs }; } return null; diff --git a/src/renderer/hooks/useSongItem.ts b/src/renderer/hooks/useSongItem.ts index 2c230e0..1d31d2b 100644 --- a/src/renderer/hooks/useSongItem.ts +++ b/src/renderer/hooks/useSongItem.ts @@ -1,4 +1,4 @@ -import { useDialog, useMessage } from 'naive-ui'; +import { useMessage } from 'naive-ui'; import { computed, ref } from 'vue'; import { useI18n } from 'vue-i18n'; @@ -7,16 +7,15 @@ import type { SongResult } from '@/types/music'; import { getImgUrl } from '@/utils'; import { getImageBackground } from '@/utils/linearColor'; +import { dislikeRecommendedSong } from "../api/music"; import { useArtist } from './useArtist'; import { useDownload } from './useDownload'; -import { dislikeRecommendedSong } from "../api/music"; export function useSongItem(props: { item: SongResult; canRemove?: boolean }) { const { t } = useI18n(); const playerStore = usePlayerStore(); const recommendStore = useRecommendStore(); const message = useMessage(); - const dialog = useDialog(); const { downloadMusic } = useDownload(); const { navigateToArtist } = useArtist(); @@ -88,8 +87,13 @@ export function useSongItem(props: { item: SongResult; canRemove?: boolean }) { } }; + // 判断当前歌曲是否为每日推荐歌曲 + const isDailyRecommendSong = computed(() => { + return recommendStore.dailyRecommendSongs.some(song => song.id === props.item.id); + }); + // 切换不喜欢状态 - const toggleDislike = async (e: Event) => { + const toggleDislike = async (e: Event) => { e && e.stopPropagation(); if (isDislike.value) { @@ -97,46 +101,43 @@ export function useSongItem(props: { item: SongResult; canRemove?: boolean }) { return; } - dialog.warning({ - title: t('songItem.dialog.dislike.title'), - content: t('songItem.dialog.dislike.content'), - positiveText: t('songItem.dialog.dislike.positiveText'), - negativeText: t('songItem.dialog.dislike.negativeText'), - onPositiveClick: async () => { - playerStore.addToDislikeList(props.item.id); - try { - console.log('发送不感兴趣请求,歌曲ID:', props.item.id); - const numericId = typeof props.item.id === 'string' ? parseInt(props.item.id) : props.item.id; - const response = await dislikeRecommendedSong(numericId); - if (response.data.data) { - console.log(response) - const newSongData = response.data.data; - const newSong: SongResult = { - ...newSongData, - name: newSongData.name, - id: newSongData.id, - picUrl: newSongData.al?.picUrl || newSongData.album?.picUrl, - ar: newSongData.ar || newSongData.artists, - al: newSongData.al || newSongData.album, - song: { - ...newSongData.song, - id: newSongData.id, - name: newSongData.name, - artists: newSongData.ar || newSongData.artists, - album: newSongData.al || newSongData.album, - }, - source: 'netease', - count: 0, - }; - recommendStore.replaceSongInDailyRecommend(props.item.id, newSong); - } else { - console.warn('标记不感兴趣API成功,但未返回新歌曲。', response.data); - } - } catch (error) { - console.error('发送不感兴趣请求时出错:', error); - } + playerStore.addToDislikeList(props.item.id); + + // 只有当前歌曲是每日推荐歌曲时才调用接口 + if (!isDailyRecommendSong.value) { + return; + } + try { + console.log('发送不感兴趣请求,歌曲ID:', props.item.id); + const numericId = typeof props.item.id === 'string' ? parseInt(props.item.id) : props.item.id; + const response = await dislikeRecommendedSong(numericId); + if (response.data.data) { + console.log(response) + const newSongData = response.data.data; + const newSong: SongResult = { + ...newSongData, + name: newSongData.name, + id: newSongData.id, + picUrl: newSongData.al?.picUrl || newSongData.album?.picUrl, + ar: newSongData.ar || newSongData.artists, + al: newSongData.al || newSongData.album, + song: { + ...newSongData.song, + id: newSongData.id, + name: newSongData.name, + artists: newSongData.ar || newSongData.artists, + album: newSongData.al || newSongData.album, + }, + source: 'netease', + count: 0, + }; + recommendStore.replaceSongInDailyRecommend(props.item.id, newSong); + } else { + console.warn('标记不感兴趣API成功,但未返回新歌曲。', response.data); } - }); + } catch (error) { + console.error('发送不感兴趣请求时出错:', error); + } }; // 添加到下一首播放 diff --git a/src/renderer/store/index.ts b/src/renderer/store/index.ts index bd8c990..fe4d61f 100644 --- a/src/renderer/store/index.ts +++ b/src/renderer/store/index.ts @@ -15,9 +15,9 @@ export * from './modules/lyric'; export * from './modules/menu'; export * from './modules/music'; export * from './modules/player'; +export * from './modules/recommend'; export * from './modules/search'; export * from './modules/settings'; export * from './modules/user'; -export * from './modules/recommend'; export default pinia; diff --git a/src/renderer/store/modules/recommend.ts b/src/renderer/store/modules/recommend.ts index 0b003d4..616ba65 100644 --- a/src/renderer/store/modules/recommend.ts +++ b/src/renderer/store/modules/recommend.ts @@ -1,42 +1,43 @@ import { defineStore } from 'pinia'; import { ref } from 'vue'; + import { getDayRecommend } from '@/api/home'; -import type { SongResult } from '@/types/music'; import type { IDayRecommend } from '@/types/day_recommend'; +import type { SongResult } from '@/types/music'; export const useRecommendStore = defineStore('recommend', () => { - const dailyRecommendSongs = ref([]); + const dailyRecommendSongs = ref([]); - const fetchDailyRecommendSongs = async () => { - try { - const { data } = await getDayRecommend(); - const recommendData = data.data as unknown as IDayRecommend; + const fetchDailyRecommendSongs = async () => { + try { + const { data } = await getDayRecommend(); + const recommendData = data.data as unknown as IDayRecommend; - if (recommendData && Array.isArray(recommendData.dailySongs)) { - dailyRecommendSongs.value = recommendData.dailySongs as any; - console.log(`[Recommend Store] 已加载 ${recommendData.dailySongs.length} 首每日推荐歌曲。`); - } else { - dailyRecommendSongs.value = []; - } - } catch (error) { - console.error('[Recommend Store] 获取每日推荐失败:', error); - dailyRecommendSongs.value = []; - } - }; + if (recommendData && Array.isArray(recommendData.dailySongs)) { + dailyRecommendSongs.value = recommendData.dailySongs as any; + console.log(`[Recommend Store] 已加载 ${recommendData.dailySongs.length} 首每日推荐歌曲。`); + } else { + dailyRecommendSongs.value = []; + } + } catch (error) { + console.error('[Recommend Store] 获取每日推荐失败:', error); + dailyRecommendSongs.value = []; + } + }; - const replaceSongInDailyRecommend = (oldSongId: number | string, newSong: SongResult) => { - const index = dailyRecommendSongs.value.findIndex(song => song.id === oldSongId); - if (index !== -1) { - dailyRecommendSongs.value.splice(index, 1, newSong as any); - console.log(`[Recommend Store] 已将歌曲 ${oldSongId} 替换为 ${newSong.name}`); - } else { - console.warn(`[Recommend Store] 未在日推列表中找到要替换的歌曲ID: ${oldSongId}`); - } - }; + const replaceSongInDailyRecommend = (oldSongId: number | string, newSong: SongResult) => { + const index = dailyRecommendSongs.value.findIndex((song) => song.id === oldSongId); + if (index !== -1) { + dailyRecommendSongs.value.splice(index, 1, newSong as any); + console.log(`[Recommend Store] 已将歌曲 ${oldSongId} 替换为 ${newSong.name}`); + } else { + console.warn(`[Recommend Store] 未在日推列表中找到要替换的歌曲ID: ${oldSongId}`); + } + }; - return { - dailyRecommendSongs, - fetchDailyRecommendSongs, - replaceSongInDailyRecommend, - }; -}); \ No newline at end of file + return { + dailyRecommendSongs, + fetchDailyRecommendSongs, + replaceSongInDailyRecommend + }; +}); diff --git a/src/renderer/views/music/MusicListPage.vue b/src/renderer/views/music/MusicListPage.vue index 0ea8156..5c33efb 100644 --- a/src/renderer/views/music/MusicListPage.vue +++ b/src/renderer/views/music/MusicListPage.vue @@ -354,11 +354,13 @@ const getCoverImgUrl = computed(() => { const filteredSongs = computed(() => { // 1. 确定数据源是来自store的完整列表(日推)还是来自本地分页的列表(其他) const sourceList = isDailyRecommend.value - ? songList.value // 如果是日推,直接使用来自 recommendStore 的完整、响应式列表 - : displayedSongs.value; // 否则,使用用于分页加载的 displayedSongs + ? songList.value // 如果是日推,直接使用来自 recommendStore 的完整、响应式列表 + : displayedSongs.value; // 否则,使用用于分页加载的 displayedSongs // 2. 过滤不喜欢的歌曲 - const dislikeFilteredList = sourceList.filter(song => !playerStore.dislikeList.includes(song.id)); + const dislikeFilteredList = sourceList.filter( + (song) => !playerStore.dislikeList.includes(song.id) + ); // ================================================================= // 3. 如果没有搜索词,直接返回处理后的列表 @@ -388,12 +390,12 @@ const filteredSongs = computed(() => { }); return ( - nameMatch || - albumMatch || - artistsMatch || - namePinyinMatch || - albumPinyinMatch || - artistsPinyinMatch + nameMatch || + albumMatch || + artistsMatch || + namePinyinMatch || + albumPinyinMatch || + artistsPinyinMatch ); }); }); @@ -774,17 +776,17 @@ watch(searchKeyword, () => { }); watch( - songList, - (newSongs) => { - resetListState(); - initSongList(newSongs); - if (hasMore.value && listInfo.value?.trackIds) { - setTimeout(() => { - loadMoreSongs(); - }, 300); - } - }, - { immediate: true } + songList, + (newSongs) => { + resetListState(); + initSongList(newSongs); + if (hasMore.value && listInfo.value?.trackIds) { + setTimeout(() => { + loadMoreSongs(); + }, 300); + } + }, + { immediate: true } ); // 组件卸载时清理状态 @@ -929,7 +931,6 @@ const handleBatchDownload = async () => { await batchDownloadMusic(selectedSongsList); cancelSelect(); }; -