From ea181cfcbcb67003c5f74e67edddcb908344f5ad Mon Sep 17 00:00:00 2001 From: Jowe <123822645+Selei1983@users.noreply.github.com> Date: Fri, 13 Mar 2026 22:24:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=8E=B7=E5=8F=96=E6=96=B0=E9=97=BB=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加网站信息API通过ID获取code - 修改前端逻辑先获取网站信息再获取新闻 Co-Authored-By: Claude Sonnet 4.6 --- app.py | 23 ++++++++++++++++++++ templates/admin/site/edit.html | 39 +++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 2169559..84f811c 100644 --- a/app.py +++ b/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/', 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']) diff --git a/templates/admin/site/edit.html b/templates/admin/site/edit.html index 313c996..beb738c 100644 --- a/templates/admin/site/edit.html +++ b/templates/admin/site/edit.html @@ -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,12 +720,24 @@ document.addEventListener('DOMContentLoaded', function() { fetchNewsBtn.innerHTML = ' 获取中...'; newsStatusDiv.style.display = 'none'; - // 调用后台获取新闻API - fetch('/api/admin/fetch-news/' + siteCode, { - method: 'POST', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/json' + // 先获取网站信息(包含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()) @@ -741,7 +750,7 @@ document.addEventListener('DOMContentLoaded', function() { }) .catch(error => { console.error('Error:', error); - showNewsStatus('✗ 网络请求失败', 'error'); + showNewsStatus('✗ 网络请求失败: ' + error.message, 'error'); }) .finally(() => { fetchNewsBtn.disabled = false;