UrsGetUserTeamRequest.php 3.5 KB

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