diff --git a/business-service-dao/src/main/java/com/ho/business/service/impl/StationServiceImpl.java b/business-service-dao/src/main/java/com/ho/business/service/impl/StationServiceImpl.java index 52663fe..671664d 100644 --- a/business-service-dao/src/main/java/com/ho/business/service/impl/StationServiceImpl.java +++ b/business-service-dao/src/main/java/com/ho/business/service/impl/StationServiceImpl.java @@ -310,9 +310,9 @@ public class StationServiceImpl implements StationService { * @param station */ private void updateStationInfo(Station station) { - MyAddress address = AddressUntils.getAddress(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude())); + MyAddress address = AbroadAddressUtils.getAddress(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude())); if (null != address) { - String adCode = address.getAdcode(); + String adCode = address.getCityCode(); station.setAdCode(adCode); station.setDistrict(address.getDistrict()); station.setCity(address.getCity()); @@ -320,7 +320,7 @@ public class StationServiceImpl implements StationService { station.setNation(address.getNation()); String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode; if (!redisService.hasKey(key)) { - WeatherRespVo weatherRespVo = WeatherUntils.getWeatherRespVo(station.getAdCode()); + WeatherRespVo weatherRespVo = AbroadWeatherUtils.getWeatherRespVo(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude())); if (weatherRespVo != null) { weatherRespVo.setUpdateTime(System.currentTimeMillis()); redisService.set(key, weatherRespVo); diff --git a/common-tools/pom.xml b/common-tools/pom.xml index 458fc34..a0ee4b5 100644 --- a/common-tools/pom.xml +++ b/common-tools/pom.xml @@ -46,6 +46,11 @@ com.alibaba fastjson + + org.json + json + 20231013 + com.fasterxml.jackson.core diff --git a/common-tools/src/main/java/com/ho/common/tools/entity/WeatherRespVo.java b/common-tools/src/main/java/com/ho/common/tools/entity/WeatherRespVo.java index 859ab4f..c08a9db 100644 --- a/common-tools/src/main/java/com/ho/common/tools/entity/WeatherRespVo.java +++ b/common-tools/src/main/java/com/ho/common/tools/entity/WeatherRespVo.java @@ -25,6 +25,9 @@ public class WeatherRespVo { @ApiModelProperty(value = "天气现象") String skyCon; + @ApiModelProperty(value = "天气现象编码") + int code; + @ApiModelProperty(value = "风向风速") String speedAndDirection; diff --git a/common-tools/src/main/java/com/ho/common/tools/util/AbroadAddressUtils.java b/common-tools/src/main/java/com/ho/common/tools/util/AbroadAddressUtils.java new file mode 100644 index 0000000..0fdcd3a --- /dev/null +++ b/common-tools/src/main/java/com/ho/common/tools/util/AbroadAddressUtils.java @@ -0,0 +1,128 @@ +package com.ho.common.tools.util; + +import com.ho.common.tools.entity.MyAddress; +import lombok.extern.slf4j.Slf4j; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.*; + +/** + * @author kerwin + * + * 海外地址反向解析 + * + */ +@Slf4j +public class AbroadAddressUtils { + + static String apiKey = "AIzaSyAM4JOtE9yt48f60ATwByB7uWzTIrYSSUg"; + + public static MyAddress getAddress(String lat, String lng){ + // 构建API请求URL + String urlString = "https://maps.googleapis.com/maps/api/geocode/json?" + + "latlng=" + lat + "," + lng + + "&key=" + apiKey; + // 发送HTTP请求 + URL url = null; + StringBuilder response = new StringBuilder(); + try { + url = new URL(urlString); + // 创建代理(如果不需要代理,可以移除这部分) + Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 4781)); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(10000); // 10秒连接超时 + conn.setReadTimeout(10000); // 10秒读取超时 + // 检查响应状态 + int responseCode = conn.getResponseCode(); + if (responseCode != 200) { + throw new RuntimeException("HTTP请求失败: " + responseCode); + } + // 读取响应内容 + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(conn.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + } + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (ProtocolException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + + + // 解析JSON响应 + return parseGeocodeResponse(response.toString()); + } + + private static MyAddress parseGeocodeResponse(String jsonResponse) { + JSONObject json = new JSONObject(jsonResponse); + String status = json.getString("status"); + + if (!"OK".equals(status)) { + throw new RuntimeException("地理编码API错误: " + status); + } + MyAddress address = new MyAddress(); + + // 获取第一个结果(最精确的匹配) + JSONObject firstResult = json.getJSONArray("results").getJSONObject(0); + // 解析地址组件 + JSONArray components = firstResult.getJSONArray("address_components"); + for (int i = 0; i < components.length(); i++) { + JSONObject component = components.getJSONObject(i); + JSONArray types = component.getJSONArray("types"); + String longName = component.getString("long_name"); + // 国家信息 + if (types.toString().contains("country")) { + address.setNation(longName); + } + // 省份/州信息 + if (types.toString().contains("administrative_area_level_1")) { + address.setProvince(longName); + } + // 城市信息 + if (types.toString().contains("locality")) { + address.setCity(longName); + } + // 区县信息 + if (types.toString().contains("sublocality")) { + address.setDistrict(longName); + } + // 邮政编码 + if (types.toString().contains("postal_code")) { + address.setCityCode(longName); + } + } + return address; + } + + public static void main(String[] args) { + // 示例坐标 - 纽约 + String latitude = "48.877132"; + String longitude = "2.356407"; + + try { + // 获取地址信息 + MyAddress address = getAddress(latitude, longitude); + + // 打印结果 + System.out.println("============ 反向地理编码结果 ============"); + System.out.println("国家: " + address.getNation() ); + System.out.println("省份/州: " + address.getProvince()); + System.out.println("城市: " + address.getCity()); + System.out.println("地区/区县: " + address.getDistrict()); + System.out.println("邮政编码: " + address.getCityCode()); + } catch (Exception e) { + System.err.println("发生错误: " + e.getMessage()); + e.printStackTrace(); + } + } +} diff --git a/common-tools/src/main/java/com/ho/common/tools/util/AbroadWeatherUtils.java b/common-tools/src/main/java/com/ho/common/tools/util/AbroadWeatherUtils.java new file mode 100644 index 0000000..fe8fcb5 --- /dev/null +++ b/common-tools/src/main/java/com/ho/common/tools/util/AbroadWeatherUtils.java @@ -0,0 +1,91 @@ +package com.ho.common.tools.util; + +import com.ho.common.tools.entity.WeatherRespVo; +import lombok.extern.slf4j.Slf4j; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * @author kerwin + * + * 获取海外天气 + */ +@Slf4j +public class AbroadWeatherUtils { + + static String apiKey = "6358b3081a5849f09ac90138252107"; + public static void main(String[] args) { + String latitude = "40.714224"; + String longitude = "-73.961452"; + + try { + System.out.println(getWeatherRespVo(latitude, longitude)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static WeatherRespVo getWeatherRespVo(String lat, String lng) { + String urlString = "http://api.weatherapi.com/v1/forecast.json?key="+apiKey+"&q=" + lat + "," + lng +"&days=1"; + URL url = null; + StringBuilder response = new StringBuilder(); + try { + url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(conn.getInputStream()))) { + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + return parseWeatherData(response.toString()); + } + + private static WeatherRespVo parseWeatherData(String jsonResponse) { + WeatherRespVo weatherRespVo =new WeatherRespVo(); + JSONObject root = new JSONObject(jsonResponse); + + // 解析地区信息 + JSONObject location = root.getJSONObject("location"); + String area = location.getString("name"); + weatherRespVo.setCityzh(area); + + // 获取天气预报数据 + JSONObject forecast = root.getJSONObject("forecast"); + JSONArray forecastday = forecast.getJSONArray("forecastday"); + JSONObject day = forecastday.getJSONObject(0).getJSONObject("day"); + + // 解析温度数据 + double maxTemp = day.getDouble("maxtemp_c"); + weatherRespVo.setMaxTemperature(new BigDecimal(String.valueOf(maxTemp))); + double minTemp = day.getDouble("mintemp_c"); + weatherRespVo.setMinTemperature(new BigDecimal(String.valueOf(minTemp))); + String condition = day.getJSONObject("condition").getString("text"); + weatherRespVo.setSkyCon(condition); + int code = day.getJSONObject("condition").getInt("code"); + weatherRespVo.setCode(code); + double maxWindKph = day.getDouble("maxwind_kph"); + // 获取风向(从最近的小时数据中获取) + JSONArray hours = forecastday.getJSONObject(0).getJSONArray("hour"); + String windDirection = hours.getJSONObject(0).getString("wind_dir"); + // 组合风向风速信息 + String windInfo = windDirection + " " + maxWindKph + " km/h"; + weatherRespVo.setSpeedAndDirection(windInfo); + weatherRespVo.setUpdateTime(System.currentTimeMillis()); + return weatherRespVo; + } + +} diff --git a/file-center/src/main/java/com/ho/filecenter/controller/WeatherController.java b/file-center/src/main/java/com/ho/filecenter/controller/WeatherController.java index a9e1707..8bad178 100644 --- a/file-center/src/main/java/com/ho/filecenter/controller/WeatherController.java +++ b/file-center/src/main/java/com/ho/filecenter/controller/WeatherController.java @@ -12,6 +12,7 @@ import com.ho.common.tools.entity.MyAddress; import com.ho.common.tools.entity.WeatherRespVo; import com.ho.common.tools.exception.DataResult; import com.ho.common.tools.service.RedisService; +import com.ho.common.tools.util.AbroadWeatherUtils; import com.ho.common.tools.util.AddressUntils; import com.ho.common.tools.util.IPUtils; import com.ho.common.tools.util.WeatherUntils; @@ -241,7 +242,7 @@ public class WeatherController { continue; } String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode; - WeatherRespVo weatherRespVo = WeatherUntils.getWeatherRespVo(station.getAdCode()); + WeatherRespVo weatherRespVo = AbroadWeatherUtils.getWeatherRespVo(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude())); weatherRespVo.setUpdateTime(updateTime); redisService.set(key, weatherRespVo); map.put(adCode, adCode);