| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Pokemon Card Search</title>
- <style>
- :root {
- --primary-color: #4F46E5;
- --primary-hover: #4338ca;
- --bg-color: #f3f4f6;
- --card-bg: #ffffff;
- --text-main: #1f2937;
- --text-sub: #6b7280;
- --border-color: #e5e7eb;
- }
- * { box-sizing: border-box; margin: 0; padding: 0; }
- body {
- font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
- background-color: var(--bg-color);
- color: var(--text-main);
- line-height: 1.5;
- padding: 2rem 1rem;
- }
- .container {
- max-width: 1200px;
- margin: 0 auto;
- }
- header {
- text-align: center;
- margin-bottom: 2rem;
- }
- header h1 {
- font-size: 2.5rem;
- color: var(--text-main);
- margin-bottom: 0.5rem;
- }
- /* --- Tab 切换样式 --- */
- .tabs {
- display: flex;
- justify-content: center;
- margin-bottom: 1.5rem;
- gap: 1rem;
- }
- .tab-btn {
- background: transparent;
- border: none;
- padding: 0.75rem 1.5rem;
- font-size: 1rem;
- font-weight: 600;
- color: var(--text-sub);
- cursor: pointer;
- border-bottom: 3px solid transparent;
- transition: all 0.3s;
- }
- .tab-btn.active {
- color: var(--primary-color);
- border-bottom-color: var(--primary-color);
- }
- /* 搜索面板容器 */
- .search-panel-container {
- background: var(--card-bg);
- padding: 2rem;
- border-radius: 16px;
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
- max-width: 600px;
- margin: 0 auto 2rem auto;
- min-height: 300px; /* 防止切换时高度跳动太大 */
- }
- .panel-content { display: none; }
- .panel-content.active { display: block; animation: fadeIn 0.3s; }
- /* --- 图片上传区域样式 (保持不变) --- */
- .upload-zone {
- border: 2px dashed #d1d5db;
- border-radius: 12px;
- padding: 2rem;
- margin-bottom: 1.5rem;
- cursor: pointer;
- transition: all 0.3s ease;
- text-align: center;
- background-color: #fff;
- }
- .upload-zone:hover, .upload-zone.drag-active {
- border-color: var(--primary-color);
- background-color: #f9fafb;
- }
- #preview-img {
- max-width: 100%;
- max-height: 200px;
- border-radius: 8px;
- margin-top: 1rem;
- display: none;
- box-shadow: 0 4px 6px rgba(0,0,0,0.1);
- }
- /* --- 文本搜索区域样式 (新增) --- */
- .text-search-group {
- display: flex;
- flex-direction: column;
- gap: 1rem;
- margin-bottom: 1.5rem;
- text-align: left;
- }
- .form-input {
- width: 100%;
- padding: 0.8rem 1rem;
- border: 1px solid var(--border-color);
- border-radius: 8px;
- font-size: 1rem;
- transition: border-color 0.2s;
- }
- .form-input:focus {
- outline: none;
- border-color: var(--primary-color);
- box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
- }
- /* --- 公共控件样式 --- */
- .controls {
- display: flex;
- justify-content: center;
- align-items: center;
- gap: 1rem;
- margin-top: 1rem;
- }
- .search-btn {
- background-color: var(--primary-color);
- color: white;
- border: none;
- padding: 0.75rem 2rem;
- border-radius: 8px;
- font-size: 1rem;
- font-weight: 600;
- cursor: pointer;
- transition: background-color 0.2s;
- display: inline-flex;
- align-items: center;
- }
- .search-btn:hover { background-color: var(--primary-hover); }
- .search-btn:disabled { background-color: #9ca3af; cursor: not-allowed; }
- .limit-input {
- padding: 0.75rem;
- border: 1px solid var(--border-color);
- border-radius: 8px;
- width: 80px;
- text-align: center;
- }
- /* --- 结果统计栏 --- */
- .stats-bar {
- max-width: 1200px;
- margin: 0 auto 1rem auto;
- padding: 0.75rem 1.5rem;
- background-color: #e0e7ff;
- color: #3730a3;
- border-radius: 8px;
- font-weight: 500;
- display: none; /* 默认隐藏 */
- justify-content: space-between;
- align-items: center;
- }
- /* --- 结果卡片样式 --- */
- .results-grid {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
- gap: 1.5rem;
- }
- .result-card {
- background: var(--card-bg);
- border-radius: 12px;
- overflow: hidden;
- box-shadow: 0 2px 4px rgba(0,0,0,0.05);
- transition: transform 0.2s, box-shadow 0.2s;
- display: flex;
- flex-direction: column;
- }
- .result-card:hover {
- transform: translateY(-5px);
- box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
- }
- .card-img-wrapper {
- width: 100%;
- height: 280px;
- background-color: #e5e7eb;
- display: flex;
- align-items: center;
- justify-content: center;
- overflow: hidden;
- position: relative;
- }
- .card-img-wrapper img {
- width: 100%;
- height: 100%;
- object-fit: contain;
- }
- .card-info { padding: 1rem; }
- .card-title {
- font-size: 1.1rem;
- font-weight: bold;
- color: var(--text-main);
- margin-bottom: 0.25rem;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .card-meta {
- font-size: 0.875rem;
- color: var(--text-sub);
- display: flex;
- justify-content: space-between;
- margin-bottom: 0.5rem;
- }
- /* 相似度条 (仅在图片搜索时显示) */
- .similarity-wrapper { display: none; }
- .show-score .similarity-wrapper { display: block; }
- .score-bar-bg {
- height: 6px;
- background-color: #e5e7eb;
- border-radius: 3px;
- overflow: hidden;
- margin-top: 0.5rem;
- }
- .score-bar-fill {
- height: 100%;
- background-color: #10b981;
- width: 0%;
- transition: width 0.5s ease-out;
- }
- .score-text {
- font-size: 0.75rem;
- color: var(--text-sub);
- margin-top: 4px;
- text-align: right;
- }
- /* Spinner */
- .spinner {
- border: 3px solid rgba(255,255,255,0.3);
- border-radius: 50%;
- border-top: 3px solid white;
- width: 20px;
- height: 20px;
- animation: spin 1s linear infinite;
- margin-right: 8px;
- display: none;
- }
- @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
- @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
- </style>
- </head>
- <body>
- <div class="container">
- <header>
- <h1>Pokédex Search</h1>
- <p>探索宝可梦卡片数据库</p>
- </header>
- <!-- Tab 切换 -->
- <div class="tabs">
- <button class="tab-btn active" onclick="switchTab('img')">📷 以图搜图</button>
- <button class="tab-btn" onclick="switchTab('text')">🔤 名称搜索</button>
- </div>
- <div class="search-panel-container">
- <!-- 面板 1: 图片搜索 -->
- <div id="panel-img" class="panel-content active">
- <div class="upload-zone" id="dropZone">
- <div id="upload-placeholder">
- <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#9ca3af" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
- <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
- <circle cx="8.5" cy="8.5" r="1.5"></circle>
- <polyline points="21 15 16 10 5 21"></polyline>
- </svg>
- <span style="display:block; margin-top:0.5rem; color:#6b7280;">点击或拖拽上传图片</span>
- </div>
- <img id="preview-img" src="" alt="Preview">
- <input type="file" id="imgInput" accept="image/*" style="display:none">
- </div>
- <div class="controls">
- <label>显示数量:</label>
- <input type="number" id="imgTopN" class="limit-input" value="5" min="1" max="20">
- <button class="search-btn" onclick="searchImage()" id="btnImgSearch">
- <div class="spinner"></div> 搜索相似
- </button>
- </div>
- </div>
- <!-- 面板 2: 文本搜索 (新功能) -->
- <div id="panel-text" class="panel-content">
- <div class="text-search-group">
- <label for="cardNameInput" style="font-weight:600; color:var(--text-main)">卡片名称 (模糊匹配):</label>
- <input type="text" id="cardNameInput" class="form-input" placeholder="例如: Pikachu, Charizard...">
- </div>
- <div class="controls">
- <label>最大返回:</label>
- <input type="number" id="textLimit" class="limit-input" value="20" min="1" max="100">
- <button class="search-btn" onclick="searchByMetadata()" id="btnTextSearch">
- <div class="spinner"></div> 查询数据
- </button>
- </div>
- </div>
- </div>
- <!-- 统计信息栏 -->
- <div id="statsBar" class="stats-bar">
- <span id="statsText"></span>
- </div>
- <!-- 结果展示区域 -->
- <div id="resultsArea">
- <div class="results-grid" id="resultsGrid"></div>
- </div>
- </div>
- <script>
- // --- Tab 切换逻辑 ---
- function switchTab(type) {
- document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
- document.querySelectorAll('.panel-content').forEach(p => p.classList.remove('active'));
- if (type === 'img') {
- document.querySelector('.tab-btn:nth-child(1)').classList.add('active');
- document.getElementById('panel-img').classList.add('active');
- } else {
- document.querySelector('.tab-btn:nth-child(2)').classList.add('active');
- document.getElementById('panel-text').classList.add('active');
- document.getElementById('cardNameInput').focus();
- }
- // 切换时清空结果,避免混淆
- document.getElementById('resultsGrid').innerHTML = '';
- document.getElementById('statsBar').style.display = 'none';
- }
- // --- 图片上传相关逻辑 (Drag & Drop) ---
- const dropZone = document.getElementById('dropZone');
- const imgInput = document.getElementById('imgInput');
- const previewImg = document.getElementById('preview-img');
- const placeholder = document.getElementById('upload-placeholder');
- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
- dropZone.addEventListener(eventName, (e) => { e.preventDefault(); e.stopPropagation(); }, false);
- });
- ['dragenter', 'dragover'].forEach(eventName => {
- dropZone.addEventListener(eventName, () => dropZone.classList.add('drag-active'), false);
- });
- ['dragleave', 'drop'].forEach(eventName => {
- dropZone.addEventListener(eventName, () => dropZone.classList.remove('drag-active'), false);
- });
- dropZone.addEventListener('drop', (e) => {
- const files = e.dataTransfer.files;
- if (files.length) {
- imgInput.files = files;
- updatePreview(files[0]);
- }
- });
- dropZone.addEventListener('click', () => imgInput.click());
- imgInput.addEventListener('click', (e) => e.stopPropagation());
- imgInput.onchange = () => { if (imgInput.files[0]) updatePreview(imgInput.files[0]); };
- function updatePreview(file) {
- placeholder.style.display = 'none';
- previewImg.style.display = 'block';
- previewImg.src = URL.createObjectURL(file);
- }
- // --- 功能 1: 图片搜索 (原有) ---
- async function searchImage() {
- if (!imgInput.files[0]) return alert("请先上传图片!");
- const btn = document.getElementById('btnImgSearch');
- setLoading(btn, true);
- try {
- const formData = new FormData();
- formData.append('file', imgInput.files[0]);
- const topN = document.getElementById('imgTopN').value;
- const res = await fetch(`/api/search/image?top_k=${topN}`, {
- method: 'POST',
- body: formData
- });
- const data = await res.json();
- // 图片搜索通常没有 total 统计,显示返回数量即可
- showStats(`🔍 相似度匹配完成,显示前 ${data.results.length} 个结果`);
- renderResults(data.results, true); // true 表示显示分数条
- } catch (e) {
- alert("搜索失败: " + e.message);
- } finally {
- setLoading(btn, false);
- }
- }
- // --- 功能 2: 元数据搜索 (新增) ---
- async function searchByMetadata() {
- const name = document.getElementById('cardNameInput').value.trim();
- const limit = document.getElementById('textLimit').value;
- const btn = document.getElementById('btnTextSearch');
- setLoading(btn, true);
- try {
- // 构建 URL Query Params
- const params = new URLSearchParams({ limit: limit });
- if (name) params.append('card_name', name);
- // 注意:这里使用你提供的 POST 接口
- const res = await fetch(`/api/search/state?${params.toString()}`, {
- method: 'POST',
- headers: { 'Accept': 'application/json' },
- body: '' // POST 请求体为空
- });
- if (!res.ok) throw new Error("API Request Failed");
- const data = await res.json();
- // 显示统计信息
- if (data.total !== undefined) {
- showStats(`📊 数据库中符合条件的共有 <b>${data.total}</b> 条,当前展示前 <b>${data.returned_count}</b> 条`);
- }
- // 渲染结果 (false 表示不显示分数条)
- renderResults(data.data || [], false);
- } catch (e) {
- console.error(e);
- alert("查询出错: " + e.message);
- } finally {
- setLoading(btn, false);
- }
- }
- // --- 通用渲染函数 ---
- function renderResults(items, showScore) {
- const grid = document.getElementById('resultsGrid');
- grid.innerHTML = '';
- if (!items || items.length === 0) {
- grid.innerHTML = '<p style="grid-column:1/-1; text-align:center; color:#666; padding:2rem;">未找到相关数据</p>';
- return;
- }
- items.forEach((item, index) => {
- // 处理相似度分数 (仅图片搜索有)
- let scoreHtml = '';
- if (showScore && item.score !== undefined) {
- const pct = (item.score * 100).toFixed(1);
- scoreHtml = `
- <div class="similarity-wrapper">
- <div class="score-bar-bg">
- <div class="score-bar-fill" style="width: ${pct}%"></div>
- </div>
- <div class="score-text">相似度: ${pct}%</div>
- </div>
- `;
- }
- const cardNumDisplay = (item.card_num && item.card_num !== -1) ? `#${item.card_num}` : '';
- const html = `
- <div class="result-card" style="opacity:0; animation:fadeIn 0.5s forwards ${index * 0.05}s">
- <div class="card-img-wrapper">
- <img src="${item.img_url}" loading="lazy"
- onerror="this.src='https://via.placeholder.com/200x280?text=No+Image'">
- </div>
- <div class="card-info">
- <div class="card-title" title="${item.card_name}">${item.card_name}</div>
- <div class="card-meta">
- <span style="background:#eee; padding:2px 6px; border-radius:4px; font-size:12px;">${item.lang ? item.lang.toUpperCase() : 'UNK'}</span>
- <span>${cardNumDisplay}</span>
- </div>
- <div class="result-card-class ${showScore ? 'show-score' : ''}">
- ${scoreHtml}
- </div>
- </div>
- </div>
- `;
- grid.insertAdjacentHTML('beforeend', html);
- });
- }
- // --- 辅助 UI 函数 ---
- function setLoading(btn, isLoading) {
- btn.disabled = isLoading;
- btn.querySelector('.spinner').style.display = isLoading ? 'inline-block' : 'none';
- }
- function showStats(htmlContent) {
- const bar = document.getElementById('statsBar');
- const text = document.getElementById('statsText');
- bar.style.display = 'flex';
- text.innerHTML = htmlContent;
- }
- </script>
- </body>
- </html>
|