🔄Update: 天气板块

This commit is contained in:
2025-10-28 10:31:12 +08:00
parent 6c53f352df
commit ef4a5e18c9
19 changed files with 293 additions and 4 deletions
+46
View File
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hezhaohui</groupId>
<artifactId>lark-panel</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>weather</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.27</version>
</dependency>
<dependency>
<groupId>cn.hezhaohui</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
@@ -0,0 +1,11 @@
package cn.hezhaohui;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,16 @@
package cn.hezhaohui.entity;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Weather {
private String weather;
private String temperature;
private String windPower;
private String windDirection;
private String reportTime;
private String humidity;
private String content;
}
@@ -0,0 +1,30 @@
package cn.hezhaohui.service;
import cn.hezhaohui.common.AIConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ChatService {
private final ChatClient chatClient;
public ChatService(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
public String doChat(String background, String content) {
String ans = chatClient
.prompt()
.system(AIConstant.SYSTEM_PROMPT)
.user("[BackGround]: " + background + "\n" + "[Content]: " + content)
.call()
.content();
log.info(ans);
return ans;
}
}
@@ -0,0 +1,21 @@
package cn.hezhaohui.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}
@@ -0,0 +1,64 @@
package cn.hezhaohui.util;
import cn.hezhaohui.common.AIConstant;
import cn.hezhaohui.entity.Weather;
import cn.hezhaohui.service.ChatService;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import static cn.hezhaohui.common.WeatherConstant.*;
@Slf4j
public class WeatherUtil {
/**
* 参考文档: https://lbs.amap.com/api/webservice/guide/api/weatherinfo
* URL 格式: https://restapi.amap.com/v3/weather/weatherInfo?parameters
*
*/
private static String createUrl() {
String url = API_BASE_URL +
"?key=" +
API_SECRET +
"&city=" +
CITY_CODE_WUHAN +
"&extensions=base";
log.info("[Weather] URL: {}", url);
return url;
}
public static Weather getWeather() {
String url = createUrl();
String ans = HttpUtil.get(url);
log.info("[Weather] Response: {}", ans);
JSONObject obj = JSONUtil.parseObj(ans);
log.info("[Weather] Obj: {}", obj);
// 检查返回状态码
String status = obj.getStr("status");
if (!"1".equals(status)) {
String info = obj.getStr("info", "Unknown Error");
String errorCode = obj.getStr("infocode", "N/A");
log.error("[Weather] Request failed with code: {}, error: {}", errorCode, info);
return null;
}
// 获取天气信息数组
JSONObject weatherData = obj.getJSONArray("lives").getJSONObject(0);
Weather weather = Weather.builder()
.weather(weatherData.getStr("weather"))
.temperature(weatherData.getStr("temperature"))
.windDirection(weatherData.getStr("winddirection"))
.windPower(weatherData.getStr("windpower"))
.reportTime(weatherData.getStr("reporttime"))
.humidity(weatherData.getStr("humidity"))
.build();
String content = SpringContextUtil.getBean(ChatService.class).doChat(AIConstant.BACKGROUND_WEATHER, weather.toString());
weather.setContent(content);
log.info("[Weather] Prepared: {}", weather);
return weather;
}
}
@@ -0,0 +1,9 @@
spring:
ai:
openai:
api-key: sk-ihsqnxhnzmgsaojdkxomcmgdypdfpzojymfndfklgzfchlgz
base-url: https://api.siliconflow.cn
chat:
options:
model: Pro/Qwen/Qwen2.5-7B-Instruct
temperature: 0.7
@@ -0,0 +1,18 @@
package cn.hezhaohui.service;
import cn.hezhaohui.common.AIConstant;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class ChatServiceTest {
@Resource
private ChatService chatService;
@Test
void chat() {
chatService.doChat(AIConstant.BACKGROUND_WEATHER, "今天下雨");
}
}
@@ -0,0 +1,15 @@
package cn.hezhaohui.util;
import cn.hezhaohui.entity.Weather;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class WeatherUtilTest {
@Test
void getWeather() {
Weather weather = WeatherUtil.getWeather();
System.out.println(weather);
}
}