feat: v2.3.0 - 新闻获取准确性优化

核心改进:
1. 新增专用新闻关键词字段(sites.news_keywords)
2. 严格匹配搜索策略(双引号包裹关键词)
3. 前台手动刷新新闻功能

数据库变更:
- Sites表添加news_keywords字段(VARCHAR(200))
- 提供迁移脚本migrate_news_keywords.py

代码变更:
- models.py: Site模型添加news_keywords字段
- app.py: 后台表单配置、API路由、search_site_news调用优化
- utils/news_searcher.py: 支持news_keywords参数优先匹配
- templates/detail_new.html: 添加刷新按钮和JavaScript

新增功能:
- 后台可为每个网站设置专属新闻关键词
- 详情页"获取最新资讯"按钮(前台可用,无需登录)
- 新API端点:POST /api/refresh-site-news/<site_code>

文档:
- DEPLOY_v2.3.0.md: 完整部署指南
- DEPLOY_v2.3_QUICK.md: 快速部署指南

向后兼容:
- 现有网站自动使用网站名称作为默认关键词
- 未设置关键词时降级使用网站名称搜索

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Jowe
2025-12-31 11:01:51 +08:00
parent bdf31090ed
commit fdde6990fb
7 changed files with 1009 additions and 8 deletions

View File

@@ -622,10 +622,16 @@
<!-- Related News -->
{% if news_list %}
<div class="content-block">
<h2>
<span>📰</span>
相关新闻
</h2>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">
<span>📰</span>
相关新闻
</h2>
<button id="refreshNewsBtn" class="refresh-news-btn" onclick="refreshNews('{{ site.code }}')">
<span class="refresh-icon"></span> 获取最新资讯
</button>
</div>
<div id="newsContainer">
{% for news in news_list %}
<div class="news-item">
<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px;">
@@ -667,6 +673,7 @@
</div>
</div>
{% endfor %}
</div><!-- End newsContainer -->
</div>
{% endif %}
</div>
@@ -698,4 +705,127 @@
</div>
</div>
</div>
<style>
.refresh-news-btn {
padding: 8px 16px;
background: linear-gradient(135deg, var(--primary-blue) 0%, var(--primary-dark) 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s;
display: flex;
align-items: center;
gap: 6px;
}
.refresh-news-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(14, 165, 233, 0.3);
}
.refresh-news-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.refresh-news-btn.loading .refresh-icon {
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.news-status {
padding: 12px;
margin: 16px 0;
border-radius: 8px;
font-size: 14px;
display: none;
}
.news-status.success {
background: rgba(34, 197, 94, 0.1);
border: 1px solid rgba(34, 197, 94, 0.3);
color: #22c55e;
}
.news-status.error {
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.3);
color: #ef4444;
}
</style>
<script>
function refreshNews(siteCode) {
const btn = document.getElementById('refreshNewsBtn');
const newsContainer = document.getElementById('newsContainer');
if (!btn || !newsContainer) return;
// 禁用按钮,显示加载状态
btn.disabled = true;
btn.classList.add('loading');
btn.innerHTML = '<span class="refresh-icon">↻</span> 正在获取...';
// 显示加载提示
const loadingMsg = document.createElement('div');
loadingMsg.className = 'news-status';
loadingMsg.style.display = 'block';
loadingMsg.textContent = '正在获取最新资讯,请稍候...';
newsContainer.insertAdjacentElement('beforebegin', loadingMsg);
// 调用刷新API
fetch(`/api/refresh-site-news/${siteCode}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
// 刷新成功,重新加载页面
loadingMsg.className = 'news-status success';
loadingMsg.textContent = `✓ 成功获取 ${data.saved_count || 0} 条新资讯,页面即将刷新...`;
setTimeout(() => {
window.location.reload();
}, 1500);
} else {
// 刷新失败
loadingMsg.className = 'news-status error';
loadingMsg.textContent = `${data.message || '获取失败,请稍后重试'}`;
btn.disabled = false;
btn.classList.remove('loading');
btn.innerHTML = '<span class="refresh-icon">↻</span> 获取最新资讯';
// 3秒后隐藏错误消息
setTimeout(() => {
loadingMsg.style.display = 'none';
}, 3000);
}
})
.catch(error => {
console.error('Error:', error);
loadingMsg.className = 'news-status error';
loadingMsg.textContent = '✗ 网络请求失败,请检查网络连接';
btn.disabled = false;
btn.classList.remove('loading');
btn.innerHTML = '<span class="refresh-icon">↻</span> 获取最新资讯';
// 3秒后隐藏错误消息
setTimeout(() => {
loadingMsg.style.display = 'none';
}, 3000);
});
}
</script>
{% endblock %}