IP查询归属地

pull/1/head
chaiyapeng 2024-08-23 21:00:12 +08:00
parent 6d4f0c67b9
commit bc97dc7f89
6 changed files with 103 additions and 37 deletions

View File

@ -0,0 +1,25 @@
package com.muyu.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.domain
* @Projectcloud-mart
* @namePhonePlace
* @Date2024/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;
}

View File

@ -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

View File

@ -16,7 +16,7 @@ import java.util.List;
public interface MarketService extends IService<Market> {
List<Market> findMarketList();
void getPhonePlace(String tel);
Result getPhonePlace(String tel);
Result getIpPlace(String ip);

View File

@ -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<MarketMapper, Market> 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<MarketMapper, Market> impleme
*/
@Override
public Result getIpPlace(String ip) {
String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955";
String apiUrl = "http://apis.juhe.cn/ip/ipNewV3";
HashMap<String, String> 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);
StringBuffer stringBuffer = IPLocation.queryMobileLocation(ip);
return Result.success(stringBuffer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*

View File

@ -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;
/**
* @Authorchaiyapeng
* @Packagecom.muyu.cloud.mart.utils
* @Projectcloud-mart
* @nameIPLocation
* @Date2024/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<String, String> 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<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

@ -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<String, Object> 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;
}
/**