From dcd13dec4f159b6064f017e3c8c6a1da512f2e6b Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Mon, 13 Jul 2026 17:20:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=93=E5=8F=96=E5=89=8D=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=BA=93=E6=98=AF=E5=90=A6=E5=B7=B2=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9B=B8=E5=90=8C=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 抓取前先搜索内容库检查 URL 是否已存在 - 已存在的链接标记为黄色,显示'已存在' - 避免重复抓取和保存 - 添加 checkUrlExists 函数 --- static/css/search.css | 5 +++++ static/js/search.js | 45 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/static/css/search.css b/static/css/search.css index e9ec94a..82e74d6 100644 --- a/static/css/search.css +++ b/static/css/search.css @@ -217,6 +217,11 @@ background: #f0fdf4; } +.result-item.exists { + border-color: #f59e0b; + background: #fffbeb; +} + .result-item.fetched { border-color: #3b82f6; } diff --git a/static/js/search.js b/static/js/search.js index 3ab3319..b35f601 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -127,6 +127,21 @@ function displayResults() { `).join(''); } +// 检查URL是否已在内容库 +async function checkUrlExists(url) { + try { + const response = await fetch(`${API_BASE}/api/articles/search?q=${encodeURIComponent(url)}`); + const data = await response.json(); + if (data.success && data.articles && data.articles.length > 0) { + // 检查是否有完全匹配的 URL + return data.articles.some(a => a.url === url); + } + } catch (error) { + console.error('检查URL失败:', error); + } + return false; +} + // 抓取单个结果 async function fetchResult(index) { const result = searchResults[index]; @@ -134,6 +149,28 @@ async function fetchResult(index) { const btn = document.getElementById(`fetch-btn-${index}`); btn.disabled = true; + btn.innerHTML = ' 检查中...'; + + // 先检查内容库是否已存在 + const exists = await checkUrlExists(result.url); + if (exists) { + searchResults[index].fetched = true; + searchResults[index].saved = true; + searchResults[index].content = '[内容库中已存在此链接]'; + btn.innerHTML = ' 已存在'; + btn.className = 'btn btn-sm btn-warning'; + + const saveBtn = document.getElementById(`save-btn-${index}`); + saveBtn.disabled = true; + saveBtn.innerHTML = ' 已存在'; + + const item = document.getElementById(`result-${index}`); + item.classList.add('saved'); + + showToast('该链接已在内容库中', 'warning'); + return; + } + btn.innerHTML = ' 抓取中...'; try { @@ -178,10 +215,16 @@ async function saveResult(index) { const result = searchResults[index]; if (!result) return; + // 如果已经保存(可能是内容库中已存在),直接返回 + if (result.saved) { + return; + } + // 如果未抓取,先抓取 if (!result.fetched) { await fetchResult(index); - if (!searchResults[index].fetched) return; + // 抓取后如果已标记为 saved(内容库已存在),不继续保存 + if (searchResults[index].saved) return; } const btn = document.getElementById(`save-btn-${index}`);