UrsCheckBalanceRequest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace ThirdParty\Urs\Request;
  3. use App\Module\ThirdParty\Services\BaseRequest;
  4. /**
  5. * URS余额检查请求类
  6. *
  7. * 专门处理余额检查请求,遵循"一个Request类只完成一种请求"的原则
  8. */
  9. class UrsCheckBalanceRequest extends BaseRequest
  10. {
  11. /**
  12. * 构造函数
  13. */
  14. public function __construct()
  15. {
  16. // 使用'urs'作为服务代码,需要在thirdparty_services表中注册
  17. parent::__construct('urs');
  18. }
  19. /**
  20. * 处理余额检查请求
  21. *
  22. * @param array $params 请求参数
  23. * @return array
  24. * @throws \Exception
  25. */
  26. protected function handler(array $params): array
  27. {
  28. return $this->handleCheck($params);
  29. }
  30. /**
  31. * 处理余额检查
  32. *
  33. * @param array $params 请求参数
  34. * @return array
  35. * @throws \Exception
  36. */
  37. protected function handleCheck(array $params): array
  38. {
  39. // 验证必需参数
  40. if (empty($params['user_id'])) {
  41. throw new \Exception('user_id参数是必填的');
  42. }
  43. // 获取URS配置
  44. $config = $this->getConfig();
  45. $apiUrl = $config['api_url'] ?? '';
  46. $appId = $config['app_id'] ?? '';
  47. $appSecret = $config['app_secret'] ?? '';
  48. if (empty($apiUrl) || empty($appId) || empty($appSecret)) {
  49. throw new \Exception('URS配置不完整,缺少api_url、app_id或app_secret');
  50. }
  51. $requestData = [
  52. 'app_id' => $appId,
  53. 'user_id' => $params['user_id'],
  54. 'timestamp' => time(),
  55. ];
  56. $requestData['sign'] = $this->generateSign($requestData, $appSecret);
  57. $response = $this->sendHttpRequest($apiUrl . '/check', $requestData);
  58. return [
  59. 'success' => $response['code'] === 0,
  60. 'message' => $response['message'] ?? '',
  61. 'data' => $response['data'] ?? [],
  62. ];
  63. }
  64. /**
  65. * 生成签名
  66. *
  67. * @param array $data 数据
  68. * @param string $secret 密钥
  69. * @return string
  70. */
  71. protected function generateSign(array $data, string $secret): string
  72. {
  73. // 排序参数
  74. ksort($data);
  75. // 构建签名字符串
  76. $signString = '';
  77. foreach ($data as $key => $value) {
  78. if ($key !== 'sign') {
  79. $signString .= $key . '=' . $value . '&';
  80. }
  81. }
  82. $signString .= 'key=' . $secret;
  83. return md5($signString);
  84. }
  85. /**
  86. * 发送HTTP请求
  87. *
  88. * @param string $url 请求URL
  89. * @param array $data 请求数据
  90. * @return array
  91. * @throws \Exception
  92. */
  93. protected function sendHttpRequest(string $url, array $data): array
  94. {
  95. $ch = curl_init();
  96. curl_setopt_array($ch, [
  97. CURLOPT_URL => $url,
  98. CURLOPT_POST => true,
  99. CURLOPT_POSTFIELDS => json_encode($data),
  100. CURLOPT_RETURNTRANSFER => true,
  101. CURLOPT_TIMEOUT => 30,
  102. CURLOPT_HTTPHEADER => [
  103. 'Content-Type: application/json',
  104. 'User-Agent: KKU-ThirdParty-URS/1.0',
  105. ],
  106. ]);
  107. $response = curl_exec($ch);
  108. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  109. $error = curl_error($ch);
  110. curl_close($ch);
  111. if ($error) {
  112. throw new \Exception("HTTP请求失败: {$error}");
  113. }
  114. if ($httpCode !== 200) {
  115. throw new \Exception("HTTP请求失败,状态码: {$httpCode}");
  116. }
  117. $result = json_decode($response, true);
  118. if (json_last_error() !== JSON_ERROR_NONE) {
  119. throw new \Exception("响应数据格式错误: " . json_last_error_msg());
  120. }
  121. return $result;
  122. }
  123. }