query("SELECT * FROM persons ORDER BY name");
$all_persons = $stmt->fetchAll();
$message = '';
$error = '';
// 处理关联观影人(当前用户)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'link_person') {
$admin_id = (int)$_POST['admin_id'];
$person_id = !empty($_POST['person_id']) ? (int)$_POST['person_id'] : null;
if ($admin_id != $_SESSION['admin_id']) {
$error = '只能修改自己的关联';
} else {
if (updateAdminPersonId($admin_id, $person_id)) {
$message = '关联观影人更新成功';
$_SESSION['admin_id'] = $admin_id;
$admin = getCurrentAdmin();
} else {
$error = '更新失败';
}
}
}
// 处理添加用户
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
$new_user = trim($_POST['new_username'] ?? '');
$new_pass = $_POST['new_password'] ?? '';
$confirm = $_POST['confirm_password'] ?? '';
$person_id = !empty($_POST['person_id']) ? (int)$_POST['person_id'] : null;
if (empty($new_user)) {
$error = '用户名不能为空';
} elseif (strlen($new_pass) < 6) {
$error = '密码至少6位';
} elseif ($new_pass !== $confirm) {
$error = '两次密码不一致';
} else {
if (addAdmin($new_user, $new_pass, null, $person_id)) {
$message = "用户 {$new_user} 添加成功";
} else {
$error = '用户名已存在';
}
}
}
// 处理编辑用户
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit') {
$edit_id = (int)$_POST['id'];
$username = trim($_POST['username'] ?? '');
$new_password = $_POST['new_password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
$person_id = !empty($_POST['person_id']) ? (int)$_POST['person_id'] : null;
if (empty($username)) {
$error = '用户名不能为空';
} else {
// 检查用户名是否已被其他用户使用
$stmt = $db->prepare("SELECT COUNT(*) FROM admins WHERE username = ? AND id != ?");
$stmt->execute([$username, $edit_id]);
if ($stmt->fetchColumn() > 0) {
$error = '用户名已被占用';
} else {
// 构建更新语句
$sql = "UPDATE admins SET username = ?, person_id = ?";
$params = [$username, $person_id];
if (!empty($new_password)) {
if (strlen($new_password) < 6) {
$error = '密码至少6位';
} elseif ($new_password !== $confirm_password) {
$error = '两次密码不一致';
} else {
$hashed = password_hash($new_password, PASSWORD_DEFAULT);
$sql .= ", password = ?";
$params[] = $hashed;
}
}
if (empty($error)) {
$sql .= " WHERE id = ?";
$params[] = $edit_id;
$stmt = $db->prepare($sql);
if ($stmt->execute($params)) {
$message = "用户 {$username} 更新成功";
// 如果编辑的是自己,刷新 session 信息
if ($edit_id == $_SESSION['admin_id']) {
$_SESSION['admin_id'] = $edit_id;
$admin = getCurrentAdmin();
}
} else {
$error = '更新失败';
}
}
}
}
}
// 处理删除用户
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') {
$delete_id = (int)$_POST['delete_id'];
if (deleteAdmin($delete_id)) {
$message = '用户已删除';
} else {
$error = '无法删除该用户(可能是最后一个管理员或您自己)';
}
}
// 获取编辑数据
$edit_data = null;
if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['id'])) {
$edit_id = (int)$_GET['id'];
$stmt = $db->prepare("SELECT * FROM admins WHERE id = ?");
$stmt->execute([$edit_id]);
$edit_data = $stmt->fetch();
if (!$edit_data) {
header('Location: users.php');
exit;
}
}
$users = getAllAdmins();
?>
用户管理 - 电影日志