UrsGetUserLevelCountRequest.php 3.7 KB

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