diff --git a/templates/admin/site/edit.html b/templates/admin/site/edit.html index 6594b5f..313c996 100644 --- a/templates/admin/site/edit.html +++ b/templates/admin/site/edit.html @@ -55,6 +55,31 @@ border: 1px solid rgba(239, 68, 68, 0.3); color: #f87171; } + .fetch-news-btn { + margin-top: 10px; + margin-bottom: 15px; + background-color: #e6a23c; + color: white; + } + .fetch-news-btn:hover { + background-color: #cf9236; + } + .news-status { + margin-top: 10px; + padding: 10px; + border-radius: 8px; + display: none; + } + .news-status.success { + background-color: rgba(34, 197, 94, 0.1); + border: 1px solid rgba(34, 197, 94, 0.3); + color: #4ade80; + } + .news-status.error { + background-color: rgba(239, 68, 68, 0.1); + border: 1px solid rgba(239, 68, 68, 0.3); + color: #f87171; + } .auto-fetch-btn .loading-icon, .generate-tags-btn .loading-icon { display: none; } @@ -658,6 +683,79 @@ document.addEventListener('DOMContentLoaded', function() { featuresStatusDiv.style.display = 'block'; } } + + // 在 News Keywords 字段后添加"获取新闻"按钮 + const newsKeywordsField = document.querySelector('input[name="news_keywords"]'); + if (newsKeywordsField) { + // 检查是否已经添加过按钮 + if (document.querySelector('.fetch-news-btn')) { + return; + } + + // 创建获取新闻按钮 + const fetchNewsBtn = document.createElement('button'); + fetchNewsBtn.type = 'button'; + fetchNewsBtn.className = 'btn fetch-news-btn'; + fetchNewsBtn.innerHTML = '📰 手动获取新闻'; + + const newsStatusDiv = document.createElement('div'); + newsStatusDiv.className = 'news-status'; + + newsKeywordsField.parentNode.appendChild(fetchNewsBtn); + newsKeywordsField.parentNode.appendChild(newsStatusDiv); + + // 从URL中获取网站code(编辑模式下URL包含site_id) + let siteCode = ''; + const codeField = document.querySelector('input[name="code"]'); + if (codeField) { + siteCode = codeField.value; + } + + fetchNewsBtn.addEventListener('click', function() { + if (!siteCode) { + showNewsStatus('请先保存网站', 'error'); + return; + } + + // 显示加载状态 + fetchNewsBtn.disabled = true; + fetchNewsBtn.classList.add('loading'); + fetchNewsBtn.innerHTML = ' 获取中...'; + newsStatusDiv.style.display = 'none'; + + // 调用后台获取新闻API + fetch('/api/admin/fetch-news/' + siteCode, { + method: 'POST', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + } + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + showNewsStatus('✓ ' + data.message, 'success'); + } else { + showNewsStatus('✗ ' + (data.error || '获取失败'), 'error'); + } + }) + .catch(error => { + console.error('Error:', error); + showNewsStatus('✗ 网络请求失败', 'error'); + }) + .finally(() => { + fetchNewsBtn.disabled = false; + fetchNewsBtn.classList.remove('loading'); + fetchNewsBtn.innerHTML = '📰 手动获取新闻'; + }); + }); + + function showNewsStatus(message, type) { + newsStatusDiv.textContent = message; + newsStatusDiv.className = 'news-status ' + type; + newsStatusDiv.style.display = 'block'; + } + } }); {% endblock %}