MobileUtil.java
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cn.fw.shirasawa.common.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 电话号码工具
* <p>
*
* @author kurisu
*/
@Slf4j
public class MobileUtil {
private final static String JUHE_API = "http://apis.juhe.cn/mobile/get";
private final static String APP_KEY = "3ae2492bf1b1a382943924e1a1a25e4d";
private final static ObjectMapper objectMapper = new ObjectMapper();
public static String attribution(String mobile) {
if (StringUtils.isEmpty(mobile)) {
return "";
}
final Map<String, String> param = new HashMap<>();
param.put("phone", mobile);
param.put("key", APP_KEY);
final String resultString = HttpClientUtil.doGet(JUHE_API, param);
try {
final JuHeResult juHeResult = objectMapper.readValue(resultString, JuHeResult.class);
final int errorCode = Integer.valueOf(juHeResult.getError_code());
final String reason = juHeResult.getReason();
if (errorCode != 0) {
log.error("调用聚合API获取手机号码:{} 归属地异常:{}", mobile, errorCode + ":" + reason);
return "";
}
final String province = juHeResult.getResult().getProvince();
final String city = juHeResult.getResult().getCity();
return province.concat(" ").concat(city);
} catch (IOException e) {
e.printStackTrace();
}
return "未知";
}
@Data
public static class JuHeResult {
/**
* 错误码
*/
private String error_code;
/**
* 结果
*/
private String resultcode;
/**
* 错误原因
*/
private String reason;
/**
* 结果
*/
private Result result;
}
@Data
public static class Result {
/**
* 省
*/
private String province;
/**
* 市
*/
private String city;
/**
* 区划编码
*/
private String areacode;
/**
* 邮编
*/
private String zip;
/**
* 公司
*/
private String company;
/**
*
*/
private String card;
}
}