Sfoglia il codice sorgente

优化:易客云天气API次数限制

lvy 3 mesi fa
parent
commit
3878db354a

+ 30 - 0
blade-common/src/main/java/org/springblade/common/utils/YiKeYunApiUtils.java

@@ -43,6 +43,7 @@ public class YiKeYunApiUtils {
             map.put("high", jsonData.get("tem_day").toString());
             map.put("low", jsonData.get("tem_night").toString());
             map.put("windLevel", jsonData.get("win_speed").toString());
+            map.put("nums", jsonData.getInteger("nums").toString());
         } catch (IOException e) {
             e.printStackTrace();
             return null;
@@ -98,6 +99,9 @@ public class YiKeYunApiUtils {
             }
             log.info("获取当天天气 ======= sb.toString():" + sb);
             JSONObject jsonData = JSONObject.parseObject(sb.toString());
+            map.put("nums", new HashMap<String, String>(){{
+                put("nums", jsonData.getInteger("nums").toString());
+            }});
             JSONArray list = jsonData.getJSONArray("list");
 
             list.forEach(item -> {
@@ -122,5 +126,31 @@ public class YiKeYunApiUtils {
         return map;
     }
 
+    /**
+     * 易客 国内LBS逆地理编码服务 (区县级) 每日免费500次
+     * 根据经纬度坐标解析地址详情
+     */
+    public static Map<String, String> getPosition(String LatitudeAndLongitude) throws IOException {
+        String getUrl = String.format("http://apia.yikeapi.com/geocode?appid=%s&appsecret=%s&output=json&location=", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, LatitudeAndLongitude);
+        URL url = new URL(getUrl);
+        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
+        String res;
+        StringBuilder sb = new StringBuilder("");
+        while ((res = in.readLine()) != null) {
+            sb.append(res.trim());
+        }
+        log.info("获取经纬度坐标地址 ======= sb.toString():" + sb);
+        Map<String, String> map = new HashMap<>();
+        JSONObject jsonData = JSONObject.parseObject(sb.toString());
+        map.put("nums", jsonData.get("nums").toString());
+        JSONObject regeocode = (JSONObject) jsonData.get("regeocode");
+        JSONObject addressComponent = (JSONObject) regeocode.get("addressComponent");
+        map.put("province", addressComponent.get("province").toString());
+        map.put("city", addressComponent.get("city").toString());
+        map.put("district", addressComponent.get("district").toString());
+        map.put("adcode", addressComponent.get("adcode").toString());
+        return map;
+    }
+
 
 }

+ 25 - 2
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/WeatherInfoServiceImpl.java

@@ -26,6 +26,8 @@ import org.springblade.manager.feign.ProjectClient;
 import org.springblade.manager.feign.ProjectContractAreaClient;
 import org.springblade.manager.vo.ContractInfoVO;
 import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.ResultSetExtractor;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -54,6 +56,8 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
 
     private final JdbcTemplate jdbcTemplate;
 
+    private final StringRedisTemplate stringRedisTemplate;
+
     /**
      * 根据所选年份获取当年所有天气台账
      */
@@ -138,11 +142,19 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
                 Map<String, String> weatherMap = cachedWeatherMap.get(area.getCity_code());
                 if (weatherMap == null) {
                     //获取天气信息(百度天气)
-                    weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcode(area.getCity_code() + "000000");
+                    String nums = stringRedisTemplate.opsForValue().get("blade-business::contractArea:yiKeYun:todayWeatherNums");
+                    if (nums != null && Integer.parseInt(nums) <= 19000) {
+                        weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcode(area.getCity_code() + "000000");
+                    }
                     if (weatherMap == null) {
                         weatherMap = BaiduApiUtil.getTodayWeather(area.getCity_code());
                     }
-                    cachedWeatherMap.put(area.getCity_code(), weatherMap);
+                    if (weatherMap != null) {
+                        cachedWeatherMap.put(area.getCity_code(), weatherMap);
+                        if (weatherMap.get("nums") != null) {
+                            stringRedisTemplate.opsForValue().set("blade-business::contractArea:yiKeYun:todayWeatherNums", weatherMap.get("nums"));
+                        }
+                    }
                 }
                 if (weatherMap != null) {
                     //计算平均气温
@@ -406,6 +418,11 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
     }
 
     public Map<String, Map<String, String>> getHistoryWeather(ProjectContractArea area, String month) {
+        String nums = stringRedisTemplate.opsForValue().get("blade-business::contractArea:yiKeYun:historyWeatherNums");
+        if (nums == null || Integer.parseInt(nums) > 19000) {
+            log.info("获取历史天气失败:易客云获取历史天气的api次数已用完。");
+            return null;
+        }
         String county = area.getCounty();
         if (county.length() > 2) {
             county = county.substring(0, county.length() - 1);
@@ -425,6 +442,12 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
         }
         if (cityId != null) {
             Map<String, Map<String, String>> historyWeather = YiKeYunApiUtils.getHistoryWeather(cityId, Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)));
+            if (historyWeather != null) {
+                Map<String, String> map = historyWeather.get("nums");
+                if (map != null && map.get("nums") != null) {
+                    stringRedisTemplate.opsForValue().set("blade-business::contractArea:yiKeYun:historyWeatherNums", map.get("nums"));
+                }
+            }
             return historyWeather;
         } else {
             log.info("获取历史天气失败:" + area.getCity()+"-"+area.getCounty());

+ 19 - 18
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/ContractInfoServiceImpl.java

@@ -1059,19 +1059,23 @@ public class ContractInfoServiceImpl extends BaseServiceImpl<ContractInfoMapper,
                 });
             }
             String projectPlace = contractInfo.getProjectPlace();
+            Map<String, String> position = new HashMap<>();
             try {
                 Map<String, Object> addressInfo = BaiduApiUtil.geocoding(projectPlace);
-                Map<String, String> position = BaiduApiUtil.getPosition(addressInfo.get("lat").toString()
+                position = BaiduApiUtil.getPosition(addressInfo.get("lat").toString()
                         + "," + addressInfo.get("lng").toString());
-                ProjectContractArea projectContractArea = new ProjectContractArea();
-                projectContractArea.setProvince(position.get("province"));
-                projectContractArea.setCity(position.get("city"));
-                projectContractArea.setCounty(position.get("district"));
-                projectContractArea.setCity_code(position.get("adcode"));
-                projectContractArea.setProjectId(contractInfo.getPId());
-                projectContractArea.setIsDeleted(0);
-                projectContractArea.setContractId(String.valueOf(contractInfo.getId()));
-                projectContractArea.setProjectId(contractInfo.getPId());
+            } catch (Exception e) {
+                throw new ServiceException("施工台账初始化失败: 百度地理编码API配额超限,今日限制访问");
+            }
+            ProjectContractArea projectContractArea = new ProjectContractArea();
+            projectContractArea.setProvince(position.get("province"));
+            projectContractArea.setCity(position.get("city"));
+            projectContractArea.setCounty(position.get("district"));
+            projectContractArea.setCity_code(position.get("adcode"));
+            projectContractArea.setProjectId(contractInfo.getPId());
+            projectContractArea.setIsDeleted(0);
+            projectContractArea.setContractId(String.valueOf(contractInfo.getId()));
+            projectContractArea.setProjectId(contractInfo.getPId());
 //                QueryWrapper<ProjectContractArea> queryWrapper = new QueryWrapper<>();
 //                queryWrapper.eq("project_id", contractInfo.getPId());
 //                queryWrapper.eq("contract_id", contractInfo.getId());
@@ -1085,15 +1089,12 @@ public class ContractInfoServiceImpl extends BaseServiceImpl<ContractInfoMapper,
 //                    int res = projectContractAreaMapper.insert(projectContractArea);
 //                    return res > 0;
 //                }
-                //删除之前所有的地址,再新增
-                projectContractAreaMapper.delete(new LambdaQueryWrapper<ProjectContractArea>()
-                        .eq(ProjectContractArea::getContractId, projectContractArea.getContractId()));
-                int res = projectContractAreaMapper.insert(projectContractArea);
-                return res > 0;
+            //删除之前所有的地址,再新增
+            projectContractAreaMapper.delete(new LambdaQueryWrapper<ProjectContractArea>()
+                    .eq(ProjectContractArea::getContractId, projectContractArea.getContractId()));
+            int res = projectContractAreaMapper.insert(projectContractArea);
+            return res > 0;
 //                return true;
-            } catch (IOException e) {
-                throw new ServiceException("施工台账初始化失败");
-            }
         }
         return false;
     }