diff --git a/src/main/index.ts b/src/main/index.ts index efdfa6a..69f91d8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -16,6 +16,7 @@ import { setupUpdateHandlers } from './modules/update'; import { createMainWindow, initializeWindowManager, setAppQuitting } from './modules/window'; import { initWindowSizeManager } from './modules/window-size'; import { startMusicApi } from './server'; +import { initializeOtherApi } from './modules/otherApi'; // 导入所有图标 const iconPath = join(__dirname, '../../resources'); @@ -38,6 +39,8 @@ function initialize() { // 初始化文件管理 initializeFileManager(); + // 初始化其他 API (搜索建议等) + initializeOtherApi(); // 初始化窗口管理 initializeWindowManager(); // 初始化字体管理 diff --git a/src/main/modules/fileManager.ts b/src/main/modules/fileManager.ts index c6526e6..b7e74ed 100644 --- a/src/main/modules/fileManager.ts +++ b/src/main/modules/fileManager.ts @@ -275,28 +275,6 @@ export function initializeFileManager() { } } }); - - // 搜索建议 - ipcMain.handle('get-search-suggestions', async (_, keyword: string) => { - if (!keyword || !keyword.trim()) { - return []; - } - try { - console.log(`[Main Process Proxy] Forwarding suggestion request for: ${keyword}`); - const response = await axios.get('http://msearchcdn.kugou.com/new/app/i/search.php', { - params: { - cmd: 302, - keyword: keyword, - }, - timeout: 5000 - }); - return response.data; - - } catch (error: any) { - console.error('[Main Process Proxy] Failed to fetch search suggestions:', error.message); - return []; - } - }); } /** diff --git a/src/main/modules/otherApi.ts b/src/main/modules/otherApi.ts new file mode 100644 index 0000000..efdf8ee --- /dev/null +++ b/src/main/modules/otherApi.ts @@ -0,0 +1,28 @@ +import axios from 'axios'; +import { ipcMain } from 'electron'; + +/** + * 初始化其他杂项 API(如搜索建议等) + */ +export function initializeOtherApi() { + // 搜索建议(从酷狗获取) + ipcMain.handle('get-search-suggestions', async (_, keyword: string) => { + if (!keyword || !keyword.trim()) { + return []; + } + try { + console.log(`[Main Process Proxy] Forwarding suggestion request for: ${keyword}`); + const response = await axios.get('http://msearchcdn.kugou.com/new/app/i/search.php', { + params: { + cmd: 302, + keyword: keyword + }, + timeout: 5000 + }); + return response.data; + } catch (error: any) { + console.error('[Main Process Proxy] Failed to fetch search suggestions:', error.message); + return []; + } + }); +} diff --git a/src/renderer/api/search.ts b/src/renderer/api/search.ts index bc5cb7d..4368bcf 100644 --- a/src/renderer/api/search.ts +++ b/src/renderer/api/search.ts @@ -1,5 +1,5 @@ -import request from '@/utils/request'; import { isElectron } from '@/utils'; +import request from '@/utils/request'; interface IParams { keywords: string; @@ -25,6 +25,16 @@ interface KugouSuggestionResponse { data: Suggestion[]; } +// 网易云搜索建议返回的数据结构(部分字段) +interface NeteaseSuggestResult { + result?: { + songs?: Array<{ name: string }>; + artists?: Array<{ name: string }>; + albums?: Array<{ name: string }>; + }; + code?: number; +} + /** * 从酷狗获取搜索建议 * @param keyword 搜索关键词 @@ -44,11 +54,25 @@ export const getSearchSuggestions = async (keyword: string) => { console.log('[API] Running in Electron, using IPC proxy.'); responseData = await window.api.getSearchSuggestions(keyword); } else { - return []; + // 非 Electron 环境下,使用网易云接口 + const res = await request.get('/search/suggest', { + params: { keywords: keyword } + }); + + const result = res?.data?.result || {}; + const names: string[] = []; + if (Array.isArray(result.songs)) names.push(...result.songs.map((s) => s.name)); + if (Array.isArray(result.artists)) names.push(...result.artists.map((a) => a.name)); + if (Array.isArray(result.albums)) names.push(...result.albums.map((al) => al.name)); + + // 去重并截取前10个 + const unique = Array.from(new Set(names)).slice(0, 10); + console.log('[API] getSearchSuggestions: 网易云建议解析成功:', unique); + return unique; } if (responseData && Array.isArray(responseData.data)) { - const suggestions = responseData.data.map(item => item.keyword).slice(0, 10); + const suggestions = responseData.data.map((item) => item.keyword).slice(0, 10); console.log('[API] getSearchSuggestions: 成功解析建议:', suggestions); return suggestions; } diff --git a/src/renderer/layout/components/SearchBar.vue b/src/renderer/layout/components/SearchBar.vue index 1ca3d25..bb47ca9 100644 --- a/src/renderer/layout/components/SearchBar.vue +++ b/src/renderer/layout/components/SearchBar.vue @@ -5,25 +5,25 @@