Files
stock-advisor/static/js/news.js
T

58 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 财经新闻 */
let page = 1, total = 0;
async function load(p) {
if (p) page = p;
const q = new URLSearchParams({ keyword: $('#kw').value.trim(), category: $('#cat').value, page });
try {
const d = await api('/api/news?' + q.toString());
total = d.total;
render(d.items);
$('#pageInfo').textContent = `第 ${page} / ${Math.max(1, Math.ceil(total / 15))} 页`;
$('#count').textContent = `共 ${total} 条`;
$('#prevBtn').disabled = page <= 1;
$('#nextBtn').disabled = page * 15 >= total;
} catch (e) {
$('#listBox').innerHTML = '<div class="empty">加载失败</div>';
}
}
function render(items) {
if (!items.length) { $('#listBox').innerHTML = '<div class="empty">无匹配新闻</div>'; return; }
$('#listBox').innerHTML = items.map(n => `
<div class="news-item" onclick="showDetail(${n.id})">
<div class="news-title">${escapeHtml(n.title)}</div>
<div class="news-meta">
<span class="cat-tag ${n.category}">${n.category}</span>
<span class="tag tag-${n.sentiment > 0 ? '正' : n.sentiment < 0 ? '负' : '平'}">${n.sentiment > 0 ? '利好' : n.sentiment < 0 ? '利空' : '中性'}</span>
<span>${escapeHtml(n.source)}</span><span>${n.publish_date}</span>
</div>
<div class="news-summary">${escapeHtml(n.content)}</div>
</div>`).join('');
}
async function showDetail(id) {
try {
const d = await api('/api/news/' + id);
const n = d.news;
const stocks = (d.stocks || []).map(s => `<a href="/stock/${s.code}">${s.name}(${s.code})</a>`).join(' · ') || '无';
openModal(`
<h3>${escapeHtml(n.title)}</h3>
<div class="news-meta" style="margin-bottom:12px">
<span class="cat-tag ${n.category}">${n.category}</span>
<span class="tag tag-${n.sentiment > 0 ? '正' : n.sentiment < 0 ? '负' : '平'}">${n.sentiment > 0 ? '利好' : n.sentiment < 0 ? '利空' : '中性'}${Number(n.sentiment).toFixed(2)}</span>
<span>${escapeHtml(n.source)}</span><span>${n.publish_date}</span>
</div>
<div style="line-height:1.9;color:var(--text)">${escapeHtml(n.content)}</div>
<div class="mt16" style="padding-top:12px;border-top:1px solid var(--border)">
<b>关联股票:</b>${stocks}
</div>
`);
} catch (e) { toast('加载失败'); }
}
$('#kw').addEventListener('keydown', e => { if (e.key === 'Enter') load(1); });
$('#kw').addEventListener('input', debounce(() => load(1), 400));
$('#cat').onchange = () => load(1);
load(1);