From 914e04350291e3c1b4e3b8b11c9c47c684ab82e2 Mon Sep 17 00:00:00 2001 From: alger Date: Fri, 17 Jan 2025 22:35:42 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=8E=BB=E9=99=A4=E6=AD=8C?= =?UTF-8?q?=E6=9B=B2=E7=BC=93=E5=AD=98=20=E4=BC=98=E5=8C=96=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E4=B8=8B=E4=B8=80=E9=A6=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/unblockMusic.ts | 89 +-------------------------- src/renderer/hooks/MusicHook.ts | 6 +- src/renderer/services/audioService.ts | 2 +- 3 files changed, 7 insertions(+), 90 deletions(-) diff --git a/src/main/unblockMusic.ts b/src/main/unblockMusic.ts index 54663ed..d392bcb 100644 --- a/src/main/unblockMusic.ts +++ b/src/main/unblockMusic.ts @@ -1,5 +1,4 @@ import match from '@unblockneteasemusic/server'; -import Store from 'electron-store'; type Platform = 'qq' | 'migu' | 'kugou' | 'pyncmd' | 'joox' | 'kuwo' | 'bilibili' | 'youtube'; @@ -28,73 +27,6 @@ interface UnblockResult { }; } -interface CacheData extends UnblockResult { - timestamp: number; -} - -interface CacheStore { - [key: string]: CacheData; -} - -// 初始化缓存存储 -const store = new Store({ - name: 'unblock-cache' -}); - -// 缓存过期时间(24小时) -const CACHE_EXPIRY = 24 * 60 * 60 * 1000; - -/** - * 检查缓存是否有效 - * @param cacheData 缓存数据 - * @returns boolean - */ -const isCacheValid = (cacheData: CacheData | null): boolean => { - if (!cacheData) return false; - const now = Date.now(); - return now - cacheData.timestamp < CACHE_EXPIRY; -}; - -/** - * 从缓存中获取数据 - * @param id 歌曲ID - * @returns CacheData | null - */ -const getFromCache = (id: string | number): CacheData | null => { - const cacheData = store.get(String(id)) as CacheData | null; - if (isCacheValid(cacheData)) { - return cacheData; - } - // 清除过期缓存 - store.delete(String(id)); - return null; -}; - -/** - * 将数据存入缓存 - * @param id 歌曲ID - * @param data 解析结果 - */ -const saveToCache = (id: string | number, data: UnblockResult): void => { - const cacheData: CacheData = { - ...data, - timestamp: Date.now() - }; - store.set(String(id), cacheData); -}; - -/** - * 清理过期缓存 - */ -const cleanExpiredCache = (): void => { - const allData = store.store; - Object.entries(allData).forEach(([id, data]) => { - if (!isCacheValid(data)) { - store.delete(id); - } - }); -}; - /** * 音乐解析函数 * @param id 歌曲ID @@ -107,12 +39,6 @@ const unblockMusic = async ( songData: SongData, retryCount = 3 ): Promise => { - // 检查缓存 - const cachedData = getFromCache(id); - if (cachedData) { - return cachedData; - } - // 所有可用平台 const platforms: Platform[] = ['migu', 'kugou', 'pyncmd', 'joox', 'kuwo', 'bilibili', 'youtube']; @@ -128,9 +54,6 @@ const unblockMusic = async ( } } }; - - // 保存到缓存 - saveToCache(id, result); return result; } catch (err) { if (attempt < retryCount) { @@ -149,14 +72,4 @@ const unblockMusic = async ( return retry(1); }; -// 定期清理过期缓存(每小时执行一次) -setInterval(cleanExpiredCache, 60 * 60 * 1000); - -export { - cleanExpiredCache, // 导出清理缓存函数,以便手动调用 - type Platform, - type ResponseData, - type SongData, - unblockMusic, - type UnblockResult -}; +export { type Platform, type ResponseData, type SongData, unblockMusic, type UnblockResult }; diff --git a/src/renderer/hooks/MusicHook.ts b/src/renderer/hooks/MusicHook.ts index 3c6de6f..4e9218d 100644 --- a/src/renderer/hooks/MusicHook.ts +++ b/src/renderer/hooks/MusicHook.ts @@ -6,7 +6,7 @@ import store from '@/store'; import type { Artist, ILyricText, SongResult } from '@/type/music'; import { isElectron } from '@/utils'; import { getTextColors } from '@/utils/linearColor'; - +import { createDiscreteApi } from 'naive-ui'; const windowData = window as any; export const lrcArray = ref([]); // 歌词数组 @@ -51,6 +51,8 @@ document.onkeyup = (e) => { } }; +const { message } = createDiscreteApi(['message']); + watch( () => store.state.playMusicUrl, async (newVal) => { @@ -60,8 +62,10 @@ watch( sound.value = newSound as Howl; setupAudioListeners(); } catch (error) { + console.error('播放音频失败:', error); store.commit('setPlayMusic', false); + message.error('当前歌曲播放失败,播放下一首'); // 下一首 store.commit('nextPlay'); } diff --git a/src/renderer/services/audioService.ts b/src/renderer/services/audioService.ts index dc4a7ed..432332b 100644 --- a/src/renderer/services/audioService.ts +++ b/src/renderer/services/audioService.ts @@ -128,7 +128,7 @@ class AudioService { } return new Promise((resolve, reject) => { let retryCount = 0; - const maxRetries = 3; + const maxRetries = 1; const tryPlay = () => { if (this.currentSound) {