提交 00dfe046 authored 作者: 吴德鹏's avatar 吴德鹏

国家管理

上级 70f7dc62
......@@ -50,18 +50,23 @@ public class CountryController extends Controller {
.filter(c -> StringUtils.isNotBlank(c.getNationalFlag()))
.collect(Collectors.toList());
//获取客户端ip
String ip = this.getRealIpAddress(request);
String countryName = HttpsUtil.useIpFindCountry(ip);
logger.info("ip:" + ip);
logger.info("countryName:" + countryName);
//根据ip查询国家
List<Country> list = countryRepository.findAllByCountryName(countryName);
//获取默认的国家
Optional<Country> defaultCountry = list.stream().findFirst();
params.put("countries", collect);
params.put("defaultCountry", defaultCountry.orElse(collect.get(0)));
if (StringUtils.isBlank(country)) {
//获取客户端ip
String ip = this.getRealIpAddress(request);
String countryName = HttpsUtil.useIpFindCountryEn(ip);
logger.info("ip:" + ip);
logger.info("countryName:" + countryName);
//根据ip查询国家
List<Country> list = countryRepository.findAllByCountryName(countryName);
//获取默认的国家
Optional<Country> defaultCountry = list.stream().findFirst();
Optional<Country> first = countries.stream().findFirst();
params.put("defaultCountry", defaultCountry.orElseGet(() -> first.get()));
}
return new Result(params);
}
}
......@@ -14,6 +14,6 @@ import java.util.List;
public interface CountryRepository extends JpaRepository<Country, String> {
// @Query(value = "select c from #{#entityName} c where c.countryName=:country order by c.sort,c.countryName")
@Query(value = "select c.id,c.country_name,c.country_cname,c.area_code,c.national_flag from country c where if(?1!='',c.country_name like concat('%',?1,'%') or c.country_cname like concat('%',?1,'%'),1=1) order by c.sort asc ,c.country_name asc", nativeQuery = true)
@Query(value = "select c.id,c.country_name,c.country_cname,c.area_code,c.national_flag from country c where if(?1!='',c.country_name like ?1% or c.country_cname like ?1%,1=1) order by c.sort asc ,c.country_name asc", nativeQuery = true)
List<Country> findAllByCountryName(String country);
}
......@@ -319,4 +319,21 @@ public class HttpsUtil {
}
return null;
}
/**
* http://ip-api.com/json/49.65.247.32
*/
public static String useIpFindCountryEn(String ip) throws IOException {
String url = "http://ip-api.com/json/" + ip;
Map<String, Object> params = new HashMap();
String result = HttpClientUtil.createConnection(url, params, "utf-8");
JSONObject jsonObject = JSONObject.parseObject(result);
String status = jsonObject.getString("status");
if ("success".equals(status)) {
String country = jsonObject.getString("country");
System.out.println("country:" + country);
return country;
}
return null;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论