diff --git a/electron.vite.config.ts b/electron.vite.config.ts index 82aa7d7..372e62b 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -40,7 +40,14 @@ export default defineConfig({ ], publicDir: resolve('resources'), server: { - host: '0.0.0.0' + host: '0.0.0.0', + proxy: { + '/kugou': { + target: 'http://msearchcdn.kugou.com', // 目标服务器地址 + changeOrigin: true, // 必须设置为 true + rewrite: (path) => path.replace(/^\/kugou/, '') // 重写请求路径,去掉 /kugou 前缀 + } + } } } }); diff --git a/src/renderer/api/search.ts b/src/renderer/api/search.ts index dbbc81b..794a4c4 100644 --- a/src/renderer/api/search.ts +++ b/src/renderer/api/search.ts @@ -1,4 +1,5 @@ import request from '@/utils/request'; +import axios from 'axios'; interface IParams { keywords: string; @@ -12,3 +13,49 @@ export const getSearch = (params: IParams) => { params }); }; + +/** + * 搜索建议接口返回的数据结构 + */ +interface Suggestion { + keyword: string; +} + +interface KugouSuggestionResponse { + data: Suggestion[]; +} + +/** + * 从酷狗获取搜索建议 + * @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 { + const response = await axios.get('/kugou/new/app/i/search.php', { + params: { + cmd: 302, + keyword: keyword, + }, + }); + + if (response.data && Array.isArray(response.data.data)) { + const suggestions = response.data.data.map(item => item.keyword).slice(0, 8); + 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..1ca3d25 100644 --- a/src/renderer/layout/components/SearchBar.vue +++ b/src/renderer/layout/components/SearchBar.vue @@ -3,29 +3,63 @@
-
- + -