147 lines
5.2 KiB
PHP
147 lines
5.2 KiB
PHP
<?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>
|