Face_reg_app/FaceFeatureExtractorAPI/admin.html

448 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>人脸识别管理后台</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.header h1 {
color: #333;
margin-bottom: 10px;
}
.header p {
color: #666;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-bottom: 20px;
}
.stat-card {
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.stat-card h3 {
color: #666;
font-size: 14px;
margin-bottom: 10px;
}
.stat-card .number {
color: #667eea;
font-size: 32px;
font-weight: bold;
}
.main-content {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.section {
margin-bottom: 40px;
}
.section h2 {
color: #333;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #667eea;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 500;
}
.form-group input,
.form-group select {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 14px;
}
.btn {
background: #667eea;
color: white;
padding: 12px 30px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: background 0.3s;
}
.btn:hover {
background: #5568d3;
}
.btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.table th,
.table td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #eee;
}
.table th {
background: #f8f9fa;
color: #333;
font-weight: 600;
}
.table tr:hover {
background: #f8f9fa;
}
.badge {
padding: 4px 12px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.badge-success {
background: #d4edda;
color: #155724;
}
.badge-danger {
background: #f8d7da;
color: #721c24;
}
.message {
padding: 15px;
margin: 15px 0;
border-radius: 5px;
display: none;
}
.message.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.message.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.loading {
text-align: center;
padding: 20px;
color: #666;
}
#preview {
max-width: 300px;
max-height: 300px;
margin-top: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🎛️ 人脸识别管理后台</h1>
<p>统一管理用户注册和识别记录</p>
</div>
<div class="stats" id="stats">
<div class="stat-card">
<h3>注册用户数</h3>
<div class="number" id="totalUsers">-</div>
</div>
<div class="stat-card">
<h3>识别总次数</h3>
<div class="number" id="totalRecognitions">-</div>
</div>
<div class="stat-card">
<h3>成功识别次数</h3>
<div class="number" id="successfulRecognitions">-</div>
</div>
<div class="stat-card">
<h3>识别成功率</h3>
<div class="number" id="recognitionRate">-</div>
</div>
</div>
<div class="main-content">
<!-- 用户注册 -->
<div class="section">
<h2>👤 注册新用户</h2>
<div id="registerMessage" class="message"></div>
<form id="registerForm">
<div class="form-group">
<label for="userName">姓名 *</label>
<input type="text" id="userName" name="name" required>
</div>
<div class="form-group">
<label for="userAge">年龄(可选)</label>
<input type="number" id="userAge" name="age" min="1" max="150">
</div>
<div class="form-group">
<label for="userPhoto">人脸照片 *</label>
<input type="file" id="userPhoto" name="image" accept="image/*" required>
<img id="preview" style="display:none;">
</div>
<button type="submit" class="btn" id="registerBtn">注册用户</button>
</form>
</div>
<!-- 用户列表 -->
<div class="section">
<h2>📋 用户列表</h2>
<button class="btn" onclick="loadUsers()">刷新列表</button>
<div id="usersList"></div>
</div>
</div>
</div>
<script>
const API_BASE = window.location.origin;
// 加载统计数据
async function loadStats() {
try {
const response = await fetch(`${API_BASE}/api/stats`);
const data = await response.json();
if (data.success) {
document.getElementById('totalUsers').textContent = data.stats.total_users;
document.getElementById('totalRecognitions').textContent = data.stats.total_recognitions;
document.getElementById('successfulRecognitions').textContent = data.stats.successful_recognitions;
document.getElementById('recognitionRate').textContent = data.stats.recognition_rate + '%';
}
} catch (error) {
console.error('加载统计数据失败:', error);
}
}
// 加载用户列表
async function loadUsers() {
const usersListDiv = document.getElementById('usersList');
usersListDiv.innerHTML = '<div class="loading">加载中...</div>';
try {
const response = await fetch(`${API_BASE}/api/users/list`);
const data = await response.json();
if (data.success) {
if (data.users.length === 0) {
usersListDiv.innerHTML = '<p style="text-align:center; color:#666; padding:20px;">暂无注册用户</p>';
return;
}
let html = `
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>特征维度</th>
<th>注册时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
`;
data.users.forEach(user => {
const createdAt = new Date(user.created_at).toLocaleString('zh-CN');
html += `
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age || '-'}</td>
<td>${user.feature_dim}</td>
<td>${createdAt}</td>
<td><span class="badge ${user.is_active ? 'badge-success' : 'badge-danger'}">${user.is_active ? '激活' : '禁用'}</span></td>
<td><button class="btn" onclick="deleteUser(${user.id}, '${user.name}')" style="background:#dc3545; padding:6px 15px;">删除</button></td>
</tr>
`;
});
html += '</tbody></table>';
usersListDiv.innerHTML = html;
} else {
usersListDiv.innerHTML = `<p style="color:red;">加载失败: ${data.message}</p>`;
}
} catch (error) {
usersListDiv.innerHTML = `<p style="color:red;">加载失败: ${error.message}</p>`;
}
}
// 删除用户
async function deleteUser(userId, userName) {
if (!confirm(`确定要删除用户 "${userName}" 吗?`)) {
return;
}
try {
const response = await fetch(`${API_BASE}/api/users/${userId}`, {
method: 'DELETE'
});
const data = await response.json();
if (data.success) {
alert(data.message);
loadUsers();
loadStats();
} else {
alert('删除失败: ' + data.message);
}
} catch (error) {
alert('删除失败: ' + error.message);
}
}
// 图片预览
document.getElementById('userPhoto').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const preview = document.getElementById('preview');
preview.src = e.target.result;
preview.style.display = 'block';
}
reader.readAsDataURL(file);
}
});
// 注册表单提交
document.getElementById('registerForm').addEventListener('submit', async function(e) {
e.preventDefault();
const messageDiv = document.getElementById('registerMessage');
const submitBtn = document.getElementById('registerBtn');
messageDiv.style.display = 'none';
submitBtn.disabled = true;
submitBtn.textContent = '注册中...';
// 手动构建 FormData,确保字段名正确
const formData = new FormData();
formData.append('name', document.getElementById('userName').value.trim());
const ageInput = document.getElementById('userAge').value;
if (ageInput) {
formData.append('age', ageInput);
}
const photoFile = document.getElementById('userPhoto').files[0];
if (!photoFile) {
messageDiv.className = 'message error';
messageDiv.textContent = '❌ 请选择人脸照片';
messageDiv.style.display = 'block';
submitBtn.disabled = false;
submitBtn.textContent = '注册用户';
return;
}
formData.append('image', photoFile);
try {
const response = await fetch(`${API_BASE}/api/users/register`, {
method: 'POST',
body: formData
});
const data = await response.json();
if (response.ok && data.success) {
messageDiv.className = 'message success';
messageDiv.textContent = '✅ ' + data.message;
messageDiv.style.display = 'block';
// 重置表单
e.target.reset();
document.getElementById('preview').style.display = 'none';
// 刷新列表和统计
loadUsers();
loadStats();
} else {
// 显示详细错误信息
let errorMsg = data.message || data.detail || '注册失败';
throw new Error(`[${response.status}] ${errorMsg}`);
}
} catch (error) {
messageDiv.className = 'message error';
messageDiv.textContent = '❌ ' + error.message;
messageDiv.style.display = 'block';
console.error('注册错误详情:', error);
} finally {
submitBtn.disabled = false;
submitBtn.textContent = '注册用户';
}
});
// 页面加载时初始化
window.addEventListener('load', function() {
loadStats();
loadUsers();
});
</script>
</body>
</html>