危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.gkhy.hazmat.common.utils;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.hazmat.common.config.SsasConfig;
import lombok.extern.slf4j.Slf4j;
 
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
public class AddressUtils {
 
    public static final String IP_URL="http://whois.pconline.com.cn/ipJson.jsp";
 
 
    public static String getRealAddressByIP(String ip){
        if(IpUtils.internalIp(ip)){
            return "内网IP";
        }
 
        if(SsasConfig.isAddressEnabled()){
            try {
                Map<String, Object> queryParams = new HashMap<>();
                queryParams.put("ip", ip);
                queryParams.put("json", "true");
                String rspStr = HttpUtils.sendGet(IP_URL, queryParams);
                if (StringUtils.isBlank(rspStr)) {
                    log.error("获取地理位置异常 {}", ip);
                    return "XX XX";
                }
                JSONObject jsonObject = JSON.parseObject(rspStr);
                String region = jsonObject.getString("pro");
                String city = jsonObject.getString("city");
                return String.format("%s %s", region, city);
            }catch (Exception e){
                log.error("获取地理位置异常 {}", ip);
            }
        }
        return "XX XX";
    }
}