@ApiOperation(value = "自助机-获取天气信息")
@GetMapping("/weather")
public CommonResult getWeather(@ApiParam(name = "publicIp", value = "外网ip", required = true)@RequestParam(value = "publicIp") String publicIp){
log.info("publicIp-----------------请求外网ip"+publicIp);
String cityInfo = IPUtils.getCityInfo(publicIp);//国家|区域|省份|城市|ISP
if (StringUtils.isBlank(cityInfo)){
return CommonResult.failed("获取失败");
}
String[] split = cityInfo.split("\\|");
String city = "";
for (String aSplit : split) {
if (aSplit.contains("市")){
city = aSplit;//拿取市级名称
}
}
if (StringUtils.isBlank(city)) {
return CommonResult.failed("获取失败");
}
String weatherInformation = HttpUtil.get("http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather?cityName=" + city);//调用天气接口
if (StringUtils.isBlank(weatherInformation)){
return CommonResult.failed("获取失败");
}
Weather weather= JSONUtil.toBean(weatherInformation, Weather.class);
WeatherDto weatherDto = weather.getResults().get(0);
List<DailyDto> daily = weatherDto.getDaily();
List<SsmWeather> ssmWeathers=machineHomePageService.querySsmWeather();
Map<String, String> map = ssmWeathers.stream().collect(Collectors.toMap(SsmWeather::getRemarks, SsmWeather::getIcon));
for (DailyDto dailyDto : daily) {
if(map.containsKey(dailyDto.getText_day())){
dailyDto.setText_day_icon(map.get(dailyDto.getText_day()));
}else{
dailyDto.setText_day_icon(map.get("未知"));
}
if(map.containsKey(dailyDto.getText_night())){
dailyDto.setText_night_icon(map.get(dailyDto.getText_night()));
}else{
dailyDto.setText_night_icon(map.get("未知"));
}
}
return CommonResult.success(weatherDto);
}
public static String getCityInfo(String ip) {
String dbPath = createFtlFileByFtlArray() + "ip2region.db";
File file = new File(dbPath);
if (file.exists() == false) {
System.out.println("Error: Invalid ip2region.db file");
}
//查询算法
//B-tree, B树搜索(更快)
int algorithm = DbSearcher.BTREE_ALGORITHM;
//Binary,使用二分搜索
//DbSearcher.BINARY_ALGORITHM
//Memory,加载内存(最快)
//DbSearcher.MEMORY_ALGORITYM
try {
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, dbPath);
//define the method
Method method = null;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
default:
System.out.println("default");
break;
}
DataBlock dataBlock = null;
if (Util.isIpAddress(ip) == false) {
System.out.println("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
String ipInfo = dataBlock.getRegion();
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建ip2region文件
* @return
*/
public static String createFtlFileByFtlArray() {
String ftlPath = "db/";
return createFtlFile(ftlPath, "ip2region.db");
}
/**
* 创建文件
* @param ftlPath
* @param ftlName
* @return
*/
private static String createFtlFile(String ftlPath, String ftlName) {
InputStream certStream = null;
try {
//获取当前项目所在的绝对路径
String proFilePath = System.getProperty("user.dir");
//获取模板下的路径,然后存放在temp目录下
String newFilePath = proFilePath + File.separator + "temp" + File.separator + ftlPath;
newFilePath = newFilePath.replace("/", File.separator);
//检查项目运行时的src下的对应路径
File newFile = new File(newFilePath + ftlName);
if (newFile.isFile() && newFile.exists()) {
return newFilePath;
}
//当项目打成jar包会运行下面的代码,并且复制一份到src路径下(具体结构看下面图片)
certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ftlPath + ftlName);
byte[] certData = org.apache.commons.io.IOUtils.toByteArray(certStream);
org.apache.commons.io.FileUtils.writeByteArrayToFile(newFile, certData);
return newFilePath;
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
certStream.close();
} catch (Exception e) {
log.error(e.getMessage());
}
}
return null;
}
package com.crm.core.dto;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* @author c-tangck
* @description 天气预报图标
*/
@Data
@ApiModel(value = "天气预报图标")
public class SsmWeather {
private String remarks;
private String icon;
}
package com.crm.ssm.dto;
import io.swagger.annotations.ApiModel;
import lombok.Data;
/**
* @author c-tangck
* @description 天气预报-天气
*/
@Data
@ApiModel(value = "天气预报-天气")
public class DailyDto {
private String date;
private String text_day;
private String text_day_icon;
private String code_day;
private String text_night;
private String text_night_icon;
private String code_night;
private String high;
private String low;
private String rainfall;
private String precip;
private String wind_direction;
private String wind_direction_degree;
private String wind_speed;
private String wind_scale;
private String humidity;
}
下载开源ip2region.db文件到项目resources目录下
|