上传文件至 admin

This commit is contained in:
2025-11-30 13:07:43 +00:00
parent 176718129e
commit d6572c69b6
2 changed files with 299 additions and 0 deletions

152
admin/images.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
// 获取图片列表
$page = $_GET['page'] ?? 1;
$limit = 20;
$offset = ($page - 1) * $limit;
try {
$stmt = $pdo->prepare("
SELECT i.*, u.username
FROM images i
LEFT JOIN users u ON i.user_id = u.id
ORDER BY i.uploaded_at DESC
LIMIT ? OFFSET ?
");
$stmt->bindValue(1, $limit, PDO::PARAM_INT);
$stmt->bindValue(2, $offset, PDO::PARAM_INT);
$stmt->execute();
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 获取总图片数
$stmt = $pdo->query("SELECT COUNT(*) as total FROM images");
$totalImages = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
$totalPages = ceil($totalImages / $limit);
} catch(PDOException $e) {
$images = [];
$totalPages = 1;
}
?>
<div class="admin-section">
<h3>图片管理 (<?php echo $totalImages; ?> 张图片)</h3>
<div class="table-responsive">
<table class="admin-table" id="images-table">
<thead>
<tr>
<th>ID</th>
<th>预览</th>
<th>标题</th>
<th>上传者</th>
<th>大小</th>
<th>状态</th>
<th>浏览量</th>
<th>上传时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($images as $image): ?>
<tr>
<td><?php echo $image['id']; ?></td>
<td>
<img src="uploads/<?php echo $image['filename']; ?>"
alt="预览"
style="width: 50px; height: 50px; object-fit: cover; border-radius: 5px;">
</td>
<td>
<strong><?php echo htmlspecialchars($image['title'] ?: '未命名'); ?></strong>
</td>
<td><?php echo htmlspecialchars($image['username']); ?></td>
<td><?php echo formatFileSize($image['file_size']); ?></td>
<td>
<span class="status-badge <?php echo $image['is_public'] ? 'status-public' : 'status-private'; ?>">
<?php echo $image['is_public'] ? '公开' : '私有'; ?>
</span>
</td>
<td><?php echo $image['views']; ?></td>
<td><?php echo date('Y-m-d H:i', strtotime($image['uploaded_at'])); ?></td>
<td>
<div class="admin-actions">
<a href="view-image.php?id=<?php echo $image['id']; ?>"
class="btn btn-sm" target="_blank">查看</a>
<button class="btn btn-sm"
onclick="toggleImagePublic(<?php echo $image['id']; ?>, <?php echo $image['is_public']; ?>)">
<?php echo $image['is_public'] ? '设为私有' : '设为公开'; ?>
</button>
<button class="btn btn-sm btn-danger"
onclick="deleteImage(<?php echo $image['id']; ?>, '<?php echo htmlspecialchars($image['title']); ?>')">
删除
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- 分页 -->
<?php if($totalPages > 1): ?>
<div class="pagination">
<?php for($i = 1; $i <= $totalPages; $i++): ?>
<button class="btn btn-sm <?php echo $i == $page ? 'btn-primary' : ''; ?>"
onclick="loadImages(<?php echo $i; ?>)">
<?php echo $i; ?>
</button>
<?php endfor; ?>
</div>
<?php endif; ?>
</div>
<script>
function toggleImagePublic(imageId, currentStatus) {
const newStatus = currentStatus ? 0 : 1;
const action = newStatus ? '公开' : '私有';
if (!confirm(`确定要将图片设为${action}吗?`)) return;
fetch('admin/ajax_toggle_image.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
image_id: imageId,
is_public: newStatus
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('图片状态更新成功!');
loadImages();
} else {
alert('更新失败: ' + data.error);
}
});
}
function deleteImage(imageId, title) {
if (!confirm(`确定要删除图片 "${title}" 吗?此操作不可恢复!`)) return;
fetch('admin/ajax_delete_image.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
image_id: imageId
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('图片删除成功!');
loadImages();
} else {
alert('删除失败: ' + data.error);
}
});
}
</script>

147
admin/users.php Normal file
View File

@@ -0,0 +1,147 @@
<?php
// 获取用户列表
$page = $_GET['page'] ?? 1;
$limit = 20;
$offset = ($page - 1) * $limit;
try {
$stmt = $pdo->prepare("SELECT * FROM users ORDER BY created_at DESC LIMIT ? OFFSET ?");
$stmt->bindValue(1, $limit, PDO::PARAM_INT);
$stmt->bindValue(2, $offset, PDO::PARAM_INT);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 获取总用户数
$stmt = $pdo->query("SELECT COUNT(*) as total FROM users");
$totalUsers = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
$totalPages = ceil($totalUsers / $limit);
} catch(PDOException $e) {
$users = [];
$totalPages = 1;
}
?>
<div class="admin-section">
<h3>用户列表 (<?php echo $totalUsers; ?> 个用户)</h3>
<div class="table-responsive">
<table class="admin-table" id="users-table">
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
<th>角色</th>
<th>状态</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td>
<strong><?php echo htmlspecialchars($user['username']); ?></strong>
<?php if($user['username'] === 'admin'): ?>
<span class="status-badge status-admin">管理员</span>
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($user['email']); ?></td>
<td>
<select onchange="updateUserRole(<?php echo $user['id']; ?>, this.value)"
<?php echo $user['username'] === 'admin' ? 'disabled' : ''; ?>>
<option value="user" <?php echo $user['role'] === 'user' ? 'selected' : ''; ?>>用户</option>
<option value="admin" <?php echo $user['role'] === 'admin' ? 'selected' : ''; ?>>管理员</option>
</select>
</td>
<td>
<span class="status-badge <?php echo $user['is_verified'] ? 'status-verified' : 'status-pending'; ?>">
<?php echo $user['is_verified'] ? '已验证' : '未验证'; ?>
</span>
</td>
<td><?php echo date('Y-m-d H:i', strtotime($user['created_at'])); ?></td>
<td>
<div class="admin-actions">
<button class="btn btn-sm"
onclick="viewUserImages(<?php echo $user['id']; ?>, '<?php echo htmlspecialchars($user['username']); ?>')">
查看图片
</button>
<button class="btn btn-sm btn-danger"
onclick="deleteUser(<?php echo $user['id']; ?>, '<?php echo htmlspecialchars($user['username']); ?>')"
<?php echo $user['username'] === 'admin' ? 'disabled' : ''; ?>>
删除
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- 分页 -->
<?php if($totalPages > 1): ?>
<div class="pagination">
<?php for($i = 1; $i <= $totalPages; $i++): ?>
<button class="btn btn-sm <?php echo $i == $page ? 'btn-primary' : ''; ?>"
onclick="loadUsers(<?php echo $i; ?>)">
<?php echo $i; ?>
</button>
<?php endfor; ?>
</div>
<?php endif; ?>
</div>
<script>
function updateUserRole(userId, newRole) {
if (!confirm('确定要更改用户角色吗?')) return;
fetch('admin/ajax_update_user.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: userId,
role: newRole
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('用户角色更新成功!');
loadUsers();
} else {
alert('更新失败: ' + data.error);
}
});
}
function viewUserImages(userId, username) {
window.open(`admin/user_images.php?id=${userId}`, '_blank');
}
function deleteUser(userId, username) {
if (!confirm(`确定要删除用户 "${username}" 吗?此操作将删除该用户的所有图片!`)) return;
fetch('admin/ajax_delete_user.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: userId
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('用户删除成功!');
loadUsers();
} else {
alert('删除失败: ' + data.error);
}
});
}
</script>