diff --git a/electron.vite.config.ts b/electron.vite.config.ts index 372e62b..82aa7d7 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -40,14 +40,7 @@ export default defineConfig({ ], publicDir: resolve('resources'), server: { - host: '0.0.0.0', - proxy: { - '/kugou': { - target: 'http://msearchcdn.kugou.com', // 目标服务器地址 - changeOrigin: true, // 必须设置为 true - rewrite: (path) => path.replace(/^\/kugou/, '') // 重写请求路径,去掉 /kugou 前缀 - } - } + host: '0.0.0.0' } } }); diff --git a/src/main/modules/fileManager.ts b/src/main/modules/fileManager.ts index b7e74ed..c6526e6 100644 --- a/src/main/modules/fileManager.ts +++ b/src/main/modules/fileManager.ts @@ -275,6 +275,28 @@ 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/preload/index.d.ts b/src/preload/index.d.ts index 4af0cd2..e32db3a 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -22,6 +22,7 @@ interface API { onLanguageChanged: (callback: (locale: string) => void) => void; removeDownloadListeners: () => void; invoke: (channel: string, ...args: any[]) => Promise; + getSearchSuggestions: (keyword: string) => Promise; } // 自定义IPC渲染进程通信接口 diff --git a/src/preload/index.ts b/src/preload/index.ts index c47ac7e..6da0d15 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -54,7 +54,9 @@ const api = { return ipcRenderer.invoke(channel, ...args); } 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 794a4c4..bc5cb7d 100644 --- a/src/renderer/api/search.ts +++ b/src/renderer/api/search.ts @@ -1,5 +1,5 @@ import request from '@/utils/request'; -import axios from 'axios'; +import { isElectron } from '@/utils'; interface IParams { keywords: string; @@ -39,15 +39,16 @@ export const getSearchSuggestions = async (keyword: string) => { console.log(`[API] getSearchSuggestions: 准备请求,关键词: "${keyword}"`); try { - const response = await axios.get('/kugou/new/app/i/search.php', { - params: { - cmd: 302, - keyword: keyword, - }, - }); + let responseData: KugouSuggestionResponse; + if (isElectron) { + console.log('[API] Running in Electron, using IPC proxy.'); + responseData = await window.api.getSearchSuggestions(keyword); + } else { + return []; + } - if (response.data && Array.isArray(response.data.data)) { - const suggestions = response.data.data.map(item => item.keyword).slice(0, 8); + if (responseData && Array.isArray(responseData.data)) { + const suggestions = responseData.data.map(item => item.keyword).slice(0, 10); console.log('[API] getSearchSuggestions: 成功解析建议:', suggestions); return suggestions; }