气象预警

pull/1/head
chaiyapeng 2024-08-24 18:59:31 +08:00
parent 4ab69a8887
commit ef47644c0b
6 changed files with 75 additions and 5 deletions

View File

@ -65,5 +65,12 @@ public class MarketController extends BaseController {
public Result getHeadlines(){
return marketService.getHeadlines();
}
/**
*
*/
@GetMapping("getWeather")
public Result getWeather(){
return marketService.getWeather();
}
}

View File

@ -21,4 +21,6 @@ public interface MarketService extends IService<Market> {
Result getIpPlace(String ip);
Result getHeadlines();
Result getWeather();
}

View File

@ -8,6 +8,7 @@ import com.muyu.cloud.mart.utils.IPLocation;
import com.muyu.cloud.mart.utils.MobileLocation;
import com.muyu.cloud.mart.mapper.MarketMapper;
import com.muyu.cloud.mart.service.MarketService;
import com.muyu.cloud.mart.utils.Weather;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.Market;
import com.muyu.domain.PhonePlace;
@ -79,7 +80,7 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
*/
@Override
public Result getIpPlace(String ip) {
StringBuffer stringBuffer = IPLocation.queryMobileLocation(ip);
StringBuffer stringBuffer = IPLocation.queryIPLocation(ip);
return Result.success(stringBuffer);
}
/**
@ -88,7 +89,13 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
*/
@Override
public Result getHeadlines() {
StringBuffer stringBuffer = Headlines.queryMobileLocation();
StringBuffer stringBuffer = Headlines.queryHeadlines();
return Result.success(stringBuffer);
}
@Override
public Result getWeather() {
StringBuffer stringBuffer = Weather.queryWeather();
return Result.success(stringBuffer);
}
}

View File

@ -18,7 +18,7 @@ import static com.muyu.cloud.mart.config.IPLocation.params;
*/
public class Headlines {
public static StringBuffer queryMobileLocation(){
public static StringBuffer queryHeadlines(){
String apiKey = "cdbb93769c75054e6beda4c1dc0b6a0b";
String apiUrl = "http://v.juhe.cn/toutiao/index";
@ -28,7 +28,6 @@ public class Headlines {
map.put("page", "20");
map.put("page_size", "");
map.put("is_filter", "");
System.out.println("111");
try {
URL url = new URL(String.format(apiUrl + "?" + params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));

View File

@ -17,7 +17,7 @@ import java.util.stream.Collectors;
* @Date2024/8/23 20:54
*/
public class IPLocation {
public static StringBuffer queryMobileLocation(String ip){
public static StringBuffer queryIPLocation(String ip){
String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955";
String apiUrl = "http://apis.juhe.cn/ip/ipNewV3";
HashMap<String, String> map = new HashMap<>();

View File

@ -0,0 +1,55 @@
package com.muyu.cloud.mart.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.cloud.mart.utils
* @Projectcloud-mart
* @nameWeather
* @Date2024/8/24 18:51
*/
public class Weather {
public static StringBuffer queryWeather() {
String apiKey = "80e78ee9adaded8b1f42ec9cbdf69ac9";
String apiUrl = "https://apis.juhe.cn/fapig/alarm/citys";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
try {
URL url = new URL(String.format(apiUrl + "?" + params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
return response;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}