index.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Pokemon Card Search</title>
  7. <style>
  8. :root {
  9. --primary-color: #4F46E5;
  10. --primary-hover: #4338ca;
  11. --bg-color: #f3f4f6;
  12. --card-bg: #ffffff;
  13. --text-main: #1f2937;
  14. --text-sub: #6b7280;
  15. --border-color: #e5e7eb;
  16. }
  17. * { box-sizing: border-box; margin: 0; padding: 0; }
  18. body {
  19. font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  20. background-color: var(--bg-color);
  21. color: var(--text-main);
  22. line-height: 1.5;
  23. padding: 2rem 1rem;
  24. }
  25. .container {
  26. max-width: 1200px;
  27. margin: 0 auto;
  28. }
  29. header {
  30. text-align: center;
  31. margin-bottom: 2rem;
  32. }
  33. header h1 {
  34. font-size: 2.5rem;
  35. color: var(--text-main);
  36. margin-bottom: 0.5rem;
  37. }
  38. /* --- Tab 切换样式 --- */
  39. .tabs {
  40. display: flex;
  41. justify-content: center;
  42. margin-bottom: 1.5rem;
  43. gap: 1rem;
  44. }
  45. .tab-btn {
  46. background: transparent;
  47. border: none;
  48. padding: 0.75rem 1.5rem;
  49. font-size: 1rem;
  50. font-weight: 600;
  51. color: var(--text-sub);
  52. cursor: pointer;
  53. border-bottom: 3px solid transparent;
  54. transition: all 0.3s;
  55. }
  56. .tab-btn.active {
  57. color: var(--primary-color);
  58. border-bottom-color: var(--primary-color);
  59. }
  60. /* 搜索面板容器 */
  61. .search-panel-container {
  62. background: var(--card-bg);
  63. padding: 2rem;
  64. border-radius: 16px;
  65. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  66. max-width: 600px;
  67. margin: 0 auto 2rem auto;
  68. min-height: 300px; /* 防止切换时高度跳动太大 */
  69. }
  70. .panel-content { display: none; }
  71. .panel-content.active { display: block; animation: fadeIn 0.3s; }
  72. /* --- 图片上传区域样式 (保持不变) --- */
  73. .upload-zone {
  74. border: 2px dashed #d1d5db;
  75. border-radius: 12px;
  76. padding: 2rem;
  77. margin-bottom: 1.5rem;
  78. cursor: pointer;
  79. transition: all 0.3s ease;
  80. text-align: center;
  81. background-color: #fff;
  82. }
  83. .upload-zone:hover, .upload-zone.drag-active {
  84. border-color: var(--primary-color);
  85. background-color: #f9fafb;
  86. }
  87. #preview-img {
  88. max-width: 100%;
  89. max-height: 200px;
  90. border-radius: 8px;
  91. margin-top: 1rem;
  92. display: none;
  93. box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  94. }
  95. /* --- 文本搜索区域样式 (新增) --- */
  96. .text-search-group {
  97. display: flex;
  98. flex-direction: column;
  99. gap: 1rem;
  100. margin-bottom: 1.5rem;
  101. text-align: left;
  102. }
  103. .form-input {
  104. width: 100%;
  105. padding: 0.8rem 1rem;
  106. border: 1px solid var(--border-color);
  107. border-radius: 8px;
  108. font-size: 1rem;
  109. transition: border-color 0.2s;
  110. }
  111. .form-input:focus {
  112. outline: none;
  113. border-color: var(--primary-color);
  114. box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
  115. }
  116. /* --- 公共控件样式 --- */
  117. .controls {
  118. display: flex;
  119. justify-content: center;
  120. align-items: center;
  121. gap: 1rem;
  122. margin-top: 1rem;
  123. }
  124. .search-btn {
  125. background-color: var(--primary-color);
  126. color: white;
  127. border: none;
  128. padding: 0.75rem 2rem;
  129. border-radius: 8px;
  130. font-size: 1rem;
  131. font-weight: 600;
  132. cursor: pointer;
  133. transition: background-color 0.2s;
  134. display: inline-flex;
  135. align-items: center;
  136. }
  137. .search-btn:hover { background-color: var(--primary-hover); }
  138. .search-btn:disabled { background-color: #9ca3af; cursor: not-allowed; }
  139. .limit-input {
  140. padding: 0.75rem;
  141. border: 1px solid var(--border-color);
  142. border-radius: 8px;
  143. width: 80px;
  144. text-align: center;
  145. }
  146. /* --- 结果统计栏 --- */
  147. .stats-bar {
  148. max-width: 1200px;
  149. margin: 0 auto 1rem auto;
  150. padding: 0.75rem 1.5rem;
  151. background-color: #e0e7ff;
  152. color: #3730a3;
  153. border-radius: 8px;
  154. font-weight: 500;
  155. display: none; /* 默认隐藏 */
  156. justify-content: space-between;
  157. align-items: center;
  158. }
  159. /* --- 结果卡片样式 --- */
  160. .results-grid {
  161. display: grid;
  162. grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  163. gap: 1.5rem;
  164. }
  165. .result-card {
  166. background: var(--card-bg);
  167. border-radius: 12px;
  168. overflow: hidden;
  169. box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  170. transition: transform 0.2s, box-shadow 0.2s;
  171. display: flex;
  172. flex-direction: column;
  173. }
  174. .result-card:hover {
  175. transform: translateY(-5px);
  176. box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  177. }
  178. .card-img-wrapper {
  179. width: 100%;
  180. height: 280px;
  181. background-color: #e5e7eb;
  182. display: flex;
  183. align-items: center;
  184. justify-content: center;
  185. overflow: hidden;
  186. position: relative;
  187. }
  188. .card-img-wrapper img {
  189. width: 100%;
  190. height: 100%;
  191. object-fit: contain;
  192. }
  193. .card-info { padding: 1rem; }
  194. .card-title {
  195. font-size: 1.1rem;
  196. font-weight: bold;
  197. color: var(--text-main);
  198. margin-bottom: 0.25rem;
  199. white-space: nowrap;
  200. overflow: hidden;
  201. text-overflow: ellipsis;
  202. }
  203. .card-meta {
  204. font-size: 0.875rem;
  205. color: var(--text-sub);
  206. display: flex;
  207. justify-content: space-between;
  208. margin-bottom: 0.5rem;
  209. }
  210. /* 相似度条 (仅在图片搜索时显示) */
  211. .similarity-wrapper { display: none; }
  212. .show-score .similarity-wrapper { display: block; }
  213. .score-bar-bg {
  214. height: 6px;
  215. background-color: #e5e7eb;
  216. border-radius: 3px;
  217. overflow: hidden;
  218. margin-top: 0.5rem;
  219. }
  220. .score-bar-fill {
  221. height: 100%;
  222. background-color: #10b981;
  223. width: 0%;
  224. transition: width 0.5s ease-out;
  225. }
  226. .score-text {
  227. font-size: 0.75rem;
  228. color: var(--text-sub);
  229. margin-top: 4px;
  230. text-align: right;
  231. }
  232. /* Spinner */
  233. .spinner {
  234. border: 3px solid rgba(255,255,255,0.3);
  235. border-radius: 50%;
  236. border-top: 3px solid white;
  237. width: 20px;
  238. height: 20px;
  239. animation: spin 1s linear infinite;
  240. margin-right: 8px;
  241. display: none;
  242. }
  243. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  244. @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
  245. </style>
  246. </head>
  247. <body>
  248. <div class="container">
  249. <header>
  250. <h1>Pokédex Search</h1>
  251. <p>探索宝可梦卡片数据库</p>
  252. </header>
  253. <!-- Tab 切换 -->
  254. <div class="tabs">
  255. <button class="tab-btn active" onclick="switchTab('img')">📷 以图搜图</button>
  256. <button class="tab-btn" onclick="switchTab('text')">🔤 名称搜索</button>
  257. </div>
  258. <div class="search-panel-container">
  259. <!-- 面板 1: 图片搜索 -->
  260. <div id="panel-img" class="panel-content active">
  261. <div class="upload-zone" id="dropZone">
  262. <div id="upload-placeholder">
  263. <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">
  264. <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
  265. <circle cx="8.5" cy="8.5" r="1.5"></circle>
  266. <polyline points="21 15 16 10 5 21"></polyline>
  267. </svg>
  268. <span style="display:block; margin-top:0.5rem; color:#6b7280;">点击或拖拽上传图片</span>
  269. </div>
  270. <img id="preview-img" src="" alt="Preview">
  271. <input type="file" id="imgInput" accept="image/*" style="display:none">
  272. </div>
  273. <div class="controls">
  274. <label>显示数量:</label>
  275. <input type="number" id="imgTopN" class="limit-input" value="5" min="1" max="20">
  276. <button class="search-btn" onclick="searchImage()" id="btnImgSearch">
  277. <div class="spinner"></div> 搜索相似
  278. </button>
  279. </div>
  280. </div>
  281. <!-- 面板 2: 文本搜索 (新功能) -->
  282. <div id="panel-text" class="panel-content">
  283. <div class="text-search-group">
  284. <label for="cardNameInput" style="font-weight:600; color:var(--text-main)">卡片名称 (模糊匹配):</label>
  285. <input type="text" id="cardNameInput" class="form-input" placeholder="例如: Pikachu, Charizard...">
  286. </div>
  287. <div class="controls">
  288. <label>最大返回:</label>
  289. <input type="number" id="textLimit" class="limit-input" value="20" min="1" max="100">
  290. <button class="search-btn" onclick="searchByMetadata()" id="btnTextSearch">
  291. <div class="spinner"></div> 查询数据
  292. </button>
  293. </div>
  294. </div>
  295. </div>
  296. <!-- 统计信息栏 -->
  297. <div id="statsBar" class="stats-bar">
  298. <span id="statsText"></span>
  299. </div>
  300. <!-- 结果展示区域 -->
  301. <div id="resultsArea">
  302. <div class="results-grid" id="resultsGrid"></div>
  303. </div>
  304. </div>
  305. <script>
  306. // --- Tab 切换逻辑 ---
  307. function switchTab(type) {
  308. document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
  309. document.querySelectorAll('.panel-content').forEach(p => p.classList.remove('active'));
  310. if (type === 'img') {
  311. document.querySelector('.tab-btn:nth-child(1)').classList.add('active');
  312. document.getElementById('panel-img').classList.add('active');
  313. } else {
  314. document.querySelector('.tab-btn:nth-child(2)').classList.add('active');
  315. document.getElementById('panel-text').classList.add('active');
  316. document.getElementById('cardNameInput').focus();
  317. }
  318. // 切换时清空结果,避免混淆
  319. document.getElementById('resultsGrid').innerHTML = '';
  320. document.getElementById('statsBar').style.display = 'none';
  321. }
  322. // --- 图片上传相关逻辑 (Drag & Drop) ---
  323. const dropZone = document.getElementById('dropZone');
  324. const imgInput = document.getElementById('imgInput');
  325. const previewImg = document.getElementById('preview-img');
  326. const placeholder = document.getElementById('upload-placeholder');
  327. ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
  328. dropZone.addEventListener(eventName, (e) => { e.preventDefault(); e.stopPropagation(); }, false);
  329. });
  330. ['dragenter', 'dragover'].forEach(eventName => {
  331. dropZone.addEventListener(eventName, () => dropZone.classList.add('drag-active'), false);
  332. });
  333. ['dragleave', 'drop'].forEach(eventName => {
  334. dropZone.addEventListener(eventName, () => dropZone.classList.remove('drag-active'), false);
  335. });
  336. dropZone.addEventListener('drop', (e) => {
  337. const files = e.dataTransfer.files;
  338. if (files.length) {
  339. imgInput.files = files;
  340. updatePreview(files[0]);
  341. }
  342. });
  343. dropZone.addEventListener('click', () => imgInput.click());
  344. imgInput.addEventListener('click', (e) => e.stopPropagation());
  345. imgInput.onchange = () => { if (imgInput.files[0]) updatePreview(imgInput.files[0]); };
  346. function updatePreview(file) {
  347. placeholder.style.display = 'none';
  348. previewImg.style.display = 'block';
  349. previewImg.src = URL.createObjectURL(file);
  350. }
  351. // --- 功能 1: 图片搜索 (原有) ---
  352. async function searchImage() {
  353. if (!imgInput.files[0]) return alert("请先上传图片!");
  354. const btn = document.getElementById('btnImgSearch');
  355. setLoading(btn, true);
  356. try {
  357. const formData = new FormData();
  358. formData.append('file', imgInput.files[0]);
  359. const topN = document.getElementById('imgTopN').value;
  360. const res = await fetch(`/api/search/image?top_k=${topN}`, {
  361. method: 'POST',
  362. body: formData
  363. });
  364. const data = await res.json();
  365. // 图片搜索通常没有 total 统计,显示返回数量即可
  366. showStats(`🔍 相似度匹配完成,显示前 ${data.results.length} 个结果`);
  367. renderResults(data.results, true); // true 表示显示分数条
  368. } catch (e) {
  369. alert("搜索失败: " + e.message);
  370. } finally {
  371. setLoading(btn, false);
  372. }
  373. }
  374. // --- 功能 2: 元数据搜索 (新增) ---
  375. async function searchByMetadata() {
  376. const name = document.getElementById('cardNameInput').value.trim();
  377. const limit = document.getElementById('textLimit').value;
  378. const btn = document.getElementById('btnTextSearch');
  379. setLoading(btn, true);
  380. try {
  381. // 构建 URL Query Params
  382. const params = new URLSearchParams({ limit: limit });
  383. if (name) params.append('card_name', name);
  384. // 注意:这里使用你提供的 POST 接口
  385. const res = await fetch(`/api/search/state?${params.toString()}`, {
  386. method: 'POST',
  387. headers: { 'Accept': 'application/json' },
  388. body: '' // POST 请求体为空
  389. });
  390. if (!res.ok) throw new Error("API Request Failed");
  391. const data = await res.json();
  392. // 显示统计信息
  393. if (data.total !== undefined) {
  394. showStats(`📊 数据库中符合条件的共有 <b>${data.total}</b> 条,当前展示前 <b>${data.returned_count}</b> 条`);
  395. }
  396. // 渲染结果 (false 表示不显示分数条)
  397. renderResults(data.data || [], false);
  398. } catch (e) {
  399. console.error(e);
  400. alert("查询出错: " + e.message);
  401. } finally {
  402. setLoading(btn, false);
  403. }
  404. }
  405. // --- 通用渲染函数 ---
  406. function renderResults(items, showScore) {
  407. const grid = document.getElementById('resultsGrid');
  408. grid.innerHTML = '';
  409. if (!items || items.length === 0) {
  410. grid.innerHTML = '<p style="grid-column:1/-1; text-align:center; color:#666; padding:2rem;">未找到相关数据</p>';
  411. return;
  412. }
  413. items.forEach((item, index) => {
  414. // 处理相似度分数 (仅图片搜索有)
  415. let scoreHtml = '';
  416. if (showScore && item.score !== undefined) {
  417. const pct = (item.score * 100).toFixed(1);
  418. scoreHtml = `
  419. <div class="similarity-wrapper">
  420. <div class="score-bar-bg">
  421. <div class="score-bar-fill" style="width: ${pct}%"></div>
  422. </div>
  423. <div class="score-text">相似度: ${pct}%</div>
  424. </div>
  425. `;
  426. }
  427. const cardNumDisplay = (item.card_num && item.card_num !== -1) ? `#${item.card_num}` : '';
  428. const html = `
  429. <div class="result-card" style="opacity:0; animation:fadeIn 0.5s forwards ${index * 0.05}s">
  430. <div class="card-img-wrapper">
  431. <img src="${item.img_url}" loading="lazy"
  432. onerror="this.src='https://via.placeholder.com/200x280?text=No+Image'">
  433. </div>
  434. <div class="card-info">
  435. <div class="card-title" title="${item.card_name}">${item.card_name}</div>
  436. <div class="card-meta">
  437. <span style="background:#eee; padding:2px 6px; border-radius:4px; font-size:12px;">${item.lang ? item.lang.toUpperCase() : 'UNK'}</span>
  438. <span>${cardNumDisplay}</span>
  439. </div>
  440. <div class="result-card-class ${showScore ? 'show-score' : ''}">
  441. ${scoreHtml}
  442. </div>
  443. </div>
  444. </div>
  445. `;
  446. grid.insertAdjacentHTML('beforeend', html);
  447. });
  448. }
  449. // --- 辅助 UI 函数 ---
  450. function setLoading(btn, isLoading) {
  451. btn.disabled = isLoading;
  452. btn.querySelector('.spinner').style.display = isLoading ? 'inline-block' : 'none';
  453. }
  454. function showStats(htmlContent) {
  455. const bar = document.getElementById('statsBar');
  456. const text = document.getElementById('statsText');
  457. bar.style.display = 'flex';
  458. text.innerHTML = htmlContent;
  459. }
  460. </script>
  461. </body>
  462. </html>