Browse Source
# Conflicts: # im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java # im-uniapp/pages/login/login.vuemaster
6 changed files with 229 additions and 13 deletions
@ -0,0 +1,75 @@ |
|||||
|
package com.bx.implatform.util; |
||||
|
|
||||
|
import cn.hutool.http.HttpUtil; |
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* IP 地址查询工具类 |
||||
|
* 使用 ipify 和 httpbin 获取 IP 地址的地理位置信息 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
public class IpUtils { |
||||
|
|
||||
|
private static final String IP_API = "http://ip-api.com/json/"; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 根据 IP 地址查询地理位置 |
||||
|
* |
||||
|
* @param ip IP 地址 |
||||
|
* @return 地理位置信息,格式如:"中国广东省深圳市" |
||||
|
*/ |
||||
|
public static String getIpAddress(String ip) { |
||||
|
System.out.println("333333333"); |
||||
|
System.out.println(ip); |
||||
|
|
||||
|
|
||||
|
if (ip == null || ip.trim().isEmpty() || "unknown".equalsIgnoreCase(ip)) { |
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
// 使用 ip-api.com 查询 IP 归属地
|
||||
|
String url = IP_API + ip; |
||||
|
String response = HttpUtil.get(url, 5000); |
||||
|
|
||||
|
System.out.println("2222222222222222"); |
||||
|
System.out.println( response); |
||||
|
|
||||
|
if (response != null && !response.isEmpty()) { |
||||
|
JSONObject json = JSON.parseObject(response); |
||||
|
String status = json.getString("status"); |
||||
|
|
||||
|
if ("success".equals(status)) { |
||||
|
String country = json.getString("country"); |
||||
|
String regionName = json.getString("regionName"); |
||||
|
String city = json.getString("city"); |
||||
|
|
||||
|
StringBuilder address = new StringBuilder(); |
||||
|
if (country != null && !country.isEmpty()) { |
||||
|
address.append(country); |
||||
|
} |
||||
|
if (regionName != null && !regionName.isEmpty() && !regionName.equals(country)) { |
||||
|
address.append(regionName); |
||||
|
} |
||||
|
if (city != null && !city.isEmpty() && !city.equals(regionName)) { |
||||
|
address.append(city); |
||||
|
} |
||||
|
|
||||
|
log.info("IP 地址查询成功:ip={}, address={}", ip, address); |
||||
|
return address.toString(); |
||||
|
} |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
log.error("IP 地址查询失败:ip={}, error={}", ip, e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
// 如果查询失败,返回空字符串
|
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue