diff --git a/templates/index.html b/templates/index.html index c0fdde1..3534259 100644 --- a/templates/index.html +++ b/templates/index.html @@ -56,8 +56,20 @@ + + + -
+

热门模型 @@ -241,8 +253,123 @@ // 搜索 async function search() { const keyword = document.getElementById('searchInput').value.trim(); - if (!keyword) return; - window.location.href = `/models?q=${encodeURIComponent(keyword)}`; + if (!keyword) { + // 清空搜索时恢复原页面 + document.getElementById('searchResults').classList.add('hidden'); + document.getElementById('hotModels').classList.remove('hidden'); + document.getElementById('statsCards').classList.remove('hidden'); + document.getElementById('categoryCards').parentElement.classList.remove('hidden'); + return; + } + + // 调用全局搜索 API + const res = await fetch(`/api/search?q=${encodeURIComponent(keyword)}`); + const data = await res.json(); + + // 显示搜索结果区域 + document.getElementById('searchResults').classList.remove('hidden'); + document.getElementById('searchKeyword').textContent = `"${keyword}"`; + document.getElementById('hotModels').classList.add('hidden'); + + // 渲染搜索结果 + renderSearchResults(data, keyword); + } + + // 渲染搜索结果 + function renderSearchResults(data, keyword) { + const models = data.models || []; + const gpus = data.gpus || []; + const cpus = data.cpus || []; + const total = models.length + gpus.length + cpus.length; + + if (total === 0) { + document.getElementById('searchContent').innerHTML = ` +
+ +

未找到与 "${keyword}" 相关的结果

+
+ `; + return; + } + + let html = `
找到 ${total} 条结果
`; + + // AI模型结果 + if (models.length > 0) { + html += ` +
+

+ + AI模型 (${models.length}) +

+ +
+ `; + } + + // GPU结果 + if (gpus.length > 0) { + html += ` +
+

+ + GPU显卡 (${gpus.length}) +

+ +
+ `; + } + + // CPU结果 + if (cpus.length > 0) { + html += ` +
+

+ + CPU处理器 (${cpus.length}) +

+ +
+ `; + } + + document.getElementById('searchContent').innerHTML = html; } // 初始化