array_keys($params), 'userKey_length' => isset($params['userKey']) ? strlen($params['userKey']) : 0 ]); // 验证必需参数 if (empty($params['userKey'])) { Log::error('URS获取用户信息请求参数验证失败', ['error' => 'userKey参数是必填的']); throw new \Exception('userKey参数是必填的'); } try { // 获取URS配置 $c = $this->getUrsCredential(); $apiUrl = $this->getService()->base_url; // 直接使用服务的base_url $appKey = $c->getApiKey(); $ecologyId = $c->getEcologyId(); // 修复:应该获取ecologyId而不是apiKey Log::info('URS配置获取成功', [ 'api_url' => $apiUrl, 'app_key_length' => strlen($appKey), 'ecology_id' => $ecologyId ]); if (empty($apiUrl) || empty($appKey)) { Log::error('URS配置验证失败', [ 'api_url_empty' => empty($apiUrl), 'app_key_empty' => empty($appKey), 'api_url' => $apiUrl, 'app_key_length' => strlen($appKey) ]); throw new \Exception('URS配置不完整,缺少api_url或app_key'); } // 构建请求数据 $requestData = [ 'userKey' => $params['userKey'] ]; Log::info('开始加密请求数据', [ 'request_data_keys' => array_keys($requestData) ]); // 使用URS加密服务加密请求数据 $cryptoService = new CryptoService($appKey); $encryptedData = $cryptoService->encrypt($requestData); Log::info('请求数据加密完成', [ 'encrypted_data_keys' => array_keys($encryptedData) ]); // 构建完整的API URL $fullUrl = rtrim($apiUrl, '/') . "/api/ecology/{$ecologyId}/userInfo"; Log::info('准备发送HTTP请求', [ 'full_url' => $fullUrl ]); // 发送HTTP请求到URS $response = $this->sendHttpRequest($fullUrl, $encryptedData); Log::info('HTTP请求响应接收完成', [ 'response_keys' => array_keys($response), 'has_encrypted_fields' => isset($response['data'], $response['iv'], $response['timestamp'], $response['sign']) ]); // 解密响应数据 if (isset($response['data'], $response['iv'], $response['timestamp'], $response['sign'])) { Log::info('开始解密响应数据'); $decryptedResponse = $cryptoService->decrypt($response); Log::info('URS获取用户信息成功', [ 'decrypted_response_keys' => array_keys($decryptedResponse) ]); return [ 'success' => true, 'message' => '获取用户信息成功', 'data' => $decryptedResponse, ]; } // 如果响应格式不正确,直接返回原始响应 Log::error('URS响应格式错误', [ 'response' => $response, 'missing_fields' => [ 'data' => !isset($response['data']), 'iv' => !isset($response['iv']), 'timestamp' => !isset($response['timestamp']), 'sign' => !isset($response['sign']) ] ]); return [ 'success' => false, 'message' => '响应格式错误', 'data' => $response, ]; } catch (\Exception $e) { Log::error('URS获取用户信息处理异常', [ 'error_message' => $e->getMessage(), 'error_file' => $e->getFile(), 'error_line' => $e->getLine(), 'userKey_length' => isset($params['userKey']) ? strlen($params['userKey']) : 0 ]); throw $e; } } /** * 发送HTTP请求 * * @param string $url 请求URL * @param array $data 请求数据 * @return array * @throws \Exception */ protected function sendHttpRequest(string $url, array $data): array { // 记录请求开始日志 Log::info('URS HTTP请求开始', [ 'url' => $url, 'request_data_keys' => array_keys($data), 'request_data_size' => strlen(json_encode($data)), 'method' => 'POST' ]); $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'User-Agent: KKU-ThirdParty-URS/1.0', ], ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); $curlInfo = curl_getinfo($ch); curl_close($ch); // 记录详细的响应信息 Log::info('URS HTTP请求完成', [ 'url' => $url, 'http_code' => $httpCode, 'curl_error' => $error, 'response_size' => strlen($response), 'total_time' => $curlInfo['total_time'] ?? 0, 'connect_time' => $curlInfo['connect_time'] ?? 0, 'response_preview' => substr($response, 0, 500) // 只记录前500字符 ]); if ($error) { Log::error('URS HTTP请求CURL错误', [ 'url' => $url, 'curl_error' => $error, 'curl_info' => $curlInfo ]); throw new \Exception("HTTP请求失败: {$error}"); } if ($httpCode !== 200) { Log::error('URS HTTP请求状态码错误', [ 'url' => $url, 'http_code' => $httpCode, 'response_body' => $response, 'curl_info' => $curlInfo ]); throw new \Exception("HTTP请求失败,状态码: {$httpCode},响应内容: " . substr($response, 0, 200)); } $decodedResponse = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { Log::error('URS HTTP响应JSON解析失败', [ 'url' => $url, 'json_error' => json_last_error_msg(), 'response_body' => $response ]); throw new \Exception("响应JSON解析失败: " . json_last_error_msg() . ",响应内容: " . substr($response, 0, 200)); } return $decodedResponse; } }