UrsGetUserInfoRequest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace ThirdParty\Urs\Request;
  3. use App\Module\ThirdParty\Services\BaseRequest;
  4. use ThirdParty\Urs\Util\CryptoService;
  5. /**
  6. * URS获取用户信息请求类
  7. *
  8. * 专门处理获取用户信息请求,遵循"一个Request类只完成一种请求"的原则
  9. * 根据用户密钥获取用户ID
  10. */
  11. class UrsGetUserInfoRequest extends BaseRequest
  12. {
  13. /**
  14. * 构造函数
  15. */
  16. public function __construct()
  17. {
  18. // 使用'urs'作为服务代码,需要在thirdparty_services表中注册
  19. parent::__construct('urs');
  20. }
  21. /**
  22. * 处理获取用户信息请求
  23. *
  24. * @param array $params 请求参数,包含userKey字段
  25. * @return array 返回包含userId的数组
  26. * @throws \Exception
  27. */
  28. protected function handler(array $params): array
  29. {
  30. // 验证必需参数
  31. if (empty($params['userKey'])) {
  32. throw new \Exception('userKey参数是必填的');
  33. }
  34. // 获取URS配置
  35. $config = $this->getConfig();
  36. $apiUrl = $config['api_url'] ?? '';
  37. $appKey = $config['app_key'] ?? '';
  38. $ecologyId = $config['ecology_id'] ?? 1;
  39. if (empty($apiUrl) || empty($appKey)) {
  40. throw new \Exception('URS配置不完整,缺少api_url或app_key');
  41. }
  42. // 构建请求数据
  43. $requestData = [
  44. 'userKey' => $params['userKey']
  45. ];
  46. // 使用URS加密服务加密请求数据
  47. $cryptoService = new CryptoService($appKey);
  48. $encryptedData = $cryptoService->encrypt($requestData);
  49. // 构建完整的API URL
  50. $fullUrl = rtrim($apiUrl, '/') . "/api/ecology/{$ecologyId}/userInfo";
  51. // 发送HTTP请求到URS
  52. $response = $this->sendHttpRequest($fullUrl, $encryptedData);
  53. // 解密响应数据
  54. if (isset($response['data'], $response['iv'], $response['timestamp'], $response['sign'])) {
  55. $decryptedResponse = $cryptoService->decrypt($response);
  56. return [
  57. 'success' => true,
  58. 'message' => '获取用户信息成功',
  59. 'data' => $decryptedResponse,
  60. ];
  61. }
  62. // 如果响应格式不正确,直接返回原始响应
  63. return [
  64. 'success' => false,
  65. 'message' => '响应格式错误',
  66. 'data' => $response,
  67. ];
  68. }
  69. /**
  70. * 发送HTTP请求
  71. *
  72. * @param string $url 请求URL
  73. * @param array $data 请求数据
  74. * @return array
  75. * @throws \Exception
  76. */
  77. protected function sendHttpRequest(string $url, array $data): array
  78. {
  79. $ch = curl_init();
  80. curl_setopt_array($ch, [
  81. CURLOPT_URL => $url,
  82. CURLOPT_POST => true,
  83. CURLOPT_POSTFIELDS => json_encode($data),
  84. CURLOPT_RETURNTRANSFER => true,
  85. CURLOPT_TIMEOUT => 30,
  86. CURLOPT_HTTPHEADER => [
  87. 'Content-Type: application/json',
  88. 'User-Agent: KKU-ThirdParty-URS/1.0',
  89. ],
  90. ]);
  91. $response = curl_exec($ch);
  92. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  93. $error = curl_error($ch);
  94. curl_close($ch);
  95. if ($error) {
  96. throw new \Exception("HTTP请求失败: {$error}");
  97. }
  98. if ($httpCode !== 200) {
  99. throw new \Exception("HTTP请求失败,状态码: {$httpCode}");
  100. }
  101. $decodedResponse = json_decode($response, true);
  102. if (json_last_error() !== JSON_ERROR_NONE) {
  103. throw new \Exception("响应JSON解析失败: " . json_last_error_msg());
  104. }
  105. return $decodedResponse;
  106. }
  107. }