Compare commits

...

4 Commits

Author SHA1 Message Date
chaiyapeng 6a324ff5e1 生辰助手 2024-08-24 19:51:51 +08:00
chaiyapeng ef47644c0b 气象预警 2024-08-24 18:59:31 +08:00
chaiyapeng 4ab69a8887 新闻头条 2024-08-24 18:45:58 +08:00
chaiyapeng 92f0018697 IP查询归属地 2024-08-23 22:11:06 +08:00
9 changed files with 229 additions and 27 deletions

View File

@ -0,0 +1,22 @@
package com.muyu.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.domain
* @Projectcloud-mart
* @nameBirthdayHelper
* @Date2024/8/24 19:37
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Birthday {
private String year;
private String month;
private String day;
private String hour;
}

View File

@ -7,6 +7,7 @@ import com.muyu.cloud.mart.service.MarketService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.domain.Birthday;
import com.muyu.domain.Market;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
@ -65,5 +66,19 @@ public class MarketController extends BaseController {
public Result getHeadlines(){
return marketService.getHeadlines();
}
/**
*
*/
@GetMapping("getWeather")
public Result getWeather(){
return marketService.getWeather();
}
/**
*
*/
@PostMapping("getBirthday")
public Result getBirthday(@RequestBody Birthday birthday){
return marketService.getBirthday(birthday);
}
}

View File

@ -2,6 +2,7 @@ package com.muyu.cloud.mart.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.Birthday;
import com.muyu.domain.Market;
import java.util.List;
@ -21,4 +22,8 @@ public interface MarketService extends IService<Market> {
Result getIpPlace(String ip);
Result getHeadlines();
Result getWeather();
Result getBirthday(Birthday birthday);
}

View File

@ -3,11 +3,11 @@ package com.muyu.cloud.mart.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.cloud.mart.utils.IPLocation;
import com.muyu.cloud.mart.utils.MobileLocation;
import com.muyu.cloud.mart.utils.*;
import com.muyu.cloud.mart.mapper.MarketMapper;
import com.muyu.cloud.mart.service.MarketService;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.Birthday;
import com.muyu.domain.Market;
import com.muyu.domain.PhonePlace;
import lombok.extern.log4j.Log4j2;
@ -78,7 +78,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);
}
/**
@ -87,28 +87,21 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
*/
@Override
public Result getHeadlines() {
String apiKey = "cdbb93769c75054e6beda4c1dc0b6a0b";
String apiUrl = "http://v.juhe.cn/toutiao/index";
StringBuffer stringBuffer = Headlines.queryHeadlines();
return Result.success(stringBuffer);
}
/**
*
*/
@Override
public Result getWeather() {
StringBuffer stringBuffer = Weather.queryWeather();
return Result.success(stringBuffer);
}
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("type", "top");
map.put("page", "20");
map.put("page_size", "");
map.put("is_filter", "");
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 Result.success(response);
} catch (Exception e) {
throw new RuntimeException(e);
}
@Override
public Result getBirthday(Birthday birthday) {
StringBuffer stringBuffer = BirthdayHelper.queryBirthday(birthday);
return Result.success(stringBuffer);
}
}

View File

@ -0,0 +1,66 @@
package com.muyu.cloud.mart.utils;
import com.muyu.domain.Birthday;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.cloud.mart.utils
* @Projectcloud-mart
* @nameBirthdayHelper
* @Date2024/8/24 19:36
*/
public class BirthdayHelper {
public static StringBuffer queryBirthday(Birthday birthday){
String apiKey = "9bbfc46233d4931c39207b090ed4b27c";
String apiUrl = "http://apis.juhe.cn/birthEight/query";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("year", birthday.getYear());
birthday.setYear(map.put("year", birthday.getYear()));
map.put("month", birthday.getMonth());
birthday.setMonth(map.put("month", birthday.getMonth()));
map.put("day", birthday.getDay());
birthday.setDay(map.put("day", birthday.getDay()));
map.put("hour", birthday.getHour());
birthday.setHour(map.put("hour", birthday.getHour()));
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("&"));
}
}

View File

@ -0,0 +1,46 @@
package com.muyu.cloud.mart.utils;
import com.muyu.common.core.domain.Result;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import static com.muyu.cloud.mart.config.IPLocation.params;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.cloud.mart.utils
* @Projectcloud-mart
* @nameHeadlines
* @Date2024/8/24 18:39
*/
public class Headlines {
public static StringBuffer queryHeadlines(){
String apiKey = "cdbb93769c75054e6beda4c1dc0b6a0b";
String apiUrl = "http://v.juhe.cn/toutiao/index";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("type", "top");
map.put("page", "20");
map.put("page_size", "");
map.put("is_filter", "");
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);
}
}
}

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

@ -28,7 +28,7 @@ public class MobileLocation {
/**
* /7
* @param mobile
* @param tel
*/
public static PhonePlace queryMobileLocation(String tel)
{

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("&"));
}
}