优化结构
Vercel Deploy / deploy (push) Successful in 2m8s

This commit is contained in:
2026-05-01 08:29:46 +08:00
Unverified
parent bad707fce1
commit b3a2199f28
25 changed files with 906 additions and 902 deletions
+246
View File
@@ -0,0 +1,246 @@
const SUPABASE_URL = 'https://chixssrphfgxvqqigkzo.supabase.co';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNoaXhzc3JwaGZneHZxcWlna3pvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ2OTE0OTEsImV4cCI6MjA5MDI2NzQ5MX0.Az_Ew2J2zdOMcSV0UNAjBS-LPqGpqhsaN4IyZ5R7iqU';
const sbClient = supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const html5QrCode = new Html5Qrcode("reader");
const scannerOverlay = document.getElementById('scanner-overlay');
document.getElementById('startScanBtn').addEventListener('click', async () => {
scannerOverlay.style.display = 'flex';
const config = {
fps: 15,
qrbox: { width: 320, height: 140 },
aspectRatio: 1.0
};
if (typeof Html5QrcodeSupportedFormats !== 'undefined') {
config.formatsToSupport = [
Html5QrcodeSupportedFormats.CODE_128,
Html5QrcodeSupportedFormats.CODE_39,
Html5QrcodeSupportedFormats.CODE_93,
Html5QrcodeSupportedFormats.EAN_13,
Html5QrcodeSupportedFormats.EAN_8,
Html5QrcodeSupportedFormats.UPC_A,
Html5QrcodeSupportedFormats.UPC_E,
Html5QrcodeSupportedFormats.ITF,
Html5QrcodeSupportedFormats.CODABAR
];
}
try {
await html5QrCode.start(
{ facingMode: "environment" },
config,
(decodedText) => {
document.getElementById('searchTerm').value = decodedText;
closeScanner();
handleSearch();
},
() => {}
);
} catch (err) {
alert("无法启动摄像头,请检查 HTTPS 权限或设备连接。");
scannerOverlay.style.display = 'none';
}
});
async function closeScanner() {
if (html5QrCode.isScanning) {
await html5QrCode.stop();
}
scannerOverlay.style.display = 'none';
}
document.getElementById('stopScanBtn').addEventListener('click', closeScanner);
async function handleSearch() {
const term = document.getElementById('searchTerm').value.trim();
const start = document.getElementById('dateStart').value;
const end = document.getElementById('dateEnd').value;
const display = document.getElementById('resultsArea');
if (!term) {
alert("请输入证书编号、持有人姓名或奖项名。");
return;
}
display.innerHTML = '<div class="loading-state">正在连接证书数据库并执行检索...</div>';
try {
let query = sbClient.from('certificates').select('*');
query = query.or(`cert_number.ilike.%${term}%,holder_name.ilike.%${term}%,honor_title.ilike.%${term}%`);
if (start) query = query.gte('issue_date', start);
if (end) query = query.lte('issue_date', end);
const { data, error } = await query.order('issue_date', { ascending: false });
if (error) throw error;
const awardRecipientCounts = await loadAwardRecipientCounts(data);
renderResults(data, awardRecipientCounts);
} catch (err) {
display.innerHTML = `<div class="error-state">检索失败:${err.message}</div>`;
}
}
async function loadAwardRecipientCounts(data) {
const awardTitles = [...new Set((data || []).map(item => item.honor_title).filter(Boolean))];
if (awardTitles.length === 0) return {};
const countPairs = await Promise.all(awardTitles.map(async title => {
const { count, error } = await sbClient
.from('certificates')
.select('id', { count: 'exact', head: true })
.eq('honor_title', title);
if (error) throw error;
return [title, count || 0];
}));
return Object.fromEntries(countPairs);
}
function escapeHtml(value) {
return String(value ?? '').replace(/[&<>"']/g, char => ({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}[char]));
}
function renderAwardStats(data, awardRecipientCounts) {
const holderCounts = (data || []).reduce((counts, item) => {
const holderName = item.holder_name || '未填写';
counts[holderName] = (counts[holderName] || 0) + 1;
return counts;
}, {});
const holderStats = Object.entries(holderCounts)
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0], 'zh-CN'));
const maxHolderCount = Math.max(...holderStats.map(([, count]) => count), 1);
const awardStats = [...new Set((data || []).map(item => item.honor_title).filter(Boolean))]
.map(title => ({
title,
count: awardRecipientCounts[title] || 0
}))
.sort((a, b) => b.count - a.count || a.title.localeCompare(b.title, 'zh-CN'));
const holderBars = holderStats.map(([name, count]) => `
<div class="chart-row">
<div class="chart-row-label" title="${escapeHtml(name)}">${escapeHtml(name)}</div>
<div class="chart-bar-track">
<div class="chart-bar-fill" style="width: ${Math.max((count / maxHolderCount) * 100, 6)}%;"></div>
</div>
<div class="chart-row-value">${count} 项</div>
</div>
`).join('');
const awardChips = awardStats.map(item => `
<div class="award-count-chip">
<span class="award-count-title" title="${escapeHtml(item.title)}">${escapeHtml(item.title)}</span>
<strong>${item.count}</strong>
<span>人获得</span>
</div>
`).join('');
return `
<div class="results-summary">
<div class="summary-stat-tile">
<span>查询结果</span>
<strong>${data.length}</strong>
</div>
<div class="summary-stat-tile">
<span>涉及人员</span>
<strong>${holderStats.length}</strong>
</div>
<div class="summary-stat-tile">
<span>奖项种类</span>
<strong>${awardStats.length}</strong>
</div>
</div>
<div class="stats-grid">
<section class="stats-panel">
<div class="stats-panel-title">个人奖项数量</div>
<div class="bar-chart">${holderBars}</div>
</section>
<section class="stats-panel">
<div class="stats-panel-title">查询奖项获得人数</div>
<div class="award-count-grid">${awardChips || '<div class="muted-note">暂无奖项名称</div>'}</div>
</section>
</div>
`;
}
function renderResults(data, awardRecipientCounts = {}) {
const display = document.getElementById('resultsArea');
if (!data || data.length === 0) {
display.innerHTML = '<div class="empty-state">未找到匹配的证书记录。</div>';
return;
}
let html = `
${renderAwardStats(data, awardRecipientCounts)}
<div class="table-wrapper">
<table class="result-table">
<thead>
<tr>
<th>证书编号</th>
<th>持有人</th>
<th>荣誉标题</th>
<th>颁发日期</th>
<th>操作</th>
</tr>
</thead>
<tbody>
`;
data.forEach(item => {
html += `
<tr>
<td class="table-highlight">${escapeHtml(item.cert_number || '-')}</td>
<td>${escapeHtml(item.holder_name || '-')}</td>
<td>${escapeHtml(item.honor_title || '-')}</td>
<td>${escapeHtml(item.issue_date || '-')}</td>
<td><button class="tech-link-button secondary" type="button" onclick="goToDetail('${item.id}')">查看详情</button></td>
</tr>
`;
});
html += '</tbody></table></div>';
display.innerHTML = html;
}
function goToDetail(id) {
window.location.href = `certificate.html?id=${id}`;
}
function initializeSearchTermFromUrl() {
const params = new URLSearchParams(window.location.search);
const presetTerm =
params.get('searchTerm') ||
params.get('term') ||
params.get('keyword') ||
params.get('cert_number') ||
params.get('certNumber') ||
params.get('number') ||
params.get('awardName') ||
params.get('honor_title') ||
params.get('honorTitle') ||
params.get('award') ||
params.get('q') ||
'';
if (presetTerm) {
document.getElementById('searchTerm').value = presetTerm;
}
}
document.getElementById('searchBtn').addEventListener('click', handleSearch);
document.getElementById('searchTerm').addEventListener('keypress', e => {
if (e.key === 'Enter') handleSearch();
});
initializeSearchTermFromUrl();