diff --git a/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.java b/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.java deleted file mode 100644 index 403acb8..0000000 --- a/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.java +++ /dev/null @@ -1,99 +0,0 @@ -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; - -/** - * 电话号码工具 - *

- * - * @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 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; - - } - -} \ No newline at end of file diff --git a/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.kt b/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.kt new file mode 100644 index 0000000..dc2adab --- /dev/null +++ b/fw-shirasawa-common/src/main/java/cn/fw/shirasawa/common/utils/MobileUtil.kt @@ -0,0 +1,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 = 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)" + } +} diff --git a/fw-shirasawa-rpc/src/main/java/cn/fw/shirasawa/rpc/member/MemberRpcService.java b/fw-shirasawa-rpc/src/main/java/cn/fw/shirasawa/rpc/member/MemberRpcService.java index 19c6908..1870fb8 100644 --- a/fw-shirasawa-rpc/src/main/java/cn/fw/shirasawa/rpc/member/MemberRpcService.java +++ b/fw-shirasawa-rpc/src/main/java/cn/fw/shirasawa/rpc/member/MemberRpcService.java @@ -6,14 +6,17 @@ import cn.fw.member.sdk.api.MemberApi; import cn.fw.member.sdk.vo.BatchUserParam; import cn.fw.member.sdk.vo.MobileLocation; import cn.fw.member.sdk.vo.UserBaseInfoVO; +import cn.fw.shirasawa.common.utils.MobileUtilKt; import cn.fw.shirasawa.rpc.AbsBaseRpcService; import cn.fw.shirasawa.rpc.member.dto.MemberUserDTO; import com.alibaba.fastjson.JSONObject; +import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -148,14 +151,19 @@ public class MemberRpcService extends AbsBaseRpcService { * @param mobile * @return */ + @Cacheable(cacheNames = "mobile:attribution", key = "#mobile", unless = "#result.isEmpty()") public String attribution(final String mobile) { if (StringUtils.isBlank(mobile)) { - return null; + return ""; } try { + String attribution = MobileUtilKt.attribution(mobile); + if (StringUtils.isNotBlank(attribution)) { + return attribution; + } Message msg = functionApi.queryMobileLocation(mobile); if (!msg.isSuccess()) { - return null; + return ""; } MobileLocation data = msg.getData(); if (data != null) { @@ -165,11 +173,11 @@ public class MemberRpcService extends AbsBaseRpcService { } } catch (Exception ignored) { } - return null; + return ""; } @Override - protected String getKeyPrefix() { + public String getKeyPrefix() { return this.keyPrefix; } } diff --git a/fw-shirasawa-server/src/main/resources/application.yml b/fw-shirasawa-server/src/main/resources/application.yml index 9ffb4be..2486987 100644 --- a/fw-shirasawa-server/src/main/resources/application.yml +++ b/fw-shirasawa-server/src/main/resources/application.yml @@ -13,6 +13,11 @@ spring: key-prefix: 'shirasawa:locker:' custom: global-prefix: 'shirasawa' + global: + ttl: 12h + cache: + - ttl: 2h + name: mobile:attribution cloud: nacos: diff --git a/fw-shirasawa-service/src/main/java/cn/fw/shirasawa/service/bus/follow/strategy/AbstractFollowStrategy.java b/fw-shirasawa-service/src/main/java/cn/fw/shirasawa/service/bus/follow/strategy/AbstractFollowStrategy.java index 1a9deb5..6deaebb 100644 --- a/fw-shirasawa-service/src/main/java/cn/fw/shirasawa/service/bus/follow/strategy/AbstractFollowStrategy.java +++ b/fw-shirasawa-service/src/main/java/cn/fw/shirasawa/service/bus/follow/strategy/AbstractFollowStrategy.java @@ -1,7 +1,7 @@ package cn.fw.shirasawa.service.bus.follow.strategy; import cn.fw.shirasawa.common.utils.DateUtil; -import cn.fw.shirasawa.common.utils.MobileUtil; +import cn.fw.shirasawa.common.utils.MobileUtilKt; import cn.fw.shirasawa.common.utils.StringUtils; import cn.fw.shirasawa.domain.db.OriginalData; import cn.fw.shirasawa.domain.db.follow.FollowRecord; @@ -636,7 +636,7 @@ public abstract class AbstractFollowStrategy implements FollowStrategy { vo.setMobile(record.getContacts()); vo.setVin(cluePool.getFrameNo()); vo.setRealMobile(record.getContacts()); - vo.setRegion(MobileUtil.attribution(record.getContacts())); + vo.setRegion(memberRpcService.attribution(record.getContacts())); //默认已经打过电话 Boolean hadCall = Boolean.TRUE;