81 lines
2.9 KiB
Java
81 lines
2.9 KiB
Java
package com.cyf.devToolBox.controller;
|
|
|
|
import com.cyf.devToolBox.juhe.soup.JuheSoupClient;
|
|
import com.cyf.devToolBox.juhe.soup.model.JuheSoupResponse;
|
|
import com.cyf.devToolBox.juhe.weather.JuheWeatherClient;
|
|
import com.cyf.devToolBox.juhe.weather.model.JuheWeatherResponse;
|
|
import com.cyf.devToolBox.common.Result;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
/**
|
|
* 聚合数据接口,统一返回 {@link Result} 结构。
|
|
*
|
|
* <pre>
|
|
* GET /api/juhe/weather?city=丰台 查询实时天气(未来天数取配置默认值)
|
|
* GET /api/juhe/weather?city=北京&futureNum=3 指定未来天气天数
|
|
* GET /api/juhe/weather/raw?city=北京 返回原始 JSON,便于排查
|
|
* GET /api/juhe/soup 查询每日一句
|
|
* GET /api/juhe/soup/raw 返回原始 JSON,便于排查
|
|
* </pre>
|
|
*
|
|
* @author cyf
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/juhe")
|
|
public class JuheController {
|
|
|
|
private final JuheWeatherClient juheWeatherClient;
|
|
private final JuheSoupClient juheSoupClient;
|
|
|
|
public JuheController(JuheWeatherClient juheWeatherClient, JuheSoupClient juheSoupClient) {
|
|
this.juheWeatherClient = juheWeatherClient;
|
|
this.juheSoupClient = juheSoupClient;
|
|
}
|
|
|
|
/**
|
|
* 查询指定城市的天气。
|
|
*
|
|
* @param city 城市名称(中文),如:北京、丰台
|
|
* @param futureNum 未来天气天数(1~7),不传则使用配置中的默认值
|
|
* @return 统一返回结构,data 为天气数据
|
|
*/
|
|
@GetMapping("/weather")
|
|
public Result<JuheWeatherResponse.Result> weather(@RequestParam String city,
|
|
@RequestParam(required = false) Integer futureNum) {
|
|
JuheWeatherResponse.Result data = futureNum == null
|
|
? juheWeatherClient.query(city)
|
|
: juheWeatherClient.query(city, futureNum);
|
|
return Result.success(data);
|
|
}
|
|
|
|
/**
|
|
* 天气接口原始 JSON,便于排查问题。
|
|
*/
|
|
@GetMapping("/weather/raw")
|
|
public Result<String> weatherRaw(@RequestParam String city,
|
|
@RequestParam(required = false) Integer futureNum) {
|
|
return Result.success(juheWeatherClient.queryRaw(city, futureNum));
|
|
}
|
|
|
|
/**
|
|
* 查询每日一句。
|
|
*
|
|
* @return 统一返回结构,data 为每日一句数据
|
|
*/
|
|
@GetMapping("/soup")
|
|
public Result<JuheSoupResponse.Result> soup() {
|
|
return Result.success(juheSoupClient.query());
|
|
}
|
|
|
|
/**
|
|
* 每日一句接口原始 JSON,便于排查问题。
|
|
*/
|
|
@GetMapping("/soup/raw")
|
|
public Result<String> soupRaw() {
|
|
return Result.success(juheSoupClient.queryRaw());
|
|
}
|
|
}
|