add weather
parent
86ac28ec4f
commit
0ceac9a468
@ -0,0 +1,78 @@
|
||||
package cn.iocoder.yudao.module.mes.controller.admin.home;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.iot.framework.gateway.util.HttpUtils;
|
||||
import cn.iocoder.yudao.module.mes.controller.admin.home.weather.Forecast;
|
||||
import cn.iocoder.yudao.module.mes.controller.admin.home.weather.WeatherInfoGaodeVO;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "登录首页信息")
|
||||
@RestController
|
||||
@RequestMapping("/home/info")
|
||||
@Validated
|
||||
public class HomeController {
|
||||
|
||||
|
||||
|
||||
//中文名 adcode citycode 呼和浩特市 150100 0471
|
||||
//https://restapi.amap.com/v3/weather/weatherInfo?key=1a63897dd11e83ae0be4cc922c07b523&city=150100
|
||||
@GetMapping("/weatherInfo")
|
||||
@Operation(summary = "获得天气")
|
||||
public CommonResult<Forecast> getTask() {
|
||||
WeatherInfoGaodeVO gaodeVO = getWeather();
|
||||
Forecast forecast = null;
|
||||
if(gaodeVO.getStatus().equals("1") && gaodeVO.getForecasts()!=null && gaodeVO.getForecasts().size()>0){
|
||||
forecast = gaodeVO.getForecasts().get(0);
|
||||
forecast.setIsEnable(true);
|
||||
}
|
||||
else {
|
||||
forecast = new Forecast();
|
||||
forecast.setIsEnable(false);
|
||||
}
|
||||
return success(forecast);
|
||||
}
|
||||
|
||||
public WeatherInfoGaodeVO getWeather() {
|
||||
String key = "2f0de30f37da59122eeb774d3a2ba6da";
|
||||
String city = "150100";
|
||||
String extensions = "all";
|
||||
String URL= "https://restapi.amap.com/v3/weather/weatherInfo?key="+ key +"&city="+city+"&extensions="+extensions;
|
||||
// 创建 HttpClient 客户端
|
||||
CloseableHttpClient httpClient = HttpUtils.createSSLClientDefault();
|
||||
CloseableHttpResponse httpResponse = null;
|
||||
WeatherInfoGaodeVO gaodeVO = null;
|
||||
try {
|
||||
HttpGet httpGet = HttpUtils.getHttpGet(URL,new ArrayList<>());
|
||||
httpResponse = httpClient.execute(httpGet);
|
||||
StatusLine statusLine = httpResponse.getStatusLine();
|
||||
if (statusLine.getStatusCode() == 200) {
|
||||
String entityStr = EntityUtils.toString(httpResponse.getEntity());
|
||||
gaodeVO = JSON.parseObject(entityStr, WeatherInfoGaodeVO.class);
|
||||
}
|
||||
}
|
||||
// 无论如何必须关闭连接
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
HttpUtils.closeHttp(httpClient, httpResponse);
|
||||
}
|
||||
return gaodeVO;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.yudao.module.mes.controller.admin.home.weather;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Cast {
|
||||
private String date;
|
||||
private String week;
|
||||
private String dayweather;
|
||||
private String nightweather;
|
||||
private String daytemp;
|
||||
private String nighttemp;
|
||||
private String daywind;
|
||||
private String nightwind;
|
||||
private String daypower;
|
||||
private String nightpower;
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package cn.iocoder.yudao.module.mes.controller.admin.home.weather;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Forecast {
|
||||
private Boolean isEnable;
|
||||
private String city;
|
||||
private String adcode;
|
||||
private String province;
|
||||
private String reporttime;
|
||||
private List<Cast> casts;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.yudao.module.mes.controller.admin.home.weather;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - 高得请求结果VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class WeatherInfoGaodeVO {
|
||||
|
||||
private String status;
|
||||
private String info;
|
||||
private String count;
|
||||
private String infocode;
|
||||
private List<Map<String,String>> lives;
|
||||
private List<Forecast> forecasts;
|
||||
}
|
||||
Loading…
Reference in New Issue