getJsonInput(); return $json[$key] ?? $_GET[$key] ?? $_POST[$key] ?? $default; } /** * 返回成功响应 */ protected function success(mixed $data = null, string $message = '操作成功', int $code = 200): never { Application::jsonResponse($code, $message, $data); } /** * 返回错误响应 */ protected function error(string $message = '操作失败', int $code = 400, mixed $data = null, int $httpCode = 200): never { Application::jsonResponse($code, $message, $data, $httpCode); } /** * 获取当前用户ID(由Auth中间件注入) */ protected function getUserId(): ?int { return $_SERVER['HTTP_X_USER_ID'] ?? null; } /** * 获取当前用户角色 */ protected function getUserRole(): string { return $_SERVER['HTTP_X_USER_ROLE'] ?? ''; } /** * 获取当前用户信息(由Auth中间件从数据库实时刷新) */ protected function getCurrentUser(): ?array { $userId = $this->getUserId(); if (!$userId) return null; return [ 'id' => (int) $userId, 'username' => $_SERVER['HTTP_X_USER_NAME'] ?? '', 'nickname' => $_SERVER['HTTP_X_USER_NICKNAME'] ?? '', 'role' => $_SERVER['HTTP_X_USER_ROLE'] ?? '', 'parent_id' => (int) ($_SERVER['HTTP_X_USER_PARENT'] ?? 0), 'project_ids' => json_decode($_SERVER['HTTP_X_USER_PROJECT_IDS'] ?? '[]', true) ?: [], ]; } }