UrsRequest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace ThirdParty\Urs;
  3. use App\Module\ThirdParty\Services\BaseRequest;
  4. /**
  5. * URS请求类示例
  6. *
  7. * 继承ThirdParty模块的请求基类,实现URS特定的请求逻辑
  8. */
  9. class UrsRequest 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. $action = $params['action'] ?? '';
  29. switch ($action) {
  30. case 'register':
  31. return $this->handleRegister($params);
  32. case 'deposit':
  33. return $this->handleDeposit($params);
  34. case 'withdraw':
  35. return $this->handleWithdraw($params);
  36. case 'check':
  37. return $this->handleCheck($params);
  38. default:
  39. throw new \Exception("不支持的操作类型: {$action}");
  40. }
  41. }
  42. /**
  43. * 处理用户注册
  44. *
  45. * @param array $params 请求参数
  46. * @return array
  47. */
  48. protected function handleRegister(array $params): array
  49. {
  50. // 获取URS配置
  51. $config = $this->getConfig();
  52. $apiUrl = $config['api_url'] ?? '';
  53. $appId = $config['app_id'] ?? '';
  54. $appSecret = $config['app_secret'] ?? '';
  55. // 构建注册请求数据
  56. $requestData = [
  57. 'app_id' => $appId,
  58. 'user_id' => $params['user_id'],
  59. 'username' => $params['username'],
  60. 'timestamp' => time(),
  61. ];
  62. // 生成签名
  63. $requestData['sign'] = $this->generateSign($requestData, $appSecret);
  64. // 发送HTTP请求到URS
  65. $response = $this->sendHttpRequest($apiUrl . '/register', $requestData);
  66. return [
  67. 'success' => $response['code'] === 0,
  68. 'message' => $response['message'] ?? '',
  69. 'data' => $response['data'] ?? [],
  70. ];
  71. }
  72. /**
  73. * 处理充值操作
  74. *
  75. * @param array $params 请求参数
  76. * @return array
  77. */
  78. protected function handleDeposit(array $params): array
  79. {
  80. $config = $this->getConfig();
  81. $apiUrl = $config['api_url'] ?? '';
  82. $appId = $config['app_id'] ?? '';
  83. $appSecret = $config['app_secret'] ?? '';
  84. $requestData = [
  85. 'app_id' => $appId,
  86. 'user_id' => $params['user_id'],
  87. 'amount' => $params['amount'],
  88. 'order_id' => $params['order_id'],
  89. 'timestamp' => time(),
  90. ];
  91. $requestData['sign'] = $this->generateSign($requestData, $appSecret);
  92. $response = $this->sendHttpRequest($apiUrl . '/deposit', $requestData);
  93. return [
  94. 'success' => $response['code'] === 0,
  95. 'message' => $response['message'] ?? '',
  96. 'data' => $response['data'] ?? [],
  97. ];
  98. }
  99. /**
  100. * 处理提取操作
  101. *
  102. * @param array $params 请求参数
  103. * @return array
  104. */
  105. protected function handleWithdraw(array $params): array
  106. {
  107. $config = $this->getConfig();
  108. $apiUrl = $config['api_url'] ?? '';
  109. $appId = $config['app_id'] ?? '';
  110. $appSecret = $config['app_secret'] ?? '';
  111. $requestData = [
  112. 'app_id' => $appId,
  113. 'user_id' => $params['user_id'],
  114. 'amount' => $params['amount'],
  115. 'order_id' => $params['order_id'],
  116. 'timestamp' => time(),
  117. ];
  118. $requestData['sign'] = $this->generateSign($requestData, $appSecret);
  119. $response = $this->sendHttpRequest($apiUrl . '/withdraw', $requestData);
  120. return [
  121. 'success' => $response['code'] === 0,
  122. 'message' => $response['message'] ?? '',
  123. 'data' => $response['data'] ?? [],
  124. ];
  125. }
  126. /**
  127. * 处理余额检查
  128. *
  129. * @param array $params 请求参数
  130. * @return array
  131. */
  132. protected function handleCheck(array $params): array
  133. {
  134. $config = $this->getConfig();
  135. $apiUrl = $config['api_url'] ?? '';
  136. $appId = $config['app_id'] ?? '';
  137. $appSecret = $config['app_secret'] ?? '';
  138. $requestData = [
  139. 'app_id' => $appId,
  140. 'user_id' => $params['user_id'],
  141. 'timestamp' => time(),
  142. ];
  143. $requestData['sign'] = $this->generateSign($requestData, $appSecret);
  144. $response = $this->sendHttpRequest($apiUrl . '/check', $requestData);
  145. return [
  146. 'success' => $response['code'] === 0,
  147. 'message' => $response['message'] ?? '',
  148. 'data' => $response['data'] ?? [],
  149. ];
  150. }
  151. /**
  152. * 生成签名
  153. *
  154. * @param array $data 数据
  155. * @param string $secret 密钥
  156. * @return string
  157. */
  158. protected function generateSign(array $data, string $secret): string
  159. {
  160. // 排序参数
  161. ksort($data);
  162. // 构建签名字符串
  163. $signString = '';
  164. foreach ($data as $key => $value) {
  165. if ($key !== 'sign') {
  166. $signString .= $key . '=' . $value . '&';
  167. }
  168. }
  169. $signString .= 'key=' . $secret;
  170. return md5($signString);
  171. }
  172. /**
  173. * 发送HTTP请求
  174. *
  175. * @param string $url 请求URL
  176. * @param array $data 请求数据
  177. * @return array
  178. * @throws \Exception
  179. */
  180. protected function sendHttpRequest(string $url, array $data): array
  181. {
  182. $ch = curl_init();
  183. curl_setopt_array($ch, [
  184. CURLOPT_URL => $url,
  185. CURLOPT_POST => true,
  186. CURLOPT_POSTFIELDS => json_encode($data),
  187. CURLOPT_RETURNTRANSFER => true,
  188. CURLOPT_TIMEOUT => 30,
  189. CURLOPT_HTTPHEADER => [
  190. 'Content-Type: application/json',
  191. 'User-Agent: KKU-ThirdParty-URS/1.0',
  192. ],
  193. ]);
  194. $response = curl_exec($ch);
  195. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  196. $error = curl_error($ch);
  197. curl_close($ch);
  198. if ($error) {
  199. throw new \Exception("HTTP请求失败: {$error}");
  200. }
  201. if ($httpCode !== 200) {
  202. throw new \Exception("HTTP请求失败,状态码: {$httpCode}");
  203. }
  204. $result = json_decode($response, true);
  205. if (json_last_error() !== JSON_ERROR_NONE) {
  206. throw new \Exception("响应数据格式错误: " . json_last_error_msg());
  207. }
  208. return $result;
  209. }
  210. }