Files
image-pichost/admin/images.php
2025-11-30 13:07:43 +00:00

152 lines
5.2 KiB
PHP

<?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>