diff --git a/toolsTabBackend/pom.xml b/toolsTabBackend/pom.xml index bc988d57f3434997982a2a54122c0b4012d4b117..6ded812c2286f120cd64eb98221d4110ec877385 100644 --- a/toolsTabBackend/pom.xml +++ b/toolsTabBackend/pom.xml @@ -80,7 +80,35 @@ mapper-spring-boot-starter 2.1.5 - + + + org.lionsoul + ip2region + 2.6.5 + + + + org.jsoup + jsoup + 1.13.1 + + + + org.apache.httpcomponents + httpclient + 4.5.9 + + + + com.alibaba + fastjson + 1.2.28 + + + + org.projectlombok + lombok + @@ -102,7 +130,7 @@ ${spring-boot.version} com.sleeppeople.toolstabbackend.ToolsTabBackendApplication - true + false diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/bo/Weather.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/bo/Weather.java new file mode 100644 index 0000000000000000000000000000000000000000..61944508f3a1259fb5ffb1235137a9e4a9cddc31 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/bo/Weather.java @@ -0,0 +1,39 @@ +package com.sleeppeople.toolstabbackend.bo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Weather { + private String province; + private String city; + private String adcode; + private String temperature_float; + private String humidity_float; + private String windpower; + private String weather; + private String temperature; + private String humidity; + private String reporttime; + private String winddirection; + + @Override + public String toString() { + return "Weather{" + + "province='" + province + '\'' + + ", city='" + city + '\'' + + ", adcode='" + adcode + '\'' + + ", temperature_float='" + temperature_float + '\'' + + ", humidity_float='" + humidity_float + '\'' + + ", windpower='" + windpower + '\'' + + ", weather='" + weather + '\'' + + ", temperature='" + temperature + '\'' + + ", humidity='" + humidity + '\'' + + ", reporttime='" + reporttime + '\'' + + ", winddirection='" + winddirection + '\'' + + '}'; + } +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/AboutIpController.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/AboutIpController.java new file mode 100644 index 0000000000000000000000000000000000000000..5e5e481f28ad5fbb8f217816e2c6c97cace76b9b --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/AboutIpController.java @@ -0,0 +1,69 @@ +package com.sleeppeople.toolstabbackend.controller.beautifullife; + +import com.sleeppeople.toolstabbackend.bo.Weather; +import com.sleeppeople.toolstabbackend.service.beautifullife.impl.BeautifulLifeServiceImpl; +import com.sleeppeople.toolstabbackend.utils.iputils.CityCode; +import com.sleeppeople.toolstabbackend.utils.iputils.IPUtils; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.net.URISyntaxException; + +@RestController +public class AboutIpController { + @Data + @NoArgsConstructor + @AllArgsConstructor + @ToString + static + class MyStatus{ + String ip; + String ipUrban; + Weather weather; + } + @Autowired + private BeautifulLifeServiceImpl beautifulLifeService; + + @GetMapping("/getStatus") + public MyStatus getStatus(){ + MyStatus myStatus= new MyStatus(); + //获取请求ip(爬服务器的方式) + String ip = null; + try { + ip = beautifulLifeService.getIpFromRualaFun(); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("ip请求发给服务器时出现问题"); + } + myStatus.setIp(ip); + //获取精确的ip地址 + String ipUrban = beautifulLifeService.getIpUrban(IPUtils.getIpAddress(ip)); + myStatus.setIpUrban(ipUrban); + + CityCode cityCode=CityCode.getCityCodeFromName(ipUrban); + System.out.println(cityCode.getCode()); + + //获取该地址天气状况 + Weather allWeather = null; + try { + allWeather = beautifulLifeService.getAllWeather(ipUrban); + } catch (URISyntaxException e) { + System.out.println("获取响应体出错"); + e.printStackTrace(); + } catch (IOException e) { + System.out.println("io"); + e.printStackTrace(); + } + myStatus.setWeather(allWeather); + //检查赋值情况 + System.out.println("myStatus = " + myStatus); + return myStatus; + } + +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/LuckyController.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/LuckyController.java new file mode 100644 index 0000000000000000000000000000000000000000..d31e13ae8979fff776eeba0c4924112f8e28fb39 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/LuckyController.java @@ -0,0 +1,8 @@ +package com.sleeppeople.toolstabbackend.controller.beautifullife; + +import org.springframework.stereotype.Controller; + +@Controller +public class LuckyController { + +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/TestIpController.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/TestIpController.java new file mode 100644 index 0000000000000000000000000000000000000000..cc61e43799ff41a4e2dc5d7463340c4307b6aa80 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/controller/beautifullife/TestIpController.java @@ -0,0 +1,16 @@ +package com.sleeppeople.toolstabbackend.controller.beautifullife; + +import com.sleeppeople.toolstabbackend.utils.iputils.IPUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +public class TestIpController { + @RequestMapping("/testip") + public String testIp(HttpServletRequest request){ + String clientIp = IPUtils.getClientIp(request); + return clientIp; + } +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/BeautifulLifeService.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/BeautifulLifeService.java new file mode 100644 index 0000000000000000000000000000000000000000..c59eeaa4583460ffcd7c24166a8eb27e5c1c5a25 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/BeautifulLifeService.java @@ -0,0 +1,42 @@ +package com.sleeppeople.toolstabbackend.service.beautifullife; + +import com.sleeppeople.toolstabbackend.bo.Weather; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.net.URISyntaxException; + +@Service +public interface BeautifulLifeService { + /** + * 获取用户请求ip + * @param request request + * @return String + */ + String getIp(HttpServletRequest request); + + /** + * 通过请求服务器爬取ip地址 + * @return String + */ + String getIpFromRualaFun() throws IOException; + + + /** + * 获取ip归属地(精确到省市) + * @param ipAddress ip地址 + * @return String + */ + String getIpUrban(String ipAddress); + + /** + * 获取该ip归属地(精确到省市)的整体天气状况 + * @param cityCode 城市编码 + * @return String + */ + Weather getAllWeather(String cityCode) throws URISyntaxException, IOException; + + + +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/impl/BeautifulLifeServiceImpl.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/impl/BeautifulLifeServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..9c1a6fb3da52f62c015df7c380bed2ee5ac566e8 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/service/beautifullife/impl/BeautifulLifeServiceImpl.java @@ -0,0 +1,88 @@ +package com.sleeppeople.toolstabbackend.service.beautifullife.impl; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.sleeppeople.toolstabbackend.bo.Weather; +import com.sleeppeople.toolstabbackend.service.beautifullife.BeautifulLifeService; +import com.sleeppeople.toolstabbackend.utils.iputils.IPUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.net.URISyntaxException; + +@Service +public class BeautifulLifeServiceImpl implements BeautifulLifeService { + + + @Override + public String getIp(HttpServletRequest request) { + return IPUtils.getClientIp(request); + } + + @Override + public String getIpFromRualaFun() throws IOException { + String ip = IPUtils.getIpFromRualaFun(); + return ip; + } + + @Override + public String getIpUrban(String ipAddress) { + //返回值为 中国|0|天津|天津市|移动 ; 通过|分割 + String[] split = ipAddress.split("\\|"); + //拿到省市名称 + String urban = split[3]; + System.out.println(urban); + return urban; + } + + @Override + public Weather getAllWeather(String cityCode) throws URISyntaxException, IOException { + //创建httpClient实例 + CloseableHttpClient httpClient = HttpClients.createDefault(); + //创建uri对象 + URIBuilder uriBuilder = new URIBuilder("https://restapi.amap.com/v3/weather/weatherInfo"); + uriBuilder.setParameter("key", "90920abac0bc960ad2c34f5437cffa68"); + uriBuilder.setParameter("city", cityCode); + uriBuilder.setParameter("extensions", "base"); + uriBuilder.setParameter("output", "JSON"); + //httpGet远程连接实例 + HttpGet httpGet = new HttpGet(uriBuilder.build()); + RequestConfig requestConfig = RequestConfig.custom() + .setConnectTimeout(30000) + .setConnectionRequestTimeout(30000) + .setSocketTimeout(60000) + .build(); + httpGet.setConfig(requestConfig); + //发送请求 + CloseableHttpResponse response = httpClient.execute(httpGet); + + //拿到响应体data + HttpEntity responseEntity = response.getEntity(); + String responseEntityString = EntityUtils.toString(responseEntity); + System.out.println("responseEntityString = " + responseEntityString); + //解析json响应体 + JSONObject jsonObject = JSONObject.parseObject(responseEntityString); + //lives = [{"province":"天津","city":"天津市","adcode":"120000","temperature_float":"13.0","humidity_float":"50.0","windpower":"≤3","weather":"阴","temperature":"13","humidity":"50","reporttime":"2024-04-29 09:03:18","winddirection":"东北"}] + JSONArray lives = jsonObject.getJSONArray("lives"); + //JSONArray转为JSONObject + JSONObject object = lives.getJSONObject(0); + System.out.println("object = " + object); + //JSONObject转为Weather类 +// Weather weather = JSONObject.parseObject("object", Weather.class); + Weather weather = object.toJavaObject(Weather.class); + System.out.println("weather = " + weather); + + return weather; + } + + +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/CityCode.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/CityCode.java new file mode 100644 index 0000000000000000000000000000000000000000..903583bca6ff0a38c16b808b1cff2494a93c5298 --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/CityCode.java @@ -0,0 +1,75 @@ +package com.sleeppeople.toolstabbackend.utils.iputils; + +import lombok.Getter; + +import java.util.HashMap; + +@Getter +public enum CityCode { + BEIJING("北京市","110000"), + TIANJIN("天津市","120000"), + HEBEI("河北省","130000"), + SHIJIAZHUANG("石家庄市","130100"), + TANGSHAN("唐山市","130200"), + QINHUANGDAO("秦皇岛市","130300"), + HANDAN("邯郸市","130400"), + XINGTAI("邢台市","130500"), + BAODING("保定市","130600"), + ZHANGJIAKOU("张家口市","130700"), + CHENGDE("承德市","130800"), + CANGZHOU("沧州市","130900"), + LANGFANG("廊坊市","131000"), + HENGSHUI("衡水市","131100"), + SHANXI("山西省","140000"), + //todo: 该写太原市了 + NEIMENG("内蒙古自治区","150000"), + LIAONING("辽宁省","210000"), + JILIN("吉林省","220000"), + HEILONGJIANG("黑龙江省","230000"), + SHANGHAI("上海市","310000"), + JIANGSU("江苏省","320000"), + ZHEJIANG("浙江省","330000"), + ANHUI("安徽省","340000"), + FUJIAN("福建省","350000"), + JIANGXI("江西省","360000"), + SHANDONG("山东省","370000"), + HENAN("河南省","410000"), + HUBEI("湖北省","420000"), + HUNAN("湖南省","430000"), + GUANGDONG("广东省","440000"), + GUANGXI("广西壮族自治区","450000"), + HAINAN("海南省","460000"), + CHONGQING("重庆市","500000"), + SICHUAN("四川省","510000"), + GUIZHOU("贵州省","520000"), + YUNNAN("云南省","530000"), + XIZANG("西藏自治区","540000"), + SHANXISHENG("陕西省","610000"),//陕西和山西拼音相同,故加"省"的拼音区分 + GANSU("甘肃省","620000"), + QINGHAI("青海省","630000"), + NINGXIA("宁夏回族自治区","640000"), + XINJIANG("新疆维吾尔自治区","650000"), + TAIWAN("台湾省","710000"), + XIANGGANG("香港特别行政区","810000"), + AOMEN("澳门特别行政区","820000"), + ; + + private String name; //城市名称 + private String code; //城市编码 + private static HashMap map; + CityCode(String name,String code) { + this.name=name; + this.code=code; + } + static { + HashMap cityCodeHashMap=new HashMap<>(CityCode.values().length); + for (CityCode value: CityCode.values()) { + cityCodeHashMap.put(value.getName(), value); + } + map=cityCodeHashMap; + } + public static CityCode getCityCodeFromName(String name){ + return map.get(name); + } + +} diff --git a/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/IPUtils.java b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/IPUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..946e314cf3bf1a417ec978a2e7ab5b3b5204c35e --- /dev/null +++ b/toolsTabBackend/src/main/java/com/sleeppeople/toolstabbackend/utils/iputils/IPUtils.java @@ -0,0 +1,110 @@ +package com.sleeppeople.toolstabbackend.utils.iputils; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.lionsoul.ip2region.xdb.Searcher; +import org.springframework.util.ResourceUtils; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URL; + + +public class IPUtils { + + /** + * 获取用户请求ip(本机测试只能获取局域网ip) + * @param request HttpServletRequest + * @return String + */ + public static String getClientIp(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + if (ip!= null && !ip.isEmpty() && ! "unknown".equalsIgnoreCase(ip)) { + //多次反向代理后会有多个ip值,第一个ip才是真实ip + if (ip.contains(",")) { + ip=ip.split(",")[0]; + } + } + //Proxy-Client-IP:apache服务代理 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("Proxy-Client-IP"); + } + //WL-Proxy-Client-IP:weblogic服务代理 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("WL-Proxy-Client-IP"); + } + //HTTP_CLIENT_IP:有些代理服务器 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("HTTP_CLIENT_IP"); + } + //HTTP_X_FORWARDED_FOR:用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("HTTP_X_FORWARDED_FOR"); + } + //X-Real-IP:nginx服务代理 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getHeader("X-Real-IP"); + } + //还是不能获取到,最后再通过request.getRemoteAddr();获取 + if (ip== null ||ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip=request.getRemoteAddr(); + } + System.out.println("ip = " + ip); + return ip; + } + + /** + * 获取用户请求ip(通过请求服务器爬取ip地址)(需fastjson依赖) + * @return String + * @throws IOException IOException + */ + public static String getIpFromRualaFun() throws IOException { + String path="http://ruala.fun:2395/cloudTestIp"; + Document document = Jsoup.parse(new URL(path), 10000); + String text = document.getElementsByTag("body").text(); + System.out.println(document); + System.out.println(text); + return text; + } + + /** + * 获取ip归属地(需ip2region依赖) + * @param ip 用户请求ip + * @return String + */ + public static String getIpAddress(String ip) { + Searcher searcher=null; + //fixme: 为了在本地测ip,先请求ruala服务器拿到ip再用该ip判断归属地(方法为getIpFromRualaFun),日后换成getIp方法调用IpUtils + if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) { + return "局域网 ip"; + } + + if (searcher == null) { + try { + File file = ResourceUtils.getFile("classpath:beautifullife/utils/ipdb/ip2region.xdb"); + String dbPath = file.getPath(); + searcher = Searcher.newWithFileOnly(dbPath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + String region = null; + String errorMessage = null; + try { + region = searcher.search(ip); + } catch (Exception e) { + errorMessage = e.getMessage(); + if (errorMessage != null && errorMessage.length() > 256) { + errorMessage = errorMessage.substring(0,256); + } + e.printStackTrace(); + } + // 输出 region + System.out.println(region); + return region; + } +} diff --git a/toolsTabBackend/src/main/resources/application-bc.yaml b/toolsTabBackend/src/main/resources/application-bc.yaml index d25061583c7b1dd6f58b7bbf5674b09c63c44d75..38acc151a48496ff70d6383ef4d17911cc8f9086 100644 --- a/toolsTabBackend/src/main/resources/application-bc.yaml +++ b/toolsTabBackend/src/main/resources/application-bc.yaml @@ -1,3 +1,4 @@ + spring: #数据库配置 datasource: @@ -6,3 +7,5 @@ spring: username: root #username password: xyx020717 #password type: com.alibaba.druid.pool.DruidDataSource #数据源(spring boot默认HikariCP) + + diff --git a/toolsTabBackend/src/main/resources/beautifullife/utils/ipdb/ip2region.xdb b/toolsTabBackend/src/main/resources/beautifullife/utils/ipdb/ip2region.xdb new file mode 100644 index 0000000000000000000000000000000000000000..7052c057104ddd0c57f3ec9a8a457e4ec8f7139d Binary files /dev/null and b/toolsTabBackend/src/main/resources/beautifullife/utils/ipdb/ip2region.xdb differ diff --git a/toolsTabFront/src/router/commonRouter.js b/toolsTabFront/src/router/commonRouter.js index 5aaf6eed5a634cb2af483f731e34c74cd26226ff..8132171f79f9ee742aa482a28a42412645edec53 100644 --- a/toolsTabFront/src/router/commonRouter.js +++ b/toolsTabFront/src/router/commonRouter.js @@ -7,7 +7,7 @@ import FoodPage from "../view/FoodPage.vue"; import SchedulePage from "../view/SchedulePage.vue"; import BeautifulLifePage from "../view/BeautifulLifePage.vue"; import GetInfoPage from "../view/GetInfoPage.vue"; -import BCTest from "../view/BCchildrenview/BCTest.vue"; +import BCStatus from "../view/BCchildrenview/BCStatus.vue"; import BC_tryClothes from "../view/BCchildrenview/BC_tryClothes.vue"; export const routes=[ @@ -22,11 +22,10 @@ export const routes=[ { path: '/beautifulLifePage', component: BeautifulLifePage, children:[ - {path:'/bctest',component:BCTest}, + {path:'/bcstatus',component:BCStatus}, {path:'/bc_tryClothes',component:BC_tryClothes}, ] }, { path: '/getInfoPage',component: GetInfoPage}, - { path: '/bctest',component: BCTest}, ] \ No newline at end of file diff --git a/toolsTabFront/src/view/BCchildrenview/BCStatus.vue b/toolsTabFront/src/view/BCchildrenview/BCStatus.vue new file mode 100644 index 0000000000000000000000000000000000000000..ddd018198d1f8e4a0c133fbc08666703f070ffd8 --- /dev/null +++ b/toolsTabFront/src/view/BCchildrenview/BCStatus.vue @@ -0,0 +1,35 @@ + + + + + \ No newline at end of file diff --git a/toolsTabFront/src/view/BCchildrenview/BCTest.vue b/toolsTabFront/src/view/BCchildrenview/BCTest.vue deleted file mode 100644 index 79261426082b3768040ef179e4d9bbb98ee2e9c3..0000000000000000000000000000000000000000 --- a/toolsTabFront/src/view/BCchildrenview/BCTest.vue +++ /dev/null @@ -1,17 +0,0 @@ - - - - - \ No newline at end of file diff --git a/toolsTabFront/src/view/BeautifulLifePage.vue b/toolsTabFront/src/view/BeautifulLifePage.vue index f7b9bfd912beda25a0ebdf91d6708d1fc400cb25..9e343311b702aba1a0082013016c9664a243a619 100644 --- a/toolsTabFront/src/view/BeautifulLifePage.vue +++ b/toolsTabFront/src/view/BeautifulLifePage.vue @@ -1,5 +1,5 @@