getPdo(); $chk = $pdo->prepare("SELECT COUNT(*) FROM system_users WHERE username = ?"); $chk->execute([$username]); if ((int)$chk->fetchColumn() > 0) { Response::error('账号已存在'); } $chkRole = $pdo->prepare("SELECT COUNT(*) FROM system_roles WHERE id = ?"); $chkRole->execute([$roleId]); if ((int)$chkRole->fetchColumn() === 0) { Response::error('角色不存在', 400); } $stmt = $pdo->prepare( "INSERT INTO system_users (username, password, real_name, role_id, is_active) VALUES (?, ?, ?, ?, ?)" ); $stmt->execute([$username, sha1($password), $realName !== '' ? $realName : null, $roleId, $isActive]); $newId = (int)$pdo->lastInsertId(); logCurrent('add', 'system', 'system_users', $newId, ['username' => $username, 'role_id' => $roleId, 'is_active' => $isActive]); Response::success(['id' => $newId], '新增成功');