MobileUtil.kt
1.93 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
package cn.fw.shirasawa.common.utils
import cn.fw.common.annotation.NoArg
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.IOException
/**
*
* 电话号码工具类
*
* @className : MobileUtil
* @description : 电话号码工具类
* @author : kurisu
* @date : 2023-03-14 16:49
* @version : 1.0
*/
private const val API_URL = "http://phone.kinako.com.cn/query"
private val objectMapper = ObjectMapper()
private val log = LoggerFactory.getLogger("MobileUtil");
fun attribution(mobile: String): String {
if (StringUtils.isEmpty(mobile)) {
return ""
}
val param: MutableMap<String, String> = HashMap()
param["phone"] = mobile
val resultString = HttpClientUtil.doGet(API_URL, param)
try {
val phoneResult = objectMapper.readValue(resultString, PhoneResult::class.java)
val reason = phoneResult.result
if (phoneResult.code != 0) {
log.error("调用API获取手机号码:{} 归属地异常:{}", mobile, phoneResult.code.toString() + ":" + reason)
return ""
}
log.info("手机号码:{} 归属地信息:{}", mobile, phoneResult.toString())
val province = phoneResult.data?.province ?: ""
val city = phoneResult.data?.city ?: ""
return "$province $city"
} catch (e: IOException) {
e.printStackTrace()
}
return ""
}
@NoArg
data class PhoneResult(val code: Int, val result: String, val success: Boolean, val data: Result?) {
override fun toString(): String {
return "PhoneResult(code=$code, result='$result', success=$success, data=$data)"
}
}
@NoArg
data class Result(
val province: String?,
val city: String?,
val area_code: String?,
val zip_code: String?,
val card_type: String?
) {
override fun toString(): String {
return "Result(province=$province, city=$city, area_code=$area_code, zip_code=$zip_code, card_type=$card_type)"
}
}