UrsRegisterRequest.php 4.0 KB

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