feat: 重构为 Go Web 脚手架——在线图片识别 Web 服务 + vision-cli 独立工具
- Gin + config.yaml + SQLite 后端骨架,JWT 登录 + 管理员初始化
- web/ 原生单页:拖拽多图上传 → Agnes 免费识别 → 结果复制
- HTTP API:/api/v1/vision/analyze 免鉴权 + 令牌桶限流,支持程序直调
- cmd/vision-cli 独立 CLI 工具(复用 service 层,不依赖 Web 服务)
- build.bat / build.sh 构建脚本,移除 mcp-server
- 统一响应格式 {code, message, data},CORS/JWT/限流中间件
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
211
web/css/style.css
Normal file
211
web/css/style.css
Normal file
@@ -0,0 +1,211 @@
|
||||
/* ===== 基础 ===== */
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
|
||||
:root {
|
||||
--primary: #4f6ef7;
|
||||
--primary-dark: #3a55d8;
|
||||
--bg: #f4f6fb;
|
||||
--card: #ffffff;
|
||||
--text: #1f2430;
|
||||
--text-light: #6b7280;
|
||||
--border: #e5e9f2;
|
||||
--danger: #e5484d;
|
||||
--radius: 12px;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
/* ===== 顶栏 ===== */
|
||||
.topbar {
|
||||
background: var(--card);
|
||||
border-bottom: 1px solid var(--border);
|
||||
padding: 14px 28px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.brand { display: flex; align-items: center; gap: 12px; }
|
||||
.brand .logo { font-size: 30px; }
|
||||
.brand h1 { font-size: 20px; font-weight: 700; }
|
||||
.brand p { font-size: 12px; color: var(--text-light); margin-top: 2px; }
|
||||
|
||||
.account { display: flex; align-items: center; gap: 10px; font-size: 14px; }
|
||||
.account .user-chip {
|
||||
background: #eef1fe;
|
||||
color: var(--primary-dark);
|
||||
padding: 6px 12px;
|
||||
border-radius: 999px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* ===== 布局 ===== */
|
||||
.container {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
max-width: 860px;
|
||||
margin: 0 auto;
|
||||
padding: 28px 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: var(--card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 22px;
|
||||
box-shadow: 0 1px 3px rgba(16, 24, 40, 0.05);
|
||||
}
|
||||
|
||||
/* ===== 上传区 ===== */
|
||||
.dropzone {
|
||||
border: 2px dashed #c7d2fe;
|
||||
border-radius: var(--radius);
|
||||
padding: 40px 20px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
transition: all .15s ease;
|
||||
background: #fafbff;
|
||||
}
|
||||
.dropzone:hover, .dropzone.dragover { border-color: var(--primary); background: #eef1fe; }
|
||||
.dropzone-icon { font-size: 40px; }
|
||||
.dropzone-title { margin-top: 10px; font-weight: 600; font-size: 15px; }
|
||||
.dropzone-hint { margin-top: 6px; font-size: 12px; color: var(--text-light); }
|
||||
|
||||
.preview-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
.preview-item {
|
||||
position: relative;
|
||||
width: 96px;
|
||||
height: 96px;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
.preview-item img { width: 100%; height: 100%; object-fit: cover; }
|
||||
.preview-item .remove {
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 4px;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border-radius: 50%;
|
||||
background: rgba(0, 0, 0, .55);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
line-height: 20px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
}
|
||||
.preview-item .remove:hover { background: var(--danger); }
|
||||
|
||||
/* ===== 提问与按钮 ===== */
|
||||
.prompt-row { display: flex; gap: 12px; margin-top: 18px; }
|
||||
.prompt-input {
|
||||
flex: 1;
|
||||
padding: 12px 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
transition: border-color .15s;
|
||||
}
|
||||
.prompt-input:focus { border-color: var(--primary); }
|
||||
|
||||
.btn {
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
padding: 12px 22px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all .15s;
|
||||
}
|
||||
.btn:disabled { opacity: .5; cursor: not-allowed; }
|
||||
.btn-primary { background: var(--primary); color: #fff; }
|
||||
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
|
||||
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text-light); }
|
||||
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }
|
||||
.btn-block { width: 100%; margin-top: 6px; }
|
||||
|
||||
/* ===== 结果区 ===== */
|
||||
.result-head { display: flex; align-items: center; justify-content: space-between; }
|
||||
.result-head h2 { font-size: 16px; }
|
||||
.result-body {
|
||||
margin-top: 14px;
|
||||
background: #f8f9fc;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
padding: 16px;
|
||||
font-size: 14px;
|
||||
line-height: 1.7;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
max-height: 480px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.result-meta { margin-top: 10px; font-size: 12px; color: var(--text-light); }
|
||||
|
||||
/* ===== 加载态 ===== */
|
||||
.loading { display: inline-flex; align-items: center; gap: 10px; }
|
||||
.spinner {
|
||||
width: 18px; height: 18px;
|
||||
border: 2px solid var(--border);
|
||||
border-top-color: var(--primary);
|
||||
border-radius: 50%;
|
||||
animation: spin .8s linear infinite;
|
||||
}
|
||||
@keyframes spin { to { transform: rotate(360deg); } }
|
||||
|
||||
/* ===== 模态框 ===== */
|
||||
.modal-mask {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(15, 20, 35, .45);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 100;
|
||||
}
|
||||
.modal {
|
||||
background: var(--card);
|
||||
border-radius: 14px;
|
||||
padding: 28px;
|
||||
width: 380px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, .2);
|
||||
}
|
||||
.modal h2 { font-size: 18px; }
|
||||
.modal-desc { font-size: 13px; color: var(--text-light); margin: 6px 0 16px; }
|
||||
.modal-input {
|
||||
width: 100%;
|
||||
padding: 11px 13px;
|
||||
margin-bottom: 10px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
}
|
||||
.modal-input:focus { border-color: var(--primary); }
|
||||
.modal-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 4px; }
|
||||
|
||||
/* ===== 页脚 ===== */
|
||||
.footer {
|
||||
text-align: center;
|
||||
padding: 18px;
|
||||
font-size: 12px;
|
||||
color: var(--text-light);
|
||||
}
|
||||
80
web/index.html
Normal file
80
web/index.html
Normal file
@@ -0,0 +1,80 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Vision Tool — 免费图片识别</title>
|
||||
<link rel="stylesheet" href="/css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header class="topbar">
|
||||
<div class="brand">
|
||||
<span class="logo">👁️</span>
|
||||
<div>
|
||||
<h1>Vision Tool</h1>
|
||||
<p>免费图片识别 · Agnes-2.0-Flash</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="account" id="accountArea">
|
||||
<!-- 登录状态由 JS 渲染 -->
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
<section class="card upload-card">
|
||||
<div class="dropzone" id="dropzone">
|
||||
<input type="file" id="fileInput" accept="image/png,image/jpeg,image/gif,image/webp,image/bmp,image/tiff" multiple hidden>
|
||||
<div class="dropzone-icon">🖼️</div>
|
||||
<p class="dropzone-title">点击选择或拖拽图片到这里</p>
|
||||
<p class="dropzone-hint">支持 PNG / JPG / GIF / WebP / BMP,可多张同时识别(对比异同)</p>
|
||||
</div>
|
||||
<div class="preview-list" id="previewList"></div>
|
||||
|
||||
<div class="prompt-row">
|
||||
<input type="text" id="promptInput" class="prompt-input"
|
||||
placeholder="可选:自定义分析提问,例如「提取图中所有文字」「分析这个 UI 的布局结构」">
|
||||
<button class="btn btn-primary" id="analyzeBtn" disabled>开始识别</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card result-card" id="resultCard" hidden>
|
||||
<div class="result-head">
|
||||
<h2>识别结果</h2>
|
||||
<div class="result-actions">
|
||||
<button class="btn btn-ghost" id="copyBtn">复制</button>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="result-body" id="resultBody"></pre>
|
||||
<p class="result-meta" id="resultMeta"></p>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<!-- 初始化管理员模态框 -->
|
||||
<div class="modal-mask" id="initModal" hidden>
|
||||
<div class="modal">
|
||||
<h2>初始化管理员</h2>
|
||||
<p class="modal-desc">首次使用,请创建管理员账号</p>
|
||||
<input type="text" id="initUsername" placeholder="用户名" class="modal-input">
|
||||
<input type="password" id="initPassword" placeholder="密码(至少 6 位)" class="modal-input">
|
||||
<input type="password" id="initPassword2" placeholder="确认密码" class="modal-input">
|
||||
<div class="modal-error" id="initError"></div>
|
||||
<button class="btn btn-primary btn-block" id="initBtn">创建管理员</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 登录模态框 -->
|
||||
<div class="modal-mask" id="loginModal" hidden>
|
||||
<div class="modal">
|
||||
<h2>登录</h2>
|
||||
<input type="text" id="loginUsername" placeholder="用户名" class="modal-input">
|
||||
<input type="password" id="loginPassword" placeholder="密码" class="modal-input">
|
||||
<div class="modal-error" id="loginError"></div>
|
||||
<button class="btn btn-primary btn-block" id="loginBtn">登录</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer class="footer">推理模型专心推理,图片理解交给免费小模型 · 识别接口支持 API 直接调用</footer>
|
||||
|
||||
<script src="/js/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
236
web/js/app.js
Normal file
236
web/js/app.js
Normal file
@@ -0,0 +1,236 @@
|
||||
// ============================================================================
|
||||
// Vision Tool — 在线免费图片识别(原生单页)
|
||||
// ============================================================================
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
|
||||
// ---------- API 封装 ----------
|
||||
async function api(path, options = {}) {
|
||||
const headers = options.headers || {};
|
||||
if (!(options.body instanceof FormData)) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
const token = localStorage.getItem("vt_token");
|
||||
if (token) headers["Authorization"] = `Bearer ${token}`;
|
||||
|
||||
const resp = await fetch(path, { ...options, headers });
|
||||
let data;
|
||||
try {
|
||||
data = await resp.json();
|
||||
} catch {
|
||||
throw new Error(`HTTP ${resp.status}: 响应解析失败`);
|
||||
}
|
||||
if (data.code !== 0) throw new Error(data.message || `HTTP ${resp.status}`);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
// ---------- 状态 ----------
|
||||
let selectedFiles = []; // 待识别的图片文件
|
||||
let analyzing = false;
|
||||
|
||||
// ---------- 顶栏账号区 ----------
|
||||
async function refreshAccount() {
|
||||
const area = $("accountArea");
|
||||
area.innerHTML = "";
|
||||
|
||||
const { initialized } = await api("/api/v1/admin/check");
|
||||
|
||||
if (!initialized) {
|
||||
const btn = document.createElement("button");
|
||||
btn.className = "btn btn-primary";
|
||||
btn.textContent = "初始化管理员";
|
||||
btn.onclick = () => showModal("initModal");
|
||||
area.appendChild(btn);
|
||||
return;
|
||||
}
|
||||
|
||||
const token = localStorage.getItem("vt_token");
|
||||
if (!token) {
|
||||
const btn = document.createElement("button");
|
||||
btn.className = "btn btn-ghost";
|
||||
btn.textContent = "登录";
|
||||
btn.onclick = () => showModal("loginModal");
|
||||
area.appendChild(btn);
|
||||
return;
|
||||
}
|
||||
|
||||
// 校验 token 有效性
|
||||
try {
|
||||
const user = await api("/api/v1/user/profile");
|
||||
const chip = document.createElement("span");
|
||||
chip.className = "user-chip";
|
||||
chip.textContent = `👤 ${user.username}`;
|
||||
area.appendChild(chip);
|
||||
const logout = document.createElement("button");
|
||||
logout.className = "btn btn-ghost";
|
||||
logout.textContent = "退出";
|
||||
logout.onclick = () => {
|
||||
localStorage.removeItem("vt_token");
|
||||
refreshAccount();
|
||||
};
|
||||
area.appendChild(logout);
|
||||
} catch {
|
||||
localStorage.removeItem("vt_token");
|
||||
refreshAccount();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 模态框 ----------
|
||||
function showModal(id) {
|
||||
$(id).hidden = false;
|
||||
$("initError").textContent = "";
|
||||
$("loginError").textContent = "";
|
||||
}
|
||||
function hideModal(id) { $(id).hidden = true; }
|
||||
$("initModal").addEventListener("click", (e) => { if (e.target === e.currentTarget) hideModal("initModal"); });
|
||||
$("loginModal").addEventListener("click", (e) => { if (e.target === e.currentTarget) hideModal("loginModal"); });
|
||||
|
||||
// 初始化管理员
|
||||
$("initBtn").addEventListener("click", async () => {
|
||||
const username = $("initUsername").value.trim();
|
||||
const password = $("initPassword").value;
|
||||
const password2 = $("initPassword2").value;
|
||||
if (!username || password.length < 6) {
|
||||
$("initError").textContent = "用户名不能为空,密码至少 6 位";
|
||||
return;
|
||||
}
|
||||
if (password !== password2) {
|
||||
$("initError").textContent = "两次输入的密码不一致";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await api("/api/v1/admin/init", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
hideModal("initModal");
|
||||
await login(username, password);
|
||||
refreshAccount();
|
||||
} catch (err) {
|
||||
$("initError").textContent = err.message;
|
||||
}
|
||||
});
|
||||
|
||||
// 登录
|
||||
$("loginBtn").addEventListener("click", async () => {
|
||||
const username = $("loginUsername").value.trim();
|
||||
const password = $("loginPassword").value;
|
||||
if (!username || !password) {
|
||||
$("loginError").textContent = "请输入用户名和密码";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await login(username, password);
|
||||
hideModal("loginModal");
|
||||
refreshAccount();
|
||||
} catch (err) {
|
||||
$("loginError").textContent = err.message;
|
||||
}
|
||||
});
|
||||
|
||||
async function login(username, password) {
|
||||
const data = await api("/api/v1/auth/login", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
localStorage.setItem("vt_token", data.token);
|
||||
}
|
||||
|
||||
// ---------- 图片选择与预览 ----------
|
||||
const dropzone = $("dropzone");
|
||||
const fileInput = $("fileInput");
|
||||
|
||||
dropzone.addEventListener("click", () => fileInput.click());
|
||||
dropzone.addEventListener("dragover", (e) => { e.preventDefault(); dropzone.classList.add("dragover"); });
|
||||
dropzone.addEventListener("dragleave", () => dropzone.classList.remove("dragover"));
|
||||
dropzone.addEventListener("drop", (e) => {
|
||||
e.preventDefault();
|
||||
dropzone.classList.remove("dragover");
|
||||
addFiles(e.dataTransfer.files);
|
||||
});
|
||||
fileInput.addEventListener("change", () => { addFiles(fileInput.files); fileInput.value = ""; });
|
||||
|
||||
function addFiles(files) {
|
||||
for (const f of files) {
|
||||
if (!f.type.startsWith("image/")) continue;
|
||||
if (f.size > 10 * 1024 * 1024) { alert(`图片过大(>10MB): ${f.name}`); continue; }
|
||||
selectedFiles.push(f);
|
||||
}
|
||||
renderPreviews();
|
||||
}
|
||||
|
||||
function renderPreviews() {
|
||||
const list = $("previewList");
|
||||
list.innerHTML = "";
|
||||
selectedFiles.forEach((f, i) => {
|
||||
const item = document.createElement("div");
|
||||
item.className = "preview-item";
|
||||
const img = document.createElement("img");
|
||||
img.src = URL.createObjectURL(f);
|
||||
img.alt = f.name;
|
||||
const rm = document.createElement("button");
|
||||
rm.className = "remove";
|
||||
rm.textContent = "×";
|
||||
rm.onclick = () => {
|
||||
selectedFiles.splice(i, 1);
|
||||
renderPreviews();
|
||||
};
|
||||
item.append(img, rm);
|
||||
list.appendChild(item);
|
||||
});
|
||||
$("analyzeBtn").disabled = selectedFiles.length === 0 || analyzing;
|
||||
}
|
||||
|
||||
// ---------- 识别 ----------
|
||||
$("analyzeBtn").addEventListener("click", analyze);
|
||||
|
||||
async function analyze() {
|
||||
if (analyzing || selectedFiles.length === 0) return;
|
||||
analyzing = true;
|
||||
renderPreviews();
|
||||
|
||||
const btn = $("analyzeBtn");
|
||||
btn.innerHTML = '<span class="loading"><span class="spinner"></span>识别中…</span>';
|
||||
const resultCard = $("resultCard");
|
||||
resultCard.hidden = false;
|
||||
$("resultBody").textContent = "正在调用免费视觉模型,请稍候(最长约 2 分钟)…";
|
||||
$("resultMeta").textContent = "";
|
||||
|
||||
const fd = new FormData();
|
||||
selectedFiles.forEach((f) => fd.append("images", f, f.name));
|
||||
const prompt = $("promptInput").value.trim();
|
||||
if (prompt) fd.append("prompt", prompt);
|
||||
|
||||
try {
|
||||
const data = await api("/api/v1/vision/analyze", { method: "POST", body: fd });
|
||||
$("resultBody").textContent = data.result;
|
||||
if (data.usage) {
|
||||
$("resultMeta").textContent =
|
||||
`token 用量: prompt ${data.usage.prompt_tokens} / completion ${data.usage.completion_tokens} / total ${data.usage.total_tokens}`;
|
||||
}
|
||||
} catch (err) {
|
||||
$("resultBody").textContent = "❌ 识别失败: " + err.message;
|
||||
} finally {
|
||||
analyzing = false;
|
||||
btn.textContent = "开始识别";
|
||||
renderPreviews();
|
||||
}
|
||||
}
|
||||
|
||||
// 复制结果
|
||||
$("copyBtn").addEventListener("click", async () => {
|
||||
const text = $("resultBody").textContent;
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
$("copyBtn").textContent = "已复制 ✓";
|
||||
setTimeout(() => ($("copyBtn").textContent = "复制"), 1500);
|
||||
} catch {
|
||||
alert("复制失败,请手动选择文本");
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- 启动 ----------
|
||||
refreshAccount().catch((err) => {
|
||||
// check 失败(服务未启动等),提示
|
||||
document.querySelector(".brand p").textContent = "⚠️ 服务连接失败: " + err.message;
|
||||
});
|
||||
Reference in New Issue
Block a user