|
@@ -1031,7 +1031,8 @@ class PublicRpcService implements PublicRpcServiceInterface
|
|
|
/** @var \GuzzleHttp\Client $client */
|
|
|
$client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
|
|
|
|
|
|
- $api_url = "http://ip-api.com/json/{$client_ip}?lang=zh-cn";
|
|
|
+ // 使用新的 GeoJS API 地址
|
|
|
+ $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
|
|
|
// 将 timeout 作为请求选项传递
|
|
|
$response = $client->get($api_url, ['timeout' => 5]);
|
|
|
|
|
@@ -1040,14 +1041,17 @@ class PublicRpcService implements PublicRpcServiceInterface
|
|
|
}
|
|
|
|
|
|
$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'];
|
|
|
+
|
|
|
+ // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
|
|
|
+ if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
|
|
|
+ // GeoJS 返回的经纬度是字符串,转换为浮点数
|
|
|
+ $ip_info['latitude'] = floatval($ip_info['latitude']);
|
|
|
+ $ip_info['longitude'] = floatval($ip_info['longitude']);
|
|
|
return Result::success($ip_info);
|
|
|
}
|
|
|
|
|
|
- $errorMessage = $ip_info['message'] ?? '获取IP信息失败';
|
|
|
+ // 如果返回的数据不符合预期,提供一个通用错误
|
|
|
+ $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
|
|
|
return Result::error($errorMessage);
|
|
|
|
|
|
} catch (GuzzleException $e) {
|