fix: 修复编辑页面获取新闻按钮逻辑

- 添加网站信息API通过ID获取code
- 修改前端逻辑先获取网站信息再获取新闻

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jowe
2026-03-13 22:24:07 +08:00
parent de55283b70
commit ea181cfcbc
2 changed files with 47 additions and 15 deletions

View File

@@ -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 = '<span class="loading-icon">↻</span> 获取中...';
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;