Browse Source

优化天气服务返回信息

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

28
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()); JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) { if (jsonResult.getInteger("code") == 200) {
jsonResult.put("name",dcRegion.getRegionName()); jsonObject.put(weather.getString("METEOROLOGICALEARLYWARNING") + dcRegion.getId(), extracted(jsonResult, "warning", dcRegion));
jsonObject.put(weather.getString("METEOROLOGICALEARLYWARNING") + dcRegion.getId(), extracted(jsonResult,"warning"));
} else { } else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
@ -178,19 +177,17 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) { if (jsonResult.getInteger("code") == 200) {
jsonObject.put("name",dcRegion.getRegionName());
jsonResult.put("name",dcRegion.getRegionName());
if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) { 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"))) { } 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"))) { } 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 { } else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
@ -204,7 +201,7 @@ public class WeatherForecastController extends BaseController {
} }
if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) { if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) {
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES);
@ -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 jsonObject = new JSONObject();
jsonObject.put("code", jsonResult.getString("code")); jsonObject.put("code", jsonResult.getString("code"));
if (Objects.equals(jsonResult.getString("code"), "200")) { if (Objects.equals(jsonResult.getString("code"), "200")) {
if (Objects.equals(type, "now")) { if (Objects.equals(type, "now")) {
return jsonResult.getJSONObject(type);
JSONObject object = jsonResult.getJSONObject(type);
object.put("name", dcRegion.getRegionName());
return object;
} else { } 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")) { } else if (Objects.equals(jsonResult.getString("code"), "500")) {
jsonObject.put("msg", "请求失败:无响应或超时"); jsonObject.put("msg", "请求失败:无响应或超时");
@ -272,7 +276,7 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) { if (jsonResult.getInteger("code") == 200) {
jsonObject.put("weatherInTheAreaWhereTheIncidentOccurred",jsonResult); jsonObject.put("weatherInTheAreaWhereTheIncidentOccurred", jsonResult);
} else { } else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
} }

Loading…
Cancel
Save