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/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/preload/index.d.ts b/src/preload/index.d.ts index 11fb828..13aac75 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -23,6 +23,7 @@ interface API { removeDownloadListeners: () => void; importCustomApiPlugin: () => Promise<{ name: string; content: string } | null>; invoke: (channel: string, ...args: any[]) => Promise; + getSearchSuggestions: (keyword: string) => Promise; } // 自定义IPC渲染进程通信接口 diff --git a/src/preload/index.ts b/src/preload/index.ts index 0d021e9..5b311d2 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -56,6 +56,8 @@ const api = { } return Promise.reject(new Error(`未授权的 IPC 通道: ${channel}`)); }, + // 搜索建议 + getSearchSuggestions: (keyword: string) => ipcRenderer.invoke('get-search-suggestions', keyword), }; // 创建带类型的ipcRenderer对象,暴露给渲染进程 diff --git a/src/renderer/api/search.ts b/src/renderer/api/search.ts index dbbc81b..4368bcf 100644 --- a/src/renderer/api/search.ts +++ b/src/renderer/api/search.ts @@ -1,3 +1,4 @@ +import { isElectron } from '@/utils'; import request from '@/utils/request'; interface IParams { @@ -12,3 +13,74 @@ export const getSearch = (params: IParams) => { params }); }; + +/** + * 搜索建议接口返回的数据结构 + */ +interface Suggestion { + keyword: string; +} + +interface KugouSuggestionResponse { + data: Suggestion[]; +} + +// 网易云搜索建议返回的数据结构(部分字段) +interface NeteaseSuggestResult { + result?: { + songs?: Array<{ name: string }>; + artists?: Array<{ name: string }>; + albums?: Array<{ name: string }>; + }; + code?: number; +} + +/** + * 从酷狗获取搜索建议 + * @param keyword 搜索关键词 + */ +export const getSearchSuggestions = async (keyword: string) => { + console.log('[API] getSearchSuggestions: 开始执行'); + + if (!keyword || !keyword.trim()) { + return Promise.resolve([]); + } + + console.log(`[API] getSearchSuggestions: 准备请求,关键词: "${keyword}"`); + + try { + let responseData: KugouSuggestionResponse; + if (isElectron) { + console.log('[API] Running in Electron, using IPC proxy.'); + responseData = await window.api.getSearchSuggestions(keyword); + } else { + // 非 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); + console.log('[API] getSearchSuggestions: 成功解析建议:', suggestions); + return suggestions; + } + + console.warn('[API] getSearchSuggestions: 响应数据格式不正确,返回空数组。'); + return []; + } catch (error) { + console.error('[API] getSearchSuggestions: 请求失败,错误信息:', error); + return []; + } +}; diff --git a/src/renderer/layout/components/SearchBar.vue b/src/renderer/layout/components/SearchBar.vue index 64fb603..bb47ca9 100644 --- a/src/renderer/layout/components/SearchBar.vue +++ b/src/renderer/layout/components/SearchBar.vue @@ -3,29 +3,65 @@
-
- + -