| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace ThirdParty\Urs\Request;
- use App\Module\ThirdParty\Services\BaseRequest;
- /**
- * URS请求类示例
- *
- * 继承ThirdParty模块的请求基类,实现URS特定的请求逻辑
- */
- class UrsRequest extends BaseRequest
- {
- /**
- * 构造函数
- */
- public function __construct()
- {
- // 使用'urs'作为服务代码,需要在thirdparty_services表中注册
- parent::__construct('urs');
- }
- /**
- * 具体的请求处理逻辑
- *
- * @param array $params 请求参数
- * @return array
- * @throws \Exception
- */
- protected function handler(array $params): array
- {
- $action = $params['action'] ?? '';
- switch ($action) {
- case 'register':
- return $this->handleRegister($params);
- case 'deposit':
- return $this->handleDeposit($params);
- case 'withdraw':
- return $this->handleWithdraw($params);
- case 'check':
- return $this->handleCheck($params);
- default:
- throw new \Exception("不支持的操作类型: {$action}");
- }
- }
- /**
- * 处理用户注册
- *
- * @param array $params 请求参数
- * @return array
- */
- protected function handleRegister(array $params): array
- {
- // 获取URS配置
- $config = $this->getConfig();
- $apiUrl = $config['api_url'] ?? '';
- $appId = $config['app_id'] ?? '';
- $appSecret = $config['app_secret'] ?? '';
- // 构建注册请求数据
- $requestData = [
- 'app_id' => $appId,
- 'user_id' => $params['user_id'],
- 'username' => $params['username'],
- 'timestamp' => time(),
- ];
- // 生成签名
- $requestData['sign'] = $this->generateSign($requestData, $appSecret);
- // 发送HTTP请求到URS
- $response = $this->sendHttpRequest($apiUrl . '/register', $requestData);
- return [
- 'success' => $response['code'] === 0,
- 'message' => $response['message'] ?? '',
- 'data' => $response['data'] ?? [],
- ];
- }
- /**
- * 处理充值操作
- *
- * @param array $params 请求参数
- * @return array
- */
- protected function handleDeposit(array $params): array
- {
- $config = $this->getConfig();
- $apiUrl = $config['api_url'] ?? '';
- $appId = $config['app_id'] ?? '';
- $appSecret = $config['app_secret'] ?? '';
- $requestData = [
- 'app_id' => $appId,
- 'user_id' => $params['user_id'],
- 'amount' => $params['amount'],
- 'order_id' => $params['order_id'],
- 'timestamp' => time(),
- ];
- $requestData['sign'] = $this->generateSign($requestData, $appSecret);
- $response = $this->sendHttpRequest($apiUrl . '/deposit', $requestData);
- return [
- 'success' => $response['code'] === 0,
- 'message' => $response['message'] ?? '',
- 'data' => $response['data'] ?? [],
- ];
- }
- /**
- * 处理提取操作
- *
- * @param array $params 请求参数
- * @return array
- */
- protected function handleWithdraw(array $params): array
- {
- $config = $this->getConfig();
- $apiUrl = $config['api_url'] ?? '';
- $appId = $config['app_id'] ?? '';
- $appSecret = $config['app_secret'] ?? '';
- $requestData = [
- 'app_id' => $appId,
- 'user_id' => $params['user_id'],
- 'amount' => $params['amount'],
- 'order_id' => $params['order_id'],
- 'timestamp' => time(),
- ];
- $requestData['sign'] = $this->generateSign($requestData, $appSecret);
- $response = $this->sendHttpRequest($apiUrl . '/withdraw', $requestData);
- return [
- 'success' => $response['code'] === 0,
- 'message' => $response['message'] ?? '',
- 'data' => $response['data'] ?? [],
- ];
- }
- /**
- * 处理余额检查
- *
- * @param array $params 请求参数
- * @return array
- */
- protected function handleCheck(array $params): array
- {
- $config = $this->getConfig();
- $apiUrl = $config['api_url'] ?? '';
- $appId = $config['app_id'] ?? '';
- $appSecret = $config['app_secret'] ?? '';
- $requestData = [
- 'app_id' => $appId,
- 'user_id' => $params['user_id'],
- 'timestamp' => time(),
- ];
- $requestData['sign'] = $this->generateSign($requestData, $appSecret);
- $response = $this->sendHttpRequest($apiUrl . '/check', $requestData);
- return [
- 'success' => $response['code'] === 0,
- 'message' => $response['message'] ?? '',
- 'data' => $response['data'] ?? [],
- ];
- }
- /**
- * 生成签名
- *
- * @param array $data 数据
- * @param string $secret 密钥
- * @return string
- */
- protected function generateSign(array $data, string $secret): string
- {
- // 排序参数
- ksort($data);
- // 构建签名字符串
- $signString = '';
- foreach ($data as $key => $value) {
- if ($key !== 'sign') {
- $signString .= $key . '=' . $value . '&';
- }
- }
- $signString .= 'key=' . $secret;
- return md5($signString);
- }
- /**
- * 发送HTTP请求
- *
- * @param string $url 请求URL
- * @param array $data 请求数据
- * @return array
- * @throws \Exception
- */
- protected function sendHttpRequest(string $url, array $data): array
- {
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- 'User-Agent: KKU-ThirdParty-URS/1.0',
- ],
- ]);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $error = curl_error($ch);
- curl_close($ch);
- if ($error) {
- throw new \Exception("HTTP请求失败: {$error}");
- }
- if ($httpCode !== 200) {
- throw new \Exception("HTTP请求失败,状态码: {$httpCode}");
- }
- $result = json_decode($response, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- throw new \Exception("响应数据格式错误: " . json_last_error_msg());
- }
- return $result;
- }
- }
|