com.taotao.boot.ip2region.http.PlatformBaiduService Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.ip2region.http;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
import java.util.Map;
/**
* 百度地图开发平台实现类
*/
public class PlatformBaiduService implements IPlatFormIpAnalyzeService {
/**
* 响应成功状态码
*/
private final Integer OK_CODE = 0;
@Value("${platform.conf.baidu.ak}")
public String ak;
@Override
public IpAnalyzeResponse ipAnalyze(String ip) {
String URL = "http://api.map.baidu.com/location/ip";
Map params = new HashMap<>();
params.put("ip", ip);
params.put("ak", ak);
params.put("coor", "bd09ll");
try {
String json = SmartHttpUtil.sendGet(URL, params, null);
IpAnalyzeBaiduResponse response = JSONUtil.toBean(json, IpAnalyzeBaiduResponse.class);
if (OK_CODE.equals(response.getStatus())) {
return IpAnalyzeResponse.builder()
.status(response.getStatus() + "")
.successFlag(true)
.msg(response.getMessage())
.province(response.getContent().getAddressDetail().getProvince())
.city(response.getContent().getAddressDetail().getCity())
.x(Double.parseDouble(response.getContent().getPoint().getX()))
.y(Double.parseDouble(response.getContent().getPoint().getY()))
.build();
}
return IpAnalyzeResponse.builder()
.status(response.getStatus() + "")
.successFlag(false)
.msg(response.getMessage())
.build();
} catch (Exception e) {
return IpAnalyzeResponse.builder()
.status("500")
.successFlag(false)
.msg(e.getMessage())
.build();
}
}
}