'system']); $id = (int)($_POST['id'] ?? 0); $ids = trim($_POST['ids'] ?? ''); $idList = []; if ($id > 0) $idList[] = $id; if ($ids !== '') { foreach (explode(',', $ids) as $v) { $v = (int)trim($v); if ($v > 0) $idList[] = $v; } } $idList = array_unique($idList); if (!$idList) { Response::error('参数错误', 400); } if (in_array(1, $idList, true)) { Response::error('不允许删除内置超级管理员角色', 400); } $pdo = DB::getInstance()->getPdo(); // 检查角色是否被使用 $in = implode(',', array_fill(0, count($idList), '?')); $stmt = $pdo->prepare("SELECT role_id, COUNT(*) AS cnt FROM system_users WHERE role_id IN ($in) GROUP BY role_id"); $stmt->execute($idList); $inUse = $stmt->fetchAll(); if ($inUse) { $names = []; foreach ($inUse as $r) { $names[] = "角色ID {$r['role_id']}({$r['cnt']}个用户)"; } Response::error('以下角色仍被用户使用,无法删除:' . implode('、', $names), 400); } $del = $pdo->prepare("DELETE FROM system_roles WHERE id IN ($in)"); $del->execute($idList); $affected = $del->rowCount(); logCurrent('delete', 'system', 'system_roles', null, ['ids' => $idList]); Response::success(['affected' => $affected], "已删除 $affected 个角色");