Browse Source

优化天气服务返回信息

develop
zhaoxianglong 7 months ago
parent
commit
33aac8c36b
  1. 24
      zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

24
zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

@ -104,8 +104,7 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) {
jsonResult.put("name",dcRegion.getRegionName());
jsonObject.put(weather.getString("METEOROLOGICALEARLYWARNING") + dcRegion.getId(), extracted(jsonResult,"warning"));
jsonObject.put(weather.getString("METEOROLOGICALEARLYWARNING") + dcRegion.getId(), extracted(jsonResult, "warning", dcRegion));
} else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
@ -178,19 +177,17 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) {
jsonObject.put("name",dcRegion.getRegionName());
jsonResult.put("name",dcRegion.getRegionName());
if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now"));
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now", dcRegion));
} else if (Objects.equals(redisKey, weather.getString("METEOROLOGICALEARLYWARNING"))) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning"));
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning", dcRegion));
} else if (Objects.equals(redisKey, weather.getString("HOURLYWEATHER"))) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult,"hourly"));
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "hourly", dcRegion));
}
} else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
@ -221,14 +218,21 @@ public class WeatherForecastController extends BaseController {
}
}
private static Object extracted(JSONObject jsonResult, String type) {
private static Object extracted(JSONObject jsonResult, String type, DcRegion dcRegion) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", jsonResult.getString("code"));
if (Objects.equals(jsonResult.getString("code"), "200")) {
if (Objects.equals(type, "now")) {
return jsonResult.getJSONObject(type);
JSONObject object = jsonResult.getJSONObject(type);
object.put("name", dcRegion.getRegionName());
return object;
} else {
return jsonResult.getJSONArray(type);
JSONArray jsonArray = jsonResult.getJSONArray(type);
jsonArray.forEach(item -> {
((JSONObject) item).put("name", dcRegion.getRegionName());
});
return jsonArray;
}
} else if (Objects.equals(jsonResult.getString("code"), "500")) {
jsonObject.put("msg", "请求失败:无响应或超时");

Loading…
Cancel
Save