From c911ef5dc965715e9741f13d5659577b72704a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=B0=8F=E4=BA=91?= Date: Fri, 30 Jan 2026 14:09:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/config/WeatherProperties.java | 36 +++++++++++++++++++ .../device/domain/impl/WeatherDomainImpl.java | 20 ++++++----- 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/ruoyi/device/domain/config/WeatherProperties.java diff --git a/src/main/java/com/ruoyi/device/domain/config/WeatherProperties.java b/src/main/java/com/ruoyi/device/domain/config/WeatherProperties.java new file mode 100644 index 0000000..33ed17e --- /dev/null +++ b/src/main/java/com/ruoyi/device/domain/config/WeatherProperties.java @@ -0,0 +1,36 @@ +package com.ruoyi.device.domain.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 天气API配置属性 + * + * @author ruoyi + */ +@Data +@Component +@ConfigurationProperties(prefix = "weather.api") +public class WeatherProperties { + + /** + * 天气API AppCode + */ + private String appcode = "6a152d74a3c249bfa6db6e664f2541f0"; + + /** + * 天气API Host + */ + private String host = "https://aliv8.data.moji.com"; + + /** + * 天气API Path + */ + private String path = "/whapi/json/aliweather/condition"; + + /** + * 天气API Token + */ + private String token = "ff826c205f8f4a59701e64e9e64e01c4"; +} \ No newline at end of file diff --git a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java index 8a72da9..39a4d89 100644 --- a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java +++ b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java @@ -5,10 +5,11 @@ import com.ruoyi.device.domain.api.IWeatherDomain; import com.ruoyi.device.domain.impl.weather.HttpUtils; import com.ruoyi.device.domain.model.weather.Weather; import com.ruoyi.device.domain.model.weather.WeatherResponse; +import com.ruoyi.device.domain.config.WeatherProperties; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @@ -16,23 +17,24 @@ import java.util.Map; @Component public class WeatherDomainImpl implements IWeatherDomain { - String appcode = "6a152d74a3c249bfa6db6e664f2541f0"; + @Autowired + private WeatherProperties weatherProperties; public Weather weatherInfo(String lat, String lon) { - String host = "https://aliv8.data.moji.com"; - String path = "/whapi/json/aliweather/condition"; + String host = weatherProperties.getHost(); + String path = weatherProperties.getPath(); String method = "POST"; - Map headers = new HashMap(); + Map headers = new HashMap<>(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 - headers.put("Authorization", "APPCODE " + appcode); + headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); //根据API的要求,定义相对应的Content-Type headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - Map querys = new HashMap(); - Map bodys = new HashMap(); + Map querys = new HashMap<>(); + Map bodys = new HashMap<>(); bodys.put("lat", lat); bodys.put("lon", lon); - bodys.put("token", "ff826c205f8f4a59701e64e9e64e01c4"); + bodys.put("token", weatherProperties.getToken()); Weather weather = new Weather(); try {