From af29eb818cf058f5fc022a0719f87e2de7a512af Mon Sep 17 00:00:00 2001 From: heqijun Date: Sun, 8 Jun 2025 17:51:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=AD=96=E7=95=A5=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=8F=B7=E6=AE=B5=E8=A1=A5=E5=85=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constant/CacheConstant.java | 3 + .../common/constant/RabbitMQConstant.java | 5 + .../strategy/config/RestTemplateConfig.java | 21 ++++ .../strategy/mq/PreSendListener.java | 2 +- .../mashibing/strategy/pojo/PhasePojo.java | 31 ++++++ .../impl/PhaseStrategyFilter.java | 32 +++++- .../mashibing/strategy/utils/PhaseUtil.java | 101 ++++++++++++++++++ .../test/mapper/MobileAreaMapperTest.java | 2 +- 8 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/config/RestTemplateConfig.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/pojo/PhasePojo.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/utils/PhaseUtil.java diff --git a/beacon-common/src/main/java/com/mashibing/common/constant/CacheConstant.java b/beacon-common/src/main/java/com/mashibing/common/constant/CacheConstant.java index 33991cf..11ed65a 100644 --- a/beacon-common/src/main/java/com/mashibing/common/constant/CacheConstant.java +++ b/beacon-common/src/main/java/com/mashibing/common/constant/CacheConstant.java @@ -25,4 +25,7 @@ public class CacheConstant { @Description("客户信息:ip白名单,ipAddress") public static final String CLIENT_BUSINESS_IPADDRESS = "ipAddress"; + + @Description("号段前缀") + public static final String PHASE = "phase:"; } diff --git a/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstant.java b/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstant.java index 1a0f380..4389c6d 100644 --- a/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstant.java +++ b/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstant.java @@ -12,4 +12,9 @@ public interface RabbitMQConstant { * 接口模块发送消息到策略模块的队列名称 */ String SMS_PRE_SEND = "sms_pre_send_topic"; + + /** + * 策略模块发送【手机号归属地和运营商】到后台管理模块的队列名称 + */ + String MOBILE_AREA_OPERATOR = "mobile_area_operator_topic"; } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/config/RestTemplateConfig.java b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RestTemplateConfig.java new file mode 100644 index 0000000..5a0629a --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RestTemplateConfig.java @@ -0,0 +1,21 @@ +package com.mashibing.strategy.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @author heqijun + * @ClassName: RestTemplateConfig + * @Description: TODO(这里用一句话描述这个类的作用) + * @date 2025/6/8 16:33 + */ + +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/mq/PreSendListener.java b/beacon-strategy/src/main/java/com/mashibing/strategy/mq/PreSendListener.java index 85f46d5..fe32c83 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/mq/PreSendListener.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/mq/PreSendListener.java @@ -35,8 +35,8 @@ public class PreSendListener { log.info("【策略模块-接收消息】消息消费完毕,手动ack"); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { - e.printStackTrace(); log.info("【策略模块-接收消息】消息消费失败!!!!!!!"); + e.printStackTrace(); } } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/pojo/PhasePojo.java b/beacon-strategy/src/main/java/com/mashibing/strategy/pojo/PhasePojo.java new file mode 100644 index 0000000..755e5f2 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/pojo/PhasePojo.java @@ -0,0 +1,31 @@ +package com.mashibing.strategy.pojo; + +import com.mashibing.common.annotation.Description; +import lombok.Data; + +/** + * @author heqijun + * @ClassName: PhasePOJO + * @Description: TODO(这里用一句话描述这个类的作用) + * @date 2025/6/8 16:39 + */ + +@Data +public class PhasePojo { + + @Description("目标手机号的运营商id") + private Integer operatorId; + + @Description("目标手机号的运营商na") + private String operatorNa; + + @Description("目标手机号的归属地 哈尔滨, 绥化~") + private String area; + + public PhasePojo unknown() { + this.operatorId = 0; + this.operatorNa = "未知"; + this.area = "未知 未知"; + return this; + } +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/service/strategyfilter/impl/PhaseStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/service/strategyfilter/impl/PhaseStrategyFilter.java index 14512d2..f586271 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/service/strategyfilter/impl/PhaseStrategyFilter.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/service/strategyfilter/impl/PhaseStrategyFilter.java @@ -1,9 +1,16 @@ package com.mashibing.strategy.service.strategyfilter.impl; +import com.mashibing.common.constant.CacheConstant; import com.mashibing.common.pojo.StandardSubmit; +import com.mashibing.strategy.feignclient.CacheClient; +import com.mashibing.strategy.pojo.PhasePojo; import com.mashibing.strategy.service.strategyfilter.StrategyFilter; +import com.mashibing.strategy.utils.PhaseUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; /** * @author heqijun @@ -16,11 +23,32 @@ import org.springframework.stereotype.Service; @Service(value = "phase") public class PhaseStrategyFilter implements StrategyFilter { + @Autowired + private CacheClient cacheClient; + @Autowired + private RestTemplate restTemplate; @Override public void strategy(StandardSubmit standardSubmit) { - log.info("【策略模块-号段补全】。。。"); - + log.info("【策略模块-号段补全】开始==============================="); + String mobile7 = standardSubmit.getMobile().substring(0, 7); + //缓存中查询号段。如果查询不到,则在三方接口中查询 + String s = cacheClient.get(CacheConstant.PHASE + mobile7); + PhasePojo phasePojo = new PhasePojo(); + if (StringUtils.isBlank(s)) { + log.info("【策略模块-号段补全】缓存中未查询到信息,需要三方查询。。。"); + phasePojo = PhaseUtil.phaseMobile360(restTemplate, mobile7); + log.info("【策略模块-号段补全】三方查询结果:{}", phasePojo); + } else { + log.info("【策略模块-号段补全】缓存中查询到信息:{}", s); + String[] split = s.split(","); + phasePojo.setArea(split[0]); + phasePojo.setOperatorNa(split[1]); + } + //查询到号段之后,补全standardSubmit中运营商信息 + standardSubmit.setArea(phasePojo.getArea()); + standardSubmit.setOperatorId(PhaseUtil.getOperatorId(phasePojo.getOperatorNa())); + log.info("【策略模块-号段补全】结束==============================="); } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/utils/PhaseUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/utils/PhaseUtil.java new file mode 100644 index 0000000..e701b7e --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/utils/PhaseUtil.java @@ -0,0 +1,101 @@ +package com.mashibing.strategy.utils; + +import com.alibaba.nacos.common.utils.MapUtils; +import com.alibaba.nacos.common.utils.StringUtils; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mashibing.common.annotation.Description; +import com.mashibing.strategy.pojo.PhasePojo; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +/** + * @author heqijun + * @ClassName: PhaseUtil + * @Description: 调三方接口号段补全工具类 + * @date 2025/6/8 16:24 + */ + +@Slf4j +public class PhaseUtil { + + private final static String URL1 = "https://cx.shouji.360.cn/phonearea.php?number="; + + public static PhasePojo phaseMobile360(RestTemplate restTemplate, String mobile7) { + Map map; + //调360的接口获取结果 + //坑爹接口,返回的居然是text不是json + String s = restTemplate.getForObject(URL1 + mobile7, String.class); + + //String转map + ObjectMapper objectMapper = new ObjectMapper(); + try { + map = objectMapper.readValue(s, Map.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + if (MapUtils.isEmpty(map)) { + return new PhasePojo().unknown(); + } + + Phase360 phase360 = objectMapper.convertValue(map.get("data"), Phase360.class); + if (phase360 == null||StringUtils.isBlank(phase360.getProvince())) { + return new PhasePojo().unknown(); + } + + // 处理直辖市 + if (StringUtils.isBlank(phase360.getCity())) { + phase360.setCity(phase360.getProvince()); + } + + PhasePojo phasePojo = new PhasePojo(); + phasePojo.setArea(phase360.getProvince() + " " + phase360.getCity()); + phasePojo.setOperatorNa(phase360.getSp()); + return phasePojo; + } + + public static Integer getOperatorId(String operatorNa) { + if ("移动".equals(operatorNa)) { + return 1; + } + if ("联通".equals(operatorNa)) { + return 2; + } + if ("电信".equals(operatorNa)) { + return 3; + } + return 0; + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + public static class Phase360 { + + @Description("运营商id") + private int operatorId; + + @Description("运营商名称") + private String sp; + + @Description("号码归属地省") + private String province; + + @Description("号码归属地市") + private String city; + + public Phase360 unknuwn() { + this.operatorId = 0; + this.sp = "未知"; + this.province = "未知"; + this.city = "未知"; + return this; + } + } +} diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java index 6e967bd..7b2d510 100644 --- a/beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java @@ -34,7 +34,7 @@ class MobileAreaMapperTest { Map map = new HashMap<>(allMobileArea.size()); startTime = System.currentTimeMillis(); allMobileArea.forEach(mobileArea -> { - map.put(mobileArea.getMobileNumber(), + map.put("phase:" + mobileArea.getMobileNumber(), mobileArea.getMobileArea() + "," + mobileArea.getMobileType()); }); endTime = System.currentTimeMillis();