fix: 修复编辑页面获取新闻按钮逻辑
- 添加网站信息API通过ID获取code - 修改前端逻辑先获取网站信息再获取新闻 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
23
app.py
23
app.py
@@ -453,6 +453,29 @@ def create_app(config_name='default'):
|
||||
db.session.rollback()
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
# ========== 获取网站信息API (v3.0.1新增) ==========
|
||||
@app.route('/admin/site/api/<int:site_id>', methods=['GET'])
|
||||
@login_required
|
||||
def get_site_info(site_id):
|
||||
"""
|
||||
通过网站ID获取网站基本信息(仅管理员可用)
|
||||
"""
|
||||
# 只允许管理员访问
|
||||
if not isinstance(current_user, AdminModel):
|
||||
return jsonify({'error': '无权访问'}), 403
|
||||
|
||||
site = Site.query.get(site_id)
|
||||
if not site:
|
||||
return jsonify({'error': '网站不存在'}), 404
|
||||
|
||||
return jsonify({
|
||||
'id': site.id,
|
||||
'code': site.code,
|
||||
'name': site.name,
|
||||
'url': site.url,
|
||||
'news_keywords': site.news_keywords
|
||||
})
|
||||
|
||||
|
||||
# ========== 社媒营销路由 (v2.5新增) ==========
|
||||
@app.route('/api/generate-social-share', methods=['POST'])
|
||||
|
||||
@@ -704,16 +704,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
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;
|
||||
}
|
||||
// 从URL中获取网站ID
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const siteId = urlParams.get('id');
|
||||
|
||||
fetchNewsBtn.addEventListener('click', function() {
|
||||
if (!siteCode) {
|
||||
showNewsStatus('请先保存网站', 'error');
|
||||
if (!siteId) {
|
||||
showNewsStatus('无法获取网站ID', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -723,13 +720,25 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
fetchNewsBtn.innerHTML = '<span class="loading-icon">↻</span> 获取中...';
|
||||
newsStatusDiv.style.display = 'none';
|
||||
|
||||
// 调用后台获取新闻API
|
||||
fetch('/api/admin/fetch-news/' + siteCode, {
|
||||
// 先获取网站信息(包含code)
|
||||
fetch('/admin/site/api/' + siteId, {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data && data.code) {
|
||||
// 获取到code后再调用获取新闻API
|
||||
return fetch('/api/admin/fetch-news/' + data.code, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new Error('无法获取网站信息');
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
@@ -741,7 +750,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
showNewsStatus('✗ 网络请求失败', 'error');
|
||||
showNewsStatus('✗ 网络请求失败: ' + error.message, 'error');
|
||||
})
|
||||
.finally(() => {
|
||||
fetchNewsBtn.disabled = false;
|
||||
|
||||
Reference in New Issue
Block a user