UrsGetUserInfoRequest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace ThirdParty\Urs\Request;
  3. use ThirdParty\Urs\Util\CryptoService;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * URS获取用户信息请求类
  7. *
  8. * 专门处理获取用户信息请求,遵循"一个Request类只完成一种请求"的原则
  9. * 根据用户密钥获取用户ID
  10. */
  11. class UrsGetUserInfoRequest extends BaseRequest
  12. {
  13. /**
  14. * 处理获取用户信息请求
  15. *
  16. * @param array $params 请求参数,包含userKey字段
  17. * @return array 返回包含userId的数组
  18. * @throws \Exception
  19. */
  20. protected function handler(array $params = []): array
  21. {
  22. Log::info('URS获取用户信息请求开始', [
  23. 'params_keys' => array_keys($params),
  24. 'userKey_length' => isset($params['userKey']) ? strlen($params['userKey']) : 0
  25. ]);
  26. // 验证必需参数
  27. if (empty($params['userKey'])) {
  28. Log::error('URS获取用户信息请求参数验证失败', ['error' => 'userKey参数是必填的']);
  29. throw new \Exception('userKey参数是必填的');
  30. }
  31. try {
  32. // 获取URS配置
  33. $c = $this->getUrsCredential();
  34. $apiUrl = $this->getService()->base_url; // 直接使用服务的base_url
  35. $appKey = $c->getApiKey();
  36. $ecologyId = $c->getEcologyId(); // 修复:应该获取ecologyId而不是apiKey
  37. Log::info('URS配置获取成功', [
  38. 'api_url' => $apiUrl,
  39. 'app_key_length' => strlen($appKey),
  40. 'ecology_id' => $ecologyId
  41. ]);
  42. if (empty($apiUrl) || empty($appKey)) {
  43. Log::error('URS配置验证失败', [
  44. 'api_url_empty' => empty($apiUrl),
  45. 'app_key_empty' => empty($appKey),
  46. 'api_url' => $apiUrl,
  47. 'app_key_length' => strlen($appKey)
  48. ]);
  49. throw new \Exception('URS配置不完整,缺少api_url或app_key');
  50. }
  51. // 构建请求数据
  52. $requestData = [
  53. 'userKey' => $params['userKey']
  54. ];
  55. Log::info('开始加密请求数据', [
  56. 'request_data_keys' => array_keys($requestData)
  57. ]);
  58. // 使用URS加密服务加密请求数据
  59. $cryptoService = new CryptoService($appKey);
  60. $encryptedData = $cryptoService->encrypt($requestData);
  61. Log::info('请求数据加密完成', [
  62. 'encrypted_data_keys' => array_keys($encryptedData)
  63. ]);
  64. // 构建完整的API URL
  65. $fullUrl = rtrim($apiUrl, '/') . "/api/ecology/{$ecologyId}/userInfo";
  66. Log::info('准备发送HTTP请求', [
  67. 'full_url' => $fullUrl
  68. ]);
  69. // 发送HTTP请求到URS
  70. $response = $this->sendHttpRequest($fullUrl, $encryptedData);
  71. Log::info('HTTP请求响应接收完成', [
  72. 'response_keys' => array_keys($response),
  73. 'has_encrypted_fields' => isset($response['data'], $response['iv'], $response['timestamp'], $response['sign'])
  74. ]);
  75. // 解密响应数据
  76. if (isset($response['data'], $response['iv'], $response['timestamp'], $response['sign'])) {
  77. Log::info('开始解密响应数据');
  78. $decryptedResponse = $cryptoService->decrypt($response);
  79. Log::info('URS获取用户信息成功', [
  80. 'decrypted_response_keys' => array_keys($decryptedResponse)
  81. ]);
  82. return [
  83. 'success' => true,
  84. 'message' => '获取用户信息成功',
  85. 'data' => $decryptedResponse,
  86. ];
  87. }
  88. // 如果响应格式不正确,直接返回原始响应
  89. Log::error('URS响应格式错误', [
  90. 'response' => $response,
  91. 'missing_fields' => [
  92. 'data' => !isset($response['data']),
  93. 'iv' => !isset($response['iv']),
  94. 'timestamp' => !isset($response['timestamp']),
  95. 'sign' => !isset($response['sign'])
  96. ]
  97. ]);
  98. return [
  99. 'success' => false,
  100. 'message' => '响应格式错误',
  101. 'data' => $response,
  102. ];
  103. } catch (\Exception $e) {
  104. Log::error('URS获取用户信息处理异常', [
  105. 'error_message' => $e->getMessage(),
  106. 'error_file' => $e->getFile(),
  107. 'error_line' => $e->getLine(),
  108. 'userKey_length' => isset($params['userKey']) ? strlen($params['userKey']) : 0
  109. ]);
  110. throw $e;
  111. }
  112. }
  113. /**
  114. * 发送HTTP请求
  115. *
  116. * @param string $url 请求URL
  117. * @param array $data 请求数据
  118. * @return array
  119. * @throws \Exception
  120. */
  121. protected function sendHttpRequest(string $url, array $data): array
  122. {
  123. // 记录请求开始日志
  124. Log::info('URS HTTP请求开始', [
  125. 'url' => $url,
  126. 'request_data_keys' => array_keys($data),
  127. 'request_data_size' => strlen(json_encode($data)),
  128. 'method' => 'POST'
  129. ]);
  130. $ch = curl_init();
  131. curl_setopt_array($ch, [
  132. CURLOPT_URL => $url,
  133. CURLOPT_POST => true,
  134. CURLOPT_POSTFIELDS => json_encode($data),
  135. CURLOPT_RETURNTRANSFER => true,
  136. CURLOPT_TIMEOUT => 30,
  137. CURLOPT_HTTPHEADER => [
  138. 'Content-Type: application/json',
  139. 'User-Agent: KKU-ThirdParty-URS/1.0',
  140. ],
  141. ]);
  142. $response = curl_exec($ch);
  143. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  144. $error = curl_error($ch);
  145. $curlInfo = curl_getinfo($ch);
  146. curl_close($ch);
  147. // 记录详细的响应信息
  148. Log::info('URS HTTP请求完成', [
  149. 'url' => $url,
  150. 'http_code' => $httpCode,
  151. 'curl_error' => $error,
  152. 'response_size' => strlen($response),
  153. 'total_time' => $curlInfo['total_time'] ?? 0,
  154. 'connect_time' => $curlInfo['connect_time'] ?? 0,
  155. 'response_preview' => substr($response, 0, 500) // 只记录前500字符
  156. ]);
  157. if ($error) {
  158. Log::error('URS HTTP请求CURL错误', [
  159. 'url' => $url,
  160. 'curl_error' => $error,
  161. 'curl_info' => $curlInfo
  162. ]);
  163. throw new \Exception("HTTP请求失败: {$error}");
  164. }
  165. if ($httpCode !== 200) {
  166. Log::error('URS HTTP请求状态码错误', [
  167. 'url' => $url,
  168. 'http_code' => $httpCode,
  169. 'response_body' => $response,
  170. 'curl_info' => $curlInfo
  171. ]);
  172. throw new \Exception("HTTP请求失败,状态码: {$httpCode},响应内容: " . substr($response, 0, 200));
  173. }
  174. $decodedResponse = json_decode($response, true);
  175. if (json_last_error() !== JSON_ERROR_NONE) {
  176. Log::error('URS HTTP响应JSON解析失败', [
  177. 'url' => $url,
  178. 'json_error' => json_last_error_msg(),
  179. 'response_body' => $response
  180. ]);
  181. throw new \Exception("响应JSON解析失败: " . json_last_error_msg() . ",响应内容: " . substr($response, 0, 200));
  182. }
  183. return $decodedResponse;
  184. }
  185. }