| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace ThirdParty\Urs\Request;
- use App\Module\ThirdParty\Services\BaseRequest;
- /**
- * URS余额检查请求类
- *
- * 专门处理余额检查请求,遵循"一个Request类只完成一种请求"的原则
- */
- class UrsCheckBalanceRequest extends BaseRequest
- {
- /**
- * 构造函数
- */
- public function __construct()
- {
- // 使用'urs'作为服务代码,需要在thirdparty_services表中注册
- parent::__construct('urs');
- }
- /**
- * 处理余额检查请求
- *
- * @param array $params 请求参数
- * @return array
- * @throws \Exception
- */
- protected function handler(array $params): array
- {
- return $this->handleCheck($params);
- }
- /**
- * 处理余额检查
- *
- * @param array $params 请求参数
- * @return array
- * @throws \Exception
- */
- protected function handleCheck(array $params): array
- {
- // 验证必需参数
- if (empty($params['user_id'])) {
- throw new \Exception('user_id参数是必填的');
- }
- // 获取URS配置
- $config = $this->getConfig();
- $apiUrl = $config['api_url'] ?? '';
- $appId = $config['app_id'] ?? '';
- $appSecret = $config['app_secret'] ?? '';
- if (empty($apiUrl) || empty($appId) || empty($appSecret)) {
- throw new \Exception('URS配置不完整,缺少api_url、app_id或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;
- }
- }
|