package com.nova.sankuai.domain.api.aliyunIP; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nova.sankuai.domain.entity.City; import com.nova.sankuai.infra.mapper.CityMapper; import com.nova.sankuai.infra.utils.HttpUtils; import com.nova.sankuai.infra.utils.IpUtil; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @Slf4j @Component public class AliyunUtil { //阿里云appkey,获取ip地理信息 // private static final String appKey = "204061314"; // private static final String appKey = "204574003"; //阿里云appSecrets,获取ip地理信息 // private static final String appSecrets = "JVkQzP2zyCBvU0frWtCG9NM8w3FbkoLm"; // private static final String appSecrets = "CBvIivU5E7AnfF1YYOjGnsle3267BkuP"; private static final String appcode = "40575e08cd5c424fb1939000f262d25f"; private static final String host = "https://zjip.market.alicloudapi.com"; private static final String path = "/lifeservice/QueryIpAddr/query"; private static final String method = "GET"; // AppKey:204574003 AppSecret:CBvIivU5E7AnfF1YYOjGnsle3267BkuP //AppCode:40575e08cd5c424fb1939000f262d25f // private static final String US_CODE = "US"; // private static final Logger logger = LoggerFactory.getLogger("loginLogger"); @Resource private CityMapper cityMapper; // static { // //初始化appKey和appSecrets // HttpClientBuilderParams httpParam = new HttpClientBuilderParams(); // httpParam.setAppKey(appKey); // httpParam.setAppSecret(appSecrets); // httpParam.setConnectionTimeout(Constants.TIME_OUT); // HttpApiClientIP.getInstance().init(); // } /** * 调用阿里云的查询ip地理信息 * * @param request * @param phone * @return */ public static ResponseInfo IPLocaltionHttpSyncTest(HttpServletRequest request, String phone) { String ipAddr = IpUtil.getIpAddr(request); /*ApiResponse response = HttpsApiClientIP.getInstance().IPLocaltionSyncMode(ipAddr); if (response == null){ log.error(phone + "阿里云IP地址获取:获取失败:{}"); return null; } AliyunResponse aliyunResponse = null; String result = null; try { result = new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING); aliyunResponse = JSON.parseObject(result, AliyunResponse.class); } catch (Exception ex) { ex.printStackTrace(); log.error(phone + "阿里云IP地址获取:ip地址为" + ipAddr + "阿里云原始返回response:" + response + ",原始返回body转换字符串:" + result + ",转换ip地址信息失败:{}", ex); return null; } if (100 != aliyunResponse.getCode()) { log.error(phone + "阿里云IP地址获取:获取失败:{}", aliyunResponse.toString()); return null; } else { return aliyunResponse.getResult(); }*/ // String host = "https://zjip.market.alicloudapi.com"; // String path = "/lifeservice/QueryIpAddr/query"; // String method = "GET"; // String appcode = appCode; Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap<>(); querys.put("ip", ipAddr); AliyunResponse aliyunResponse = null; try { HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); //获取response的body if (response == null){ log.error(phone + "阿里云IP地址获取:获取失败,ip地址为:{}", ipAddr); return null; } String result = EntityUtils.toString(response.getEntity()); aliyunResponse = JSON.parseObject(result, AliyunResponse.class); } catch (Exception ex) { ex.printStackTrace(); log.error(phone + "阿里云IP地址获取:ip地址为" + ipAddr + "转换ip地址信息失败:{}", ex); return null; } if (aliyunResponse == null || 0 != aliyunResponse.getError_code()) { log.error(phone + "阿里云IP地址获取:获取失败:{}", aliyunResponse); return null; } else { return aliyunResponse.getResult(); } } /** * 调用阿里云的查询ip地理信息 * * @param request * @return */ public static void test(HttpServletRequest request) { String ipAddr = IpUtil.getIpAddr(request); ipAddr = "111.29.72.92"; // ApiResponse response = HttpsApiClientIP.getInstance().IPLocaltionSyncMode(ipAddr); // if (response == null){ // log.error("阿里云IP地址获取:获取失败:{}"); // return; // } // String result = new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING); // log.info("response:{}", result); // String host = "https://zjip.market.alicloudapi.com"; // String path = "/lifeservice/QueryIpAddr/query"; // String method = "GET"; // String appcode = appCode; Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap<>(); querys.put("ip", ipAddr); AliyunResponse aliyunResponse = null; try { HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); //获取response的body if (response == null){ log.error("阿里云IP地址获取:获取失败:{}"); } String result = EntityUtils.toString(response.getEntity()); aliyunResponse = JSON.parseObject(result, AliyunResponse.class); } catch (Exception ex) { ex.printStackTrace(); log.error("阿里云IP地址获取:ip地址为" + ipAddr + "转换ip地址信息失败:{}", ex); } if (aliyunResponse == null || 0 != aliyunResponse.getError_code()) { log.error("阿里云IP地址获取:获取失败:{}", aliyunResponse); } else { System.out.println("阿里云IP地址获取:获得城市信息为:" + aliyunResponse.getResult()); } } /** * 通过阿里云的ip地理信息,获取城市信息 * * @param request * @param phone * @return */ public City getCityInfo(HttpServletRequest request, String phone) { ResponseInfo responseInfo = AliyunUtil.IPLocaltionHttpSyncTest(request, phone); if (responseInfo == null) { log.info(phone + " 阿里云IP地址获取:获得城市信息失败"); return null; } else { log.info(phone + " 阿里云IP地址获取:获得城市信息为:{}", responseInfo); City cityInfo = cityMapper.selectOne(new LambdaQueryWrapper(). likeRight(City::getName, responseInfo.getCity()).last(" limit 1 ")); if (cityInfo != null) { log.info(phone + " 阿里云IP地址获取:获得城市信息为:{}", cityInfo); return cityInfo; } log.info(phone + " 阿里云IP地址获取:未获取到城市信息为:{}", cityInfo); return null; } } /** * 获取用户的ip及地理信息 * * @param request * @param phone * @return */ public CustomerIpInfo getCustomerIpInfo(HttpServletRequest request, String phone) { String ipAddr = IpUtil.getIpAddr(request); ResponseInfo responseInfo = AliyunUtil.IPLocaltionHttpSyncTest(request, phone); CustomerIpInfo customerIpInfo = new CustomerIpInfo(); if (ipAddr != null) { customerIpInfo.setIp(ipAddr); } if (responseInfo != null) { // customerIpInfo.setCityName(responseInfo.getCity()); City cityInfo = cityMapper.selectOne(new LambdaQueryWrapper(). likeRight(City::getName, responseInfo.getCity()).last(" limit 1 ")); if (cityInfo != null) { log.info(phone + "阿里云IP地址获取:获得城市信息为:{}", cityInfo); customerIpInfo.setSysCityId(cityInfo.getCityId()); customerIpInfo.setCityName(cityInfo.getName()); } else { customerIpInfo.setCityName(responseInfo.getCity()); } } else { log.error(phone + "阿里云IP地址获取:未获得城市信息"); } return customerIpInfo; } /** * 判断ip所属地是否未US */ public boolean checkCounty(String ip) { Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap<>(); querys.put("ip", ip); AliyunResponse aliyunResponse = null; try { HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); //获取response的body if (response == null){ log.error("阿里云IP地址获取:转换ip地址信息失败"); return false; } String result = EntityUtils.toString(response.getEntity()); aliyunResponse = JSON.parseObject(result, AliyunResponse.class); } catch (Exception ex) { ex.printStackTrace(); log.error("阿里云IP地址获取:转换ip地址信息失败:{}", ex); return false; } if (0 != aliyunResponse.getError_code()) { log.error("阿里云IP地址获取:获取失败:{}", aliyunResponse.toString()); return false; } else { ResponseInfo responseInfo = aliyunResponse.getResult(); if (responseInfo == null) { log.error("阿里云IP地址获取:获取地理信息失败:{}", aliyunResponse); return false; } else { log.info("阿里云IP地址获取:获得信息为:{}", responseInfo); return true; } } } }