rkljw 2 天之前
父節點
當前提交
410e2a2788
共有 2 個文件被更改,包括 35 次插入17 次删除
  1. 34 17
      app/JsonRpc/PublicRpcService.php
  2. 1 0
      composer.json

+ 34 - 17
app/JsonRpc/PublicRpcService.php

@@ -32,12 +32,16 @@ use function Hyperf\Support\retry;
 use Hyperf\Paginator\Paginator;
 use App\Model\User;
 use App\Model\Website;
+use App\Constants\ErrorCode;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
 
 #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class PublicRpcService implements PublicRpcServiceInterface
 {
     #[Inject]
     protected Redis $redis;
+
     /**
      * @param array $data
      * @return array
@@ -1016,28 +1020,41 @@ class PublicRpcService implements PublicRpcServiceInterface
      */
     public function getIpInfo(array $data) :array
     {
+        if (empty($data['ip'])) {
+            // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
+            return Result::error('IP地址不能为空!');
+        }
+        $client_ip = $data['ip'];
+
         try {
-            $client_ip = isset($data['ip']) && $data['ip']??$_SERVER['REMOTE_ADDR'];
-            // 使用 IPinfo 服务获取 IP 信息
-//        $api_url = "https://ipinfo.io/{$client_ip}/json";
-            //http://ip-api.com/json/117.136.12.79?lang=zh-cn
-            $api_url = "http://ip-api.com/json/{$client_ip}/?lang=zh-cn";
-            $ch = curl_init($api_url);
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-            $response = curl_exec($ch);
-            curl_close($ch);
-            // 解析 JSON 响应
-            $ip_info = json_decode($response, true);
-            // 提取地址和经纬度i
-            if ($ip_info) {
-//            $latitude = explode(',', $ip_info['loc'])[0];
-//            $longitude = explode(',', $ip_info['loc'])[1];
+            // 直接从容器获取 Guzzle 客户端实例
+            /** @var \GuzzleHttp\Client $client */
+            $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
+
+            $api_url = "http://ip-api.com/json/{$client_ip}?lang=zh-cn";
+            // 将 timeout 作为请求选项传递
+            $response = $client->get($api_url, ['timeout' => 5]);
+
+            if ($response->getStatusCode() !== 200) {
+                return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
+            }
+
+            $ip_info = json_decode($response->getBody()->getContents(), true);
+            var_dump($ip_info, '-');
+            if ($ip_info && ($ip_info['status'] ?? 'fail') === 'success') {
                 $ip_info['latitude'] = $ip_info['lat'];
                 $ip_info['longitude'] = $ip_info['lon'];
                 return Result::success($ip_info);
             }
-        }catch (\Exception $e){
-            return Result::error('获取失败!');
+
+            $errorMessage = $ip_info['message'] ?? '获取IP信息失败';
+            return Result::error($errorMessage);
+
+        } catch (GuzzleException $e) {
+            // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
+            return Result::error('请求IP查询接口异常: ' . $e->getMessage());
+        } catch (\Throwable $e) {
+            return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
         }
     }
 

+ 1 - 0
composer.json

@@ -20,6 +20,7 @@
         "hyperf/config": "~3.1.0",
         "hyperf/config-nacos": "^3.1",
         "hyperf/constants": "^3.1",
+        "hyperf/coroutine": "^3.1",
         "hyperf/database": "~3.1.0",
         "hyperf/db-connection": "~3.1.0",
         "hyperf/di": "^3.1",