dcs 7 months ago
parent
commit
7805fecf65

+ 13 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/controller/ProjectController.java

@@ -4,8 +4,10 @@ import com.bosshand.virgo.api.model.Project;
 import com.bosshand.virgo.api.service.ProjectItemTargetRoomService;
 import com.bosshand.virgo.api.service.ProjectService;
 import com.bosshand.virgo.api.service.QuartzService;
+import com.bosshand.virgo.api.util.CityUtil;
 import com.bosshand.virgo.core.config.OperationControllerLog;
 import com.bosshand.virgo.core.response.Response;
+import com.bosshand.virgo.core.utils.HttpsUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -112,4 +114,15 @@ public class ProjectController {
         return Response.ok(projectService.bimList(id));
     }
 
+    @ApiOperation(value = "获取天气", notes = "获取天气")
+    @RequestMapping(value = "/weather/{city}", method = RequestMethod.GET)
+    public Response bimList(@PathVariable String city) {
+        String code = CityUtil.getCode(city);
+        if (code != null) {
+            String url = "https://www.weatherol.cn/api/home/getCurrAnd15dAnd24h?cityid=" + code;
+            return Response.ok(HttpsUtils.getResult(url));
+        }
+        return Response.fail(200000, "未找到这个城市");
+    }
+
 }

+ 3 - 3
virgo.api/src/main/java/com/bosshand/virgo/api/job/JobCityWeatherQuartz.java

@@ -4,7 +4,6 @@ import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.bosshand.virgo.api.dao.ProjectCityWeatherDao;
 import com.bosshand.virgo.api.model.ProjectCityWeather;
-import com.bosshand.virgo.core.utils.HttpsUtils;
 import org.quartz.JobDataMap;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
@@ -42,8 +41,9 @@ public class JobCityWeatherQuartz extends QuartzJobBean {
         ProjectCityWeather projectCityWeather = new ProjectCityWeather();
         projectCityWeather.setDate(jobExecutionContext.getFireTime());
         projectCityWeather.setProjectId(json.getLongValue("projectId"));
-        String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + json.getString("city") + "&extensions=all&key=8d6519155e085eb1b83d1de7953b2414";
-        projectCityWeather.setWeatherData(HttpsUtils.getResult(url));
+        //String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + json.getString("city") + "&extensions=all&key=8d6519155e085eb1b83d1de7953b2414";
+        //String url = "https://www.weatherol.cn/api/home/getCurrAnd15dAnd24h?cityid=" + CityUtil.getCode(json.getString("city"));
+        //projectCityWeather.setWeatherData(HttpsUtils.getResult(url));
         projectCityWeatherDao.save(projectCityWeather);
 
     }

+ 51 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/util/CityUtil.java

@@ -0,0 +1,51 @@
+package com.bosshand.virgo.api.util;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.io.FileUtils;
+import org.springframework.util.ResourceUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 获取城市code
+ */
+public class CityUtil {
+
+    public static String getCode(String name) {
+        try {
+            File file = ResourceUtils.getFile("classpath:city.json");
+            String json = FileUtils.readFileToString(file, "UTF-8");
+            JSONArray jsonArray = JSONObject.parseArray(json);
+            Map<String, String> code = new HashMap<>();
+            if (code.size() > 0) {
+                return code.get(name);
+            }
+            analysisJson(code, jsonArray);
+            return code.get(name);
+        } catch (IOException o) {
+            o.printStackTrace();
+        }
+        return null;
+    }
+
+    public static void analysisJson(Map<String, String> code, JSONArray jsonArray) {
+        for (int i = 0; i < jsonArray.size(); i++) {
+            JSONArray cityList = jsonArray.getJSONObject(i).getJSONArray("cityList");
+            for (int j = 0; j < cityList.size(); j++) {
+                JSONArray disList = cityList.getJSONObject(j).getJSONArray("disList");
+                for (int h = 0; h < disList.size(); h++) {
+                    JSONObject jsonObject = disList.getJSONObject(h);
+                    code.put(jsonObject.getString("district"), jsonObject.getString("stationid"));
+                }
+            }
+        }
+    }
+
+    public static void main(String[] args) {
+        System.out.println(getCode("西城"));
+    }
+}

File diff suppressed because it is too large
+ 14932 - 0
virgo.api/src/main/resources/city.json