From bc97dc7f891787a0a9248a0d6a742350bd842f84 Mon Sep 17 00:00:00 2001 From: chaiyapeng <3535863041@qq.com> Date: Fri, 23 Aug 2024 21:00:12 +0800 Subject: [PATCH] =?UTF-8?q?IP=E6=9F=A5=E8=AF=A2=E5=BD=92=E5=B1=9E=E5=9C=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/muyu/domain/PhonePlace.java | 25 +++++++++ .../mart/controller/MarketController.java | 4 +- .../cloud/mart/service/MarketService.java | 2 +- .../mart/service/impl/MarketServiceImpl.java | 35 +++--------- .../com/muyu/cloud/mart/utils/IPLocation.java | 55 +++++++++++++++++++ .../{config => utils}/MobileLocation.java | 19 ++++--- 6 files changed, 103 insertions(+), 37 deletions(-) create mode 100644 cloud-mart-common/src/main/java/com/muyu/domain/PhonePlace.java create mode 100644 cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/IPLocation.java rename cloud-mart-server/src/main/java/com/muyu/cloud/mart/{config => utils}/MobileLocation.java (89%) diff --git a/cloud-mart-common/src/main/java/com/muyu/domain/PhonePlace.java b/cloud-mart-common/src/main/java/com/muyu/domain/PhonePlace.java new file mode 100644 index 0000000..7a6bb32 --- /dev/null +++ b/cloud-mart-common/src/main/java/com/muyu/domain/PhonePlace.java @@ -0,0 +1,25 @@ +package com.muyu.domain; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @Author:chaiyapeng + * @Package:com.muyu.domain + * @Project:cloud-mart + * @name:PhonePlace + * @Date:2024/8/23 20:34 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class PhonePlace { + private String province; + private String city; + private String areacode; + private String zip; + private String company; + private String reason; + private String tel; +} diff --git a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/controller/MarketController.java b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/controller/MarketController.java index 3b151b1..0f0c1ae 100644 --- a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/controller/MarketController.java +++ b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/controller/MarketController.java @@ -48,8 +48,8 @@ public class MarketController extends BaseController { * 手机号查询归属地 */ @GetMapping("getPhonePlace") - public void getPhonePlace(@RequestParam(name = "tel") String tel){ - marketService.getPhonePlace(tel); + public Result getPhonePlace(@RequestParam(name = "tel") String tel){ + return marketService.getPhonePlace(tel); } /** * IP查询归属地 diff --git a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/MarketService.java b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/MarketService.java index 70510a0..1b5e1c6 100644 --- a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/MarketService.java +++ b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/MarketService.java @@ -16,7 +16,7 @@ import java.util.List; public interface MarketService extends IService { List findMarketList(); - void getPhonePlace(String tel); + Result getPhonePlace(String tel); Result getIpPlace(String ip); diff --git a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/impl/MarketServiceImpl.java b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/impl/MarketServiceImpl.java index 64ed344..d7609f0 100644 --- a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/impl/MarketServiceImpl.java +++ b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/service/impl/MarketServiceImpl.java @@ -1,16 +1,15 @@ package com.muyu.cloud.mart.service.impl; -import cn.hutool.log.Log; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.muyu.cloud.mart.config.JuheDemo; -import com.muyu.cloud.mart.config.MobileLocation; +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.common.core.domain.Result; -import com.muyu.common.core.utils.StringUtils; import com.muyu.domain.Market; +import com.muyu.domain.PhonePlace; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; @@ -18,12 +17,10 @@ import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.InputStreamReader; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Locale; import static com.muyu.cloud.mart.config.IPLocation.params; import static com.muyu.common.core.utils.PageUtils.startPage; @@ -69,8 +66,9 @@ public class MarketServiceImpl extends ServiceImpl impleme * @param tel */ @Override - public void getPhonePlace(String tel) { - MobileLocation.queryMobileLocation(tel); + public Result getPhonePlace(String tel) { + PhonePlace phonePlace = MobileLocation.queryMobileLocation(tel); + return Result.success(phonePlace); } /** @@ -80,25 +78,8 @@ public class MarketServiceImpl extends ServiceImpl impleme */ @Override public Result getIpPlace(String ip) { - String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955"; - String apiUrl = "http://apis.juhe.cn/ip/ipNewV3"; - HashMap map = new HashMap<>(); - map.put("key", apiKey); - map.put("ip", ip); - try { - URL url = new URL(String.format(apiUrl + "?" + params(map))); - BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream())); - String inputLine; - StringBuffer stringBuffer = new StringBuffer(); - while ((inputLine = in.readLine()) != null){ - stringBuffer.append(inputLine); - } - in.close(); - System.out.println(stringBuffer); - return Result.success(stringBuffer); - } catch (Exception e) { - throw new RuntimeException(e); - } + StringBuffer stringBuffer = IPLocation.queryMobileLocation(ip); + return Result.success(stringBuffer); } /** * 新闻头条 diff --git a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/IPLocation.java b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/IPLocation.java new file mode 100644 index 0000000..d06edce --- /dev/null +++ b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/IPLocation.java @@ -0,0 +1,55 @@ +package com.muyu.cloud.mart.utils; + +import java.io.BufferedReader; +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; + +/** + * @Author:chaiyapeng + * @Package:com.muyu.cloud.mart.utils + * @Project:cloud-mart + * @name:IPLocation + * @Date:2024/8/23 20:54 + */ +public class IPLocation { + public static StringBuffer queryMobileLocation(String ip){ + String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955"; + String apiUrl = "http://apis.juhe.cn/ip/ipNewV3"; + HashMap map = new HashMap<>(); + map.put("key", apiKey); + map.put("ip", ip); + try { + URL url = new URL(String.format(apiUrl + "?" + params(map))); + BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream())); + String inputLine; + StringBuffer stringBuffer = new StringBuffer(); + while ((inputLine = in.readLine()) != null){ + stringBuffer.append(inputLine); + } + in.close(); + System.out.println(stringBuffer); + return stringBuffer; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + public static String params(Map 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("&")); + } +} diff --git a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/config/MobileLocation.java b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/MobileLocation.java similarity index 89% rename from cloud-mart-server/src/main/java/com/muyu/cloud/mart/config/MobileLocation.java rename to cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/MobileLocation.java index 5f7cdc2..5d34169 100644 --- a/cloud-mart-server/src/main/java/com/muyu/cloud/mart/config/MobileLocation.java +++ b/cloud-mart-server/src/main/java/com/muyu/cloud/mart/utils/MobileLocation.java @@ -1,9 +1,10 @@ -package com.muyu.cloud.mart.config; +package com.muyu.cloud.mart.utils; +import com.muyu.domain.PhonePlace; import net.sf.json.JSONObject; import java.io.*; @@ -24,21 +25,18 @@ public class MobileLocation { // 接口请求Key public static String API_KEY = "8e5493da8edee8e42831574b9a2c3492"; - public static void main(String[] args) { - String mobile = "15150771323"; - queryMobileLocation(mobile); - } /** * 根据手机号码/手机号码前7位查询号码归属地 * @param mobile */ - public static void queryMobileLocation(String mobile) + public static PhonePlace queryMobileLocation(String tel) { Map params = new HashMap<>();//组合参数 - params.put("phone", mobile); + params.put("phone", tel); params.put("key", API_KEY); String queryParams = urlencode(params); + PhonePlace phonePlace = new PhonePlace(); String response = doGet(API_URL, queryParams); try { @@ -49,6 +47,12 @@ public class MobileLocation { JSONObject result = jsonObject.getJSONObject("result"); + phonePlace.setProvince(result.getString("province")); + phonePlace.setCity(result.getString("city")); + phonePlace.setAreacode(result.getString("areacode")); + phonePlace.setZip(result.getString("zip")); + phonePlace.setCompany(result.getString("company")); + System.out.printf("省份:%s%n", result.getString("province")); System.out.printf("城市:%s%n", result.getString("city")); System.out.printf("区号:%s%n", result.getString("areacode")); @@ -61,6 +65,7 @@ public class MobileLocation { } catch (Exception e) { e.printStackTrace(); } + return phonePlace; } /**