| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace ThirdParty\Urs\Request;
- use ThirdParty\Urs\Util\CryptoService;
- use Illuminate\Support\Facades\Log;
- /**
- * URS登录请求类
- *
- * 专门处理手机号+密码登录请求,遵循"一个Request类只完成一种请求"的原则
- * 根据手机号和密码获取用户ID和用户密钥
- */
- class UrsLoginRequest extends BaseRequest
- {
- /**
- * 处理登录请求
- *
- * @param array $params 请求参数,包含mobile和password字段
- * @return array 返回包含userId和userKey的数组
- * @throws \Exception
- */
- protected function handler(array $params = []): array
- {
- Log::info('URS登录请求开始', [
- 'params_keys' => array_keys($params),
- 'mobile' => $params['mobile'] ?? 'not_provided',
- 'has_password' => !empty($params['password'])
- ]);
- // 验证必需参数
- if (empty($params['mobile'])) {
- Log::error('URS登录请求参数验证失败', ['error' => 'mobile参数是必填的']);
- throw new \Exception('mobile参数是必填的');
- }
- if (empty($params['password'])) {
- Log::error('URS登录请求参数验证失败', ['error' => 'password参数是必填的']);
- throw new \Exception('password参数是必填的');
- }
- try {
- // 获取URS凭证
- $credential = $this->getUrsCredential();
- $apiUrl = $this->getService()->base_url; // 直接使用服务的base_url
- $appKey = $credential->getApiKey();
- $ecologyId = $credential->getEcologyId();
- Log::info('URS配置获取成功', [
- 'api_url' => $apiUrl,
- 'app_key_length' => strlen($appKey),
- 'ecology_id' => $ecologyId
- ]);
- if (empty($apiUrl) || empty($appKey)) {
- Log::error('URS配置验证失败', [
- 'api_url_empty' => empty($apiUrl),
- 'app_key_empty' => empty($appKey),
- 'api_url' => $apiUrl,
- 'app_key_length' => strlen($appKey)
- ]);
- throw new \Exception('URS配置不完整,缺少api_url或app_key');
- }
- // 准备请求数据
- $requestData = [
- 'mobile' => $params['mobile'],
- 'password' => $params['password']
- ];
- Log::info('请求数据准备完成', [
- 'mobile' => $requestData['mobile'],
- 'has_password' => !empty($requestData['password'])
- ]);
- // 使用URS加密服务加密请求数据
- $cryptoService = new CryptoService($appKey);
- $encryptedData = $cryptoService->encrypt($requestData);
- Log::info('请求数据加密完成', [
- 'encrypted_data_keys' => array_keys($encryptedData)
- ]);
- // 构建完整的API URL
- $fullUrl = rtrim($apiUrl, '/') . "/api/ecology/{$ecologyId}/login";
- Log::info('准备发送HTTP请求', [
- 'full_url' => $fullUrl
- ]);
- // 发送HTTP请求到URS
- $response = $this->sendHttpRequest($fullUrl, $encryptedData);
- Log::info('HTTP请求响应接收完成', [
- 'response_keys' => array_keys($response),
- 'has_encrypted_fields' => isset($response['data'], $response['iv'], $response['timestamp'], $response['sign'])
- ]);
- // 解密响应数据
- $decryptedResponse = $cryptoService->decrypt($response);
- Log::info('响应数据解密完成', [
- 'decrypted_keys' => array_keys($decryptedResponse),
- 'has_user_id' => isset($decryptedResponse['userId']),
- 'has_user_key' => isset($decryptedResponse['userKey'])
- ]);
- // 验证响应数据
- if (!isset($decryptedResponse['userId'])) {
- Log::error('URS登录响应数据验证失败', ['error' => '响应中缺少userId字段']);
- throw new \Exception('URS登录失败:响应中缺少userId字段');
- }
- if (!isset($decryptedResponse['userKey'])) {
- Log::error('URS登录响应数据验证失败', ['error' => '响应中缺少userKey字段']);
- throw new \Exception('URS登录失败:响应中缺少userKey字段');
- }
- Log::info('URS登录请求处理成功', [
- 'user_id' => $decryptedResponse['userId'],
- 'user_key_length' => strlen($decryptedResponse['userKey'])
- ]);
- return $decryptedResponse;
- } catch (\Exception $e) {
- Log::error('URS登录请求处理失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 发送HTTP请求
- *
- * @param string $url 请求URL
- * @param array $data 请求数据
- * @return array
- * @throws \Exception
- */
- protected function sendHttpRequest(string $url, array $data): array
- {
- // 记录请求开始日志
- Log::info('URS HTTP请求开始', [
- 'url' => $url,
- 'request_data_keys' => array_keys($data),
- 'request_data_size' => strlen(json_encode($data)),
- 'method' => 'POST'
- ]);
- $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);
- // 记录HTTP响应日志
- Log::info('URS HTTP请求完成', [
- 'http_code' => $httpCode,
- 'response_size' => strlen($response),
- 'has_error' => !empty($error)
- ]);
- if ($error) {
- Log::error('URS HTTP请求失败', ['curl_error' => $error]);
- throw new \Exception("HTTP请求失败: {$error}");
- }
- if ($httpCode !== 200) {
- Log::error('URS HTTP请求返回错误状态码', [
- 'http_code' => $httpCode,
- 'response' => $response
- ]);
- throw new \Exception("HTTP请求失败,状态码: {$httpCode}");
- }
- $decodedResponse = json_decode($response, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- Log::error('URS HTTP响应JSON解析失败', [
- 'json_error' => json_last_error_msg(),
- 'response' => $response
- ]);
- throw new \Exception('响应数据格式错误');
- }
- Log::info('URS HTTP请求响应解析成功', [
- 'response_keys' => array_keys($decodedResponse)
- ]);
- return $decodedResponse;
- }
- }
|