From ce800117f6c24e92b9fe858889237fbdd7625cca Mon Sep 17 00:00:00 2001 From: DanielDeng Date: Mon, 25 Mar 2024 02:06:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=83=BD=E7=81=AB=E4=BA=91=E9=95=BF=E6=A4=BF?= =?UTF-8?q?=E8=A1=973.25=202:05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/encodings.xml | 2 + .idea/uiDesigner.xml | 124 +++++++++++++++ .../api/controller/SmsController.java | 2 + beacon-api/src/main/resources/bootstrap.yml | 4 +- .../cache/controller/CacheController.java | 115 ++++++++++++-- .../cache/controller/TestController.java | 17 +-- beacon-cache/src/main/resources/bootstrap.yml | 4 +- .../common/constant/CacheConstant.java | 22 +++ .../common/constant/RabbitMQConstants.java | 15 ++ .../common/constant/SmsConstant.java | 28 ++++ .../common/enums/ExceptionEnums.java | 9 +- .../common/enums/MobileOperatorEnum.java | 31 ++++ .../common/exception/StrategyException.java | 27 ++++ .../common/model/StandardReport.java | 70 +++++++++ .../common/model/StandardSubmit.java | 36 ++++- .../mashibing/common/util/OperatorUtil.java | 28 ++++ beacon-strategy/pom.xml | 13 ++ .../strategy/StrategyStarterApp.java | 7 + .../strategy/client/BeaconCacheClient.java | 47 +++++- .../strategy/config/RabbitMQConfig.java | 39 +++++ .../strategy/config/RabbitTemplateConfig.java | 60 ++++++++ .../impl/BlackClientStrategyFilter.java | 52 +++++++ .../impl/BlackGlobalStrategyFilter.java | 51 +++++++ .../filter/impl/BlackStrategyFilter.java | 19 --- .../impl/DirtyWordDFAStrategyFilter.java | 40 +++++ .../DirtyWordHutoolDFAStrategyFilter.java | 67 ++++++++ .../filter/impl/DirtyWordStrategyFilter.java | 43 +++++- .../filter/impl/FeeStrategyFilter.java | 55 +++++++ .../impl/LimitOneHourStrategyFilter.java | 101 ++++++++++++ .../impl/LimitOneMinuteStrategyFilter.java | 85 +++++++++++ .../filter/impl/PhaseStrategyFilter.java | 62 +++++++- .../filter/impl/RouteStrategyFilter.java | 109 ++++++++++++- .../filter/impl/TransferStrategyFilter.java | 47 ++++++ .../strategy/mq/PreSendListener.java | 8 +- .../strategy/util/ChannelTransferUtil.java | 17 +++ .../strategy/util/ClientBalanceUtil.java | 18 +++ .../com/mashibing/strategy/util/DFAUtil.java | 144 ++++++++++++++++++ .../strategy/util/ErrorSendMsgUtil.java | 65 ++++++++ .../strategy/util/HutoolDFAUtil.java | 35 +++++ .../strategy/util/MobileOperatorUtil.java | 61 ++++++++ .../mashibing/strategy/util/SpringUtil.java | 29 ++++ .../src/main/resources/bootstrap.yml | 4 +- .../mashibing/test/client/CacheClient.java | 7 + .../com/mashibing/test/entity/Channel.java | 125 +++++++++++++++ .../mashibing/test/entity/ClientChannel.java | 50 ++++++ .../com/mashibing/test/entity/MobileArea.java | 36 +++++ .../mashibing/test/entity/MobileBlack.java | 30 ++++ .../mashibing/test/entity/MobileTransfer.java | 28 ++++ .../mashibing/test/mapper/ChannelMapper.java | 16 ++ .../test/mapper/ClientChannelMapper.java | 17 +++ .../test/mapper/MobileAreaMapper.java | 18 +++ .../test/mapper/MobileBlackMapper.java | 17 +++ .../test/mapper/MobileDirtyWordMapper.java | 16 ++ .../test/mapper/MobileTransferMapper.java | 17 +++ .../src/main/resources/application.yml | 4 +- .../test/mapper/ChannelMapperTest.java | 43 ++++++ .../test/mapper/ClientChannelMapperTest.java | 41 +++++ .../test/mapper/MobileAreaMapperTest.java | 40 +++++ .../test/mapper/MobileBlackMapperTest.java | 41 +++++ .../mapper/MobileDirtyWordMapperTest.java | 34 +++++ .../test/mapper/MobileTransferMapperTest.java | 36 +++++ 61 files changed, 2355 insertions(+), 73 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 beacon-common/src/main/java/com/mashibing/common/constant/SmsConstant.java create mode 100644 beacon-common/src/main/java/com/mashibing/common/enums/MobileOperatorEnum.java create mode 100644 beacon-common/src/main/java/com/mashibing/common/exception/StrategyException.java create mode 100644 beacon-common/src/main/java/com/mashibing/common/model/StandardReport.java create mode 100644 beacon-common/src/main/java/com/mashibing/common/util/OperatorUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitMQConfig.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitTemplateConfig.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackClientStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackGlobalStrategyFilter.java delete mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordDFAStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordHutoolDFAStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/FeeStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneHourStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneMinuteStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/TransferStrategyFilter.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/ChannelTransferUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/ClientBalanceUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/DFAUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/ErrorSendMsgUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/HutoolDFAUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/MobileOperatorUtil.java create mode 100644 beacon-strategy/src/main/java/com/mashibing/strategy/util/SpringUtil.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/entity/Channel.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/entity/ClientChannel.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/entity/MobileArea.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/entity/MobileBlack.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/entity/MobileTransfer.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/ChannelMapper.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/ClientChannelMapper.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/MobileAreaMapper.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/MobileBlackMapper.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/MobileDirtyWordMapper.java create mode 100644 beacon-test/src/main/java/com/mashibing/test/mapper/MobileTransferMapper.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/ChannelMapperTest.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/ClientChannelMapperTest.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/MobileBlackMapperTest.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/MobileDirtyWordMapperTest.java create mode 100644 beacon-test/src/test/java/com/mashibing/test/mapper/MobileTransferMapperTest.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 7e38af7..2014ea6 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -7,5 +7,7 @@ + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java index ff219ce..df5db3e 100644 --- a/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java +++ b/beacon-api/src/main/java/com/mashibing/api/controller/SmsController.java @@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.time.LocalDateTime; /** * @author dch @@ -88,6 +89,7 @@ public class SmsController { //============================基于雪花算法生成唯一标识sequenceId================================= submit.setSequenceId(snowFlakeUtil.nextId()); + submit.setSendTime(LocalDateTime.now()); //=========================发送到MQ,交给策略模块处理========================================= rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_PRE_SEND,submit,new CorrelationData(submit.getSequenceId().toString())); diff --git a/beacon-api/src/main/resources/bootstrap.yml b/beacon-api/src/main/resources/bootstrap.yml index 2c2e369..66a912a 100644 --- a/beacon-api/src/main/resources/bootstrap.yml +++ b/beacon-api/src/main/resources/bootstrap.yml @@ -9,9 +9,9 @@ spring: cloud: nacos: discovery: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 # nacos配置中心地址: config: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 file-extension: yml # beacon-api-dev.yml diff --git a/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java index 282a919..59bc159 100644 --- a/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java +++ b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java @@ -3,6 +3,7 @@ package com.mashibing.cache.controller; import com.msb.framework.redis.RedisClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.ZSetOperations; import org.springframework.web.bind.annotation.*; import java.util.Map; @@ -18,47 +19,131 @@ public class CacheController { @Autowired private RedisClient redisClient; + @PostMapping(value = "/cache/hmset/{key}") - public void hmset(@PathVariable(value = "key")String key, @RequestBody Map map){ - log.info("【缓存模块】 hmset方法,存储key = {},存储value = {}",key,map); - redisClient.hSet(key,map); + public void hmset(@PathVariable(value = "key") String key, @RequestBody Map map) { + log.info("【缓存模块】 hmset方法,存储key = {},存储value = {}", key, map); + redisClient.hSet(key, map); } @PostMapping(value = "/cache/set/{key}") - public void set(@PathVariable(value = "key")String key, @RequestParam(value = "value")Object value){ - log.info("【缓存模块】 set方法,存储key = {},存储value = {}",key,value); - redisClient.set(key,value); + public void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") Object value) { + log.info("【缓存模块】 set方法,存储key = {},存储value = {}", key, value); + redisClient.set(key, value); } @PostMapping(value = "/cache/sadd/{key}") - public void sadd(@PathVariable(value = "key")String key, @RequestBody Map... value){ + public void sadd(@PathVariable(value = "key") String key, @RequestBody Map... value) { log.info("【缓存模块】 sadd方法,存储key = {},存储value = {}", key, value); - redisClient.sAdd(key,value); + redisClient.sAdd(key, value); + } + + @PostMapping(value = "/cache/saddstr/{key}") + public void saddStr(@PathVariable(value = "key") String key, @RequestBody String... value) { + log.info("【缓存模块】 saddStr方法,存储key = {},存储value = {}", key, value); + redisClient.sAdd(key, value); } @GetMapping("/cache/hgetall/{key}") - public Map hGetAll(@PathVariable(value = "key")String key){ + public Map hGetAll(@PathVariable(value = "key") String key) { log.info("【缓存模块】 hGetAll方法,获取key ={} 的数据", key); Map value = redisClient.hGetAll(key); - log.info("【缓存模块】 hGetAll方法,获取key ={} 的数据 value = {}", key,value); + log.info("【缓存模块】 hGetAll方法,获取key ={} 的数据 value = {}", key, value); return value; } @GetMapping("/cache/hget/{key}/{field}") - public Object hget(@PathVariable(value = "key")String key,@PathVariable(value = "field")String field){ - log.info("【缓存模块】 hget方法,获取key ={},field = {}的数据", key,field); + public Object hget(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field) { + log.info("【缓存模块】 hget方法,获取key ={},field = {}的数据", key, field); Object value = redisClient.hGet(key, field); - log.info("【缓存模块】 hget,获取key ={},field = {} 的数据 value = {}", key,field,value); + log.info("【缓存模块】 hget,获取key ={},field = {} 的数据 value = {}", key, field, value); return value; } @GetMapping("/cache/smember/{key}") - public Set smember(@PathVariable(value = "key")String key){ + public Set smember(@PathVariable(value = "key") String key) { log.info("【缓存模块】 smember方法,获取key ={}的数据", key); Set values = redisClient.sMembers(key); - log.info("【缓存模块】 smember方法,获取key ={} 的数据 value = {}", key,values); + log.info("【缓存模块】 smember方法,获取key ={} 的数据 value = {}", key, values); return values; } + @GetMapping("/cache/smemberMap/{key}") + public Set smemberMap(@PathVariable(value = "key") String key) { + log.info("【缓存模块】 smemberMap方法,获取key ={}的数据", key); + Set values = redisClient.sMembers(key); + log.info("【缓存模块】 smemberMap方法,获取key ={} 的数据 value = {}", key, values); + return values; + } + + @PostMapping("/cache/pipeline/string") + public void pipelineString(@RequestBody Map map) { + log.info("【缓存模块】 pipelineString,获取到存储的数据,map的长度 ={}的数据", map.size()); + redisClient.pipelined(operations -> { + for (Map.Entry entry : map.entrySet()) { + operations.opsForValue().set(entry.getKey(), entry.getValue()); + } + }); + } + + @GetMapping("/cache/get/{key}") + public Object getString(@PathVariable(value = "key") String key) { + log.info("【缓存模块】 get方法,获取key ={}", key); + Object value = redisClient.get(key); + log.info("【缓存模块】 get方法,查询key ={} 的数据 value = {}", key, value); + return value; + } + + @PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}") + public Set sinterStr(@PathVariable(value = "key") String key, @PathVariable String sinterKey, @RequestBody String... value) { + log.info("【缓存模块】 sinterStr的交集方法,存储key = {},sinterKey = {},存储value = {}", key, sinterKey, value); + //1、 存储数据到set集合 + redisClient.sAdd(key, value); + //2、 需要将key和sinterKey做交集操作,并拿到返回的set(RedisClient没提供(已提供),自己写的~~) + Set result = redisClient.sIntersect(key, sinterKey); + //3、 将key删除 + redisClient.delete(key); + //4、 返回交集结果 + return result; + } + + + @PostMapping(value = "/cache/zadd/{key}/{score}/{member}") + public Boolean zadd(@PathVariable(value = "key") String key, + @PathVariable(value = "score") Long score, + @PathVariable(value = "member") Object member) { + log.info("【缓存模块】 zadd方法,存储key = {},存储score = {},存储value = {}", key, score, member); + Boolean result = redisClient.zAdd(key, member, score); + return result; + } + + @GetMapping(value = "/cache/zrangebyscorecount/{key}/{start}/{end}") + public Integer zRangeByScoreCount(@PathVariable(value = "key") String key, + @PathVariable(value = "start") Double start, + @PathVariable(value = "end") Double end) { + log.info("【缓存模块】 zRangeByScoreCount方法,查询key = {},start = {},end = {}", key, start, end); + Set> values = redisClient.zRangeByScore(key, start, end); +// redisTemplate.opsForZSet().rangeByScoreWithScores(key, start, end); + if (values != null) { + return values.size(); + } + return 0; + } + + @DeleteMapping(value = "/cache/zremove/{key}/{member}") + public void zRemove(@PathVariable(value = "key") String key, @PathVariable(value = "member") String member) { + log.info("【缓存模块】 zRemove方法,删除key = {},member = {}", key, member); + redisClient.zRemove(key, member); + } + + @PostMapping(value = "/cache/hincrby/{key}/{field}/{delta}") + public Long hIncrBy(@PathVariable(value = "key") String key, + @PathVariable(value = "field") String field, + @PathVariable(value = "delta") Long delta) { + log.info("【缓存模块】 hIncrBy方法,自增 key = {},field = {},number = {}", key, field, delta); + Long result = redisClient.hIncrementBy(key, field, delta); + log.info("【缓存模块】 hIncrBy方法,自增 key = {},field = {},number = {},剩余数值为 = {}", key, field, delta, result); + return result; + } } diff --git a/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java b/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java index fe88bf3..ee5d97d 100644 --- a/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java +++ b/beacon-cache/src/main/java/com/mashibing/cache/controller/TestController.java @@ -22,26 +22,19 @@ public class TestController { // 写测试 hash结构 @PostMapping("/test/set/{key}") - public String set(@PathVariable String key, @RequestBody Map map){ + public String set(@PathVariable String key, @RequestBody Map map) { // redisTemplate.opsForHash().putAll(key,map); - redisClient.hSet(key,map); + redisClient.hSet(key, map); return "ok"; } + // 读测试 @GetMapping("/test/get/{key}") - public Map get(@PathVariable String key){ + public Map get(@PathVariable String key) { // Map result = redisTemplate.opsForHash().entries(key); Map result = redisClient.hGetAll(key); return result; } - @PostMapping("/cache/pipeline/string") - public void pipelineString(@RequestBody Map map){ - log.info("【缓存模块】 pipelineString,获取到存储的数据,map的长度 ={}的数据", map.size()); - redisClient.pipelined(operations -> { - for (Map.Entry entry : map.entrySet()) { - operations.opsForValue().set(entry.getKey(),entry.getValue()); - } - }); - } + } diff --git a/beacon-cache/src/main/resources/bootstrap.yml b/beacon-cache/src/main/resources/bootstrap.yml index 4b055d3..4b85102 100644 --- a/beacon-cache/src/main/resources/bootstrap.yml +++ b/beacon-cache/src/main/resources/bootstrap.yml @@ -9,9 +9,9 @@ spring: cloud: nacos: discovery: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 # nacos配置中心地址: config: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 file-extension: yml # beacon-cache-dev.yml 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 fd9e347..94402cf 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 @@ -13,4 +13,26 @@ public interface CacheConstant { String CLIENT_TEMPLATE = "client_template:"; String CLIENT_BALANCE = "client_balance:"; + + String PHASE = "phase:"; + + String DIRTY_WORD = "dirty_word"; + + String BLACK = "black:"; + + String SEPARATE = ":"; + + String TRANSFER = "transfer:"; + + /** + * 分钟的限流规则 + */ + String LIMIT_MINUTES = "limit:minutes:"; + + String LIMIT_HOURS = "limit:hours:"; + + String CLIENT_CHANNEL= "client_channel:"; + + String CHANNEL = "channel:"; + } diff --git a/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstants.java b/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstants.java index 80ce65f..58d5b66 100644 --- a/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstants.java +++ b/beacon-common/src/main/java/com/mashibing/common/constant/RabbitMQConstants.java @@ -13,4 +13,19 @@ public interface RabbitMQConstants { * 接口模块发送消息到策略模块的队列名称 */ String SMS_PRE_SEND = "sms_pre_send_topic"; + + String MOBILE_AREA_OPERATIR = "mobile_area_operator_topic"; + + /** + * 写日志到ElasticSearch的队列 + * @return + */ + String SMS_WRITE_LOG = "sms_write_log_topic"; + + String SMS_PUSH_REPORT = "sms_push_report_topic"; + + /** + * 侧落寞快推送消息到短信网关模块的队列前缀名称 + */ + String SMS_GATEWAY = "sms_gateway_topic_"; } diff --git a/beacon-common/src/main/java/com/mashibing/common/constant/SmsConstant.java b/beacon-common/src/main/java/com/mashibing/common/constant/SmsConstant.java new file mode 100644 index 0000000..7bdc1bb --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/constant/SmsConstant.java @@ -0,0 +1,28 @@ +package com.mashibing.common.constant; + +/** + * @author dch + * @create 2024-03-23 23:28 + * 通用的一些常量 + */ +public interface SmsConstant { + /** + * 短信发送成功 + */ + int REPORT_SUCCESS = 1; + + /** + * 短信发送失败 + */ + + int REPORT_FAIL = 2; + + /** + * 验证码类型 + */ + int CODE_TYPE = 0; + + int NOTIFY_TYPE = 1; + + int MARKETING_TYPE = 2; +} diff --git a/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java b/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java index bdf8588..f04cac3 100644 --- a/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java +++ b/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java @@ -9,6 +9,7 @@ import lombok.Getter; @Getter public enum ExceptionEnums { + UNKNOWN_ERROR(-999,"未知错误"), ERROR_APIKEY(-1,"非法的apikey"), IP_NOT_WHITE(-2,"请求的ip不在白名单内"), ERROR_SIGN(-3,"无可用签名"), @@ -17,7 +18,13 @@ public enum ExceptionEnums { BALANCE_NOT_ENOUGH(-6,"手客户余额不足"), PARAMETER_ERROR(-10, "参数不合法!"), SNOWFLAKE_OUT_OF_RANGE(-11,"雪花算法的机器id或服务id超出最大范围!"), - SNOWFLAKE_TIME_BACK(-12,"雪花算法的服务器出现时间回拨问题") + SNOWFLAKE_TIME_BACK(-12,"雪花算法的服务器出现时间回拨问题"), + HAVE_DIRTY_WORD(-13,"当前短信内容中包含敏感词信息"), + BLACK_GLOBAL(-14,"当前手机号为平台黑名单"), + BLACK_CLIENT(-15,"当前手机号为用户黑名单"), + ONE_MINUTE_LIMIT(-16,"1分钟限流规则生效,无法发送短信"), + ONE_HOUR_LIMIT(-17,"1小时限流规则生效,无法发送短信"), + NO_CHANNEL(-18,"没有选择到合适的通道"), ; private Integer code; diff --git a/beacon-common/src/main/java/com/mashibing/common/enums/MobileOperatorEnum.java b/beacon-common/src/main/java/com/mashibing/common/enums/MobileOperatorEnum.java new file mode 100644 index 0000000..4b74459 --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/enums/MobileOperatorEnum.java @@ -0,0 +1,31 @@ +package com.mashibing.common.enums; + + +import lombok.Getter; + +/** + * @author dch + * @create 2024-03-22 22:17 + */ +@Getter +public enum MobileOperatorEnum { + + CHINA_MOBILE(1,"移动"), + CHINA_UNION(2,"联通"), + CHINA_TELECOM(3,"电信"), + UNKNOWN(4,"未知 未知,未知"), + ; + + private Integer operatorId; + + private String operatorName; + + + /** + * 通过运营商名称获取运营商id + */ + MobileOperatorEnum(Integer operatorId, String operatorName) { + this.operatorId = operatorId; + this.operatorName = operatorName; + } +} diff --git a/beacon-common/src/main/java/com/mashibing/common/exception/StrategyException.java b/beacon-common/src/main/java/com/mashibing/common/exception/StrategyException.java new file mode 100644 index 0000000..8b25184 --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/exception/StrategyException.java @@ -0,0 +1,27 @@ +package com.mashibing.common.exception; + +import com.mashibing.common.enums.ExceptionEnums; +import lombok.Getter; + +/** + * @author dch + * @create 2024-03-20 12:15 + * 策略模块的异常对象 + */ +@Getter +public class StrategyException extends RuntimeException { + + private Integer code; + + public StrategyException(String message, Integer code) { + super(message); + this.code = code; + } + + + public StrategyException(ExceptionEnums enums) { + super(enums.getMsg()); + this.code = enums.getCode(); + } + +} diff --git a/beacon-common/src/main/java/com/mashibing/common/model/StandardReport.java b/beacon-common/src/main/java/com/mashibing/common/model/StandardReport.java new file mode 100644 index 0000000..bead5df --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/model/StandardReport.java @@ -0,0 +1,70 @@ +package com.mashibing.common.model; + +/** + * @author dch + * @create 2024-03-24 17:05 + */ + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * 状态报告推送等操作时的类 + * + * @author zjw + * @description + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class StandardReport implements Serializable { + + /** + * 针对当前短信的唯一标识,雪花算法(保留) + */ + private Long sequenceId; + + /** + * 客户端ID,基于apikey查询缓存模块得到客户的ID + */ + private Long clientId; + + + /** + * 客户业务内的uid,客户请求传递的 + */ + private String uid; + + /** + * 目标手机号,客户请求传递的 + */ + private String mobile; + + /** + * 短信的发送时间,当前系统时间 + */ + private LocalDateTime sendTime; + + /** + * 短信的发送状态, 0-等待/发送ing,1-成功,2-失败 ,默认情况就是0 + */ + private int reportState; + + /** + * 短信发送失败的原因是什么,记录在当前属性 + */ + private String errorMsg; + + /** + * 回调的信息 + * + */ + private Integer isCallback; + + private String callbackUrl; + +} \ No newline at end of file diff --git a/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java b/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java index 0ebed95..b32bada 100644 --- a/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java +++ b/beacon-common/src/main/java/com/mashibing/common/model/StandardSubmit.java @@ -71,6 +71,7 @@ public class StandardSubmit implements Serializable { /** * 目标手机号的归属地区号(策略模块) 0451 0455 + * 三方查询不到,先不管 */ private Integer areaCode; @@ -94,21 +95,40 @@ public class StandardSubmit implements Serializable { */ private int reportState; + /** + * 短信发送失败的原因是什么,记录在当前属性 + */ + private String errorMsg; -/** - 真实的IP地址 -*/ + /** + * 真实的IP地址 + */ private String realIP; -/** 请求携带的apiKey封装*/ + /** + * 请求携带的apiKey封装 + */ private String apikey; -/** - 短信的类型 0-验证码短信 1-通知类 3-营销类 -*/ + /** + * 短信的类型 0-验证码短信 1-通知类 3-营销类 + */ private Integer state; - /**签名的id*/ + /** + * 签名的id + */ private Long signId; + + /** + * 是否携号转网 + */ + private Boolean isTransfer = false; + + /** + * 针对一小时限流规则存储的系统时间毫秒值 + */ + private Long oneHourLimitMilli; + } \ No newline at end of file diff --git a/beacon-common/src/main/java/com/mashibing/common/util/OperatorUtil.java b/beacon-common/src/main/java/com/mashibing/common/util/OperatorUtil.java new file mode 100644 index 0000000..8f0466d --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/util/OperatorUtil.java @@ -0,0 +1,28 @@ +package com.mashibing.common.util; + +import com.mashibing.common.enums.MobileOperatorEnum; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author dch + * @create 2024-03-22 22:22 + */ +public class OperatorUtil { + + private static Map operators = new HashMap<>(); + + static { + MobileOperatorEnum[] operatorEnums = MobileOperatorEnum.values(); + for (MobileOperatorEnum operatorEnum : operatorEnums){ + operators.put(operatorEnum.getOperatorName(),operatorEnum.getOperatorId()); + } + } + + public static Integer getOperatorIdByOperatorName(String operatorName){ + return operators.get(operatorName); + } + +} diff --git a/beacon-strategy/pom.xml b/beacon-strategy/pom.xml index a9410de..dac9ff2 100644 --- a/beacon-strategy/pom.xml +++ b/beacon-strategy/pom.xml @@ -52,6 +52,19 @@ spring-cloud-starter-openfeign + + + com.janeluo + ikanalyzer + 2012_u6 + + + + cn.hutool + hutool-dfa + 5.8.12 + + diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/StrategyStarterApp.java b/beacon-strategy/src/main/java/com/mashibing/strategy/StrategyStarterApp.java index 5028c96..26afd32 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/StrategyStarterApp.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/StrategyStarterApp.java @@ -4,6 +4,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; /** * @author dch @@ -18,4 +20,9 @@ public class StrategyStarterApp { SpringApplication.run(StrategyStarterApp.class,args); } + @Bean + public RestTemplate restTemplate(){ + return new RestTemplate(); + } + } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java b/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java index 94ced3a..b062893 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java @@ -1,8 +1,10 @@ package com.mashibing.strategy.client; import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; +import java.util.Set; /** * @author dch @@ -12,6 +14,43 @@ import org.springframework.web.bind.annotation.PathVariable; public interface BeaconCacheClient { @GetMapping("/cache/hget/{key}/{field}") - String hget(@PathVariable(value = "key")String key, @PathVariable(value = "field")String field); + String hget(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field); + + @GetMapping("/cache/hgetall/{key}") + Map hGetAll(@PathVariable(value = "key") String key); + + @GetMapping("/cache/hget/{key}/{field}") + Integer hgetInteger(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field); + + @GetMapping("/cache/get/{key}") + String getString(@PathVariable(value = "key") String key); + + @PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}") + Set sinterStr(@PathVariable(value = "key") String key, @PathVariable String sinterKey, @RequestBody String... value); + + @GetMapping("/cache/smember/{key}") + Set smember(@PathVariable(value = "key") String key); + + @GetMapping("/cache/smemberMap/{key}") + Set smemberMap(@PathVariable(value = "key") String key); + + @PostMapping(value = "/cache/zadd/{key}/{score}/{member}") + Boolean zadd(@PathVariable(value = "key")String key, + @PathVariable(value = "score")Long score, + @PathVariable(value = "member")Object member); + + @GetMapping(value = "/cache/zrangebyscorecount/{key}/{start}/{end}") + Integer zRangeByScoreCount(@PathVariable(value = "key") String key, + @PathVariable(value = "start") Double start, + @PathVariable(value = "end") Double end); + + @DeleteMapping(value = "/cache/zremove/{key}/{member}") + void zRemove(@PathVariable(value = "key") String key,@PathVariable(value = "member") String member); + + @PostMapping(value = "/cache/hincrby/{key}/{field}/{delta}") + Long hIncrBy(@PathVariable(value = "key") String key, + @PathVariable(value = "field") String field, + @PathVariable(value = "delta") Long delta); + -} +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitMQConfig.java b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitMQConfig.java new file mode 100644 index 0000000..69c2095 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitMQConfig.java @@ -0,0 +1,39 @@ +package com.mashibing.strategy.config; + +import com.mashibing.common.constant.RabbitMQConstants; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.QueueBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author dch + * @create 2024-03-22 23:16 + */ +@Configuration +public class RabbitMQConfig { + /** + * 策略模块发送手机号归属地&运营商到后台管理模块的队列 + * @return + */ + @Bean + public Queue preSendQueue(){ + return QueueBuilder.durable(RabbitMQConstants.MOBILE_AREA_OPERATIR).build(); + } + + /** + * 写日志的队列。 + * @return + */ + @Bean + public Queue writeLogQueue(){ + return QueueBuilder.durable(RabbitMQConstants.SMS_WRITE_LOG).build(); + } + + @Bean + public Queue pushReportQueue(){ + return QueueBuilder.durable(RabbitMQConstants.SMS_PUSH_REPORT).build(); + } + + +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitTemplateConfig.java b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitTemplateConfig.java new file mode 100644 index 0000000..12b9060 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/config/RabbitTemplateConfig.java @@ -0,0 +1,60 @@ +package com.mashibing.strategy.config; + +/** + * @author dch + * @create 2024-03-21 0:31 + */ + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 设置RabbitTemplate的confirm&return机制 + * @author zjw + * @description + */ +@Configuration +@Slf4j +public class RabbitTemplateConfig { + + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){ + //1、构建RabbitTemplate对象 + RabbitTemplate rabbitTemplate = new RabbitTemplate(); + + //2、设置connectionFactory + rabbitTemplate.setConnectionFactory(connectionFactory); + + //3、配置confirm机制 + rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){ + + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + // ack为false,代表消息没有发送到exchange。 + if(!ack){ + log.error("【接口模块-发送消息】 消息没有发送到交换机,correlationData = {},cause = {}",correlationData,cause); + } + } + }); + + //4、配置return机制 + rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback(){ + + // 触发这个回调,说明交换机没有把消息路由到指定的队列中 + @Override + public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { + log.error("【接口模块-发送消息】 消息没有路由到指定的Queue。 message = {},exchange = {},routingKey = {}", + new String(message.getBody()),exchange,routingKey); + } + }); + + //5、返回 + return rabbitTemplate; + } + +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackClientStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackClientStrategyFilter.java new file mode 100644 index 0000000..9acd1bf --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackClientStrategyFilter.java @@ -0,0 +1,52 @@ +package com.mashibing.strategy.filter.impl; + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author dch + * @create 2024-03-21 10:20 + */ +@Service(value = "blackclient") +@Slf4j +public class BlackClientStrategyFilter implements StrategyFilter { + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + @Autowired + private BeaconCacheClient cacheClient; + + // 黑名单的默认value + private final String TRUE = "1"; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-客户级别黑名单校验】 校验ing…………"); + //1、获取发送短信的手机号,以及客户的ID + String mobile = submit.getMobile(); + Long clientId = submit.getClientId(); + + //2、调用Redis查询 + String value = cacheClient.getString(CacheConstant.BLACK + clientId + CacheConstant.SEPARATE + mobile); + + //3、如果查询的结果为"1",代表是黑名单 + if(TRUE.equals(value)){ + log.info("【策略模块-客户级别黑名单校验】 当前发送的手机号是客户黑名单! mobile = {}",mobile); + submit.setErrorMsg(ExceptionEnums.BLACK_CLIENT + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.BLACK_CLIENT); + } + //4、不是1,正常结束 + log.info("【策略模块-客户级别黑名单校验】 当前手机号不是客户黑名单! "); + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackGlobalStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackGlobalStrategyFilter.java new file mode 100644 index 0000000..e77c0a2 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackGlobalStrategyFilter.java @@ -0,0 +1,51 @@ +package com.mashibing.strategy.filter.impl; + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author dch + * @create 2024-03-21 10:20 + */ +@Service(value = "blackglobal") +@Slf4j +public class BlackGlobalStrategyFilter implements StrategyFilter { + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + @Autowired + private BeaconCacheClient cacheClient; + + // 黑名单的默认value + private final String TRUE = "1"; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-全局级别黑名单校验】 校验ing…………"); + //1、获取发送短信的手机号 + String mobile = submit.getMobile(); + + //2、调用Redis查询 + String value = cacheClient.getString(CacheConstant.BLACK + mobile); + + //3、如果查询的结果为"1",代表是黑名单 + if(TRUE.equals(value)){ + log.info("【策略模块-全局级别黑名单校验】 当前手机号是黑名单! mobile = {}",mobile); + submit.setErrorMsg(ExceptionEnums.BLACK_GLOBAL.getMsg() + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.BLACK_GLOBAL); + } + //4、不是1,正常结束 + log.info("【策略模块-全局级别黑名单校验】 当前手机号不是黑名单!"); + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackStrategyFilter.java deleted file mode 100644 index 0e5ee94..0000000 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/BlackStrategyFilter.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mashibing.strategy.filter.impl; - -import com.mashibing.common.model.StandardSubmit; -import com.mashibing.strategy.filter.StrategyFilter; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -/** - * @author dch - * @create 2024-03-21 10:20 - */ -@Service(value = "black") -@Slf4j -public class BlackStrategyFilter implements StrategyFilter { - @Override - public void strategy(StandardSubmit submit) { - log.info("【策略模块-黑名单校验】 校验ing…………"); - } -} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordDFAStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordDFAStrategyFilter.java new file mode 100644 index 0000000..af2f1ae --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordDFAStrategyFilter.java @@ -0,0 +1,40 @@ +package com.mashibing.strategy.filter.impl; + +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.DFAUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Set; + +/** + * @author dch + * @create 2024-03-23 11:01 + */ +@Service(value = "dfadirtyword") +@Slf4j +public class DirtyWordDFAStrategyFilter implements StrategyFilter { + + @Autowired + private BeaconCacheClient cacheClient; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-敏感词校验】 校验ing…………"); + //1、 获取短信内容 + String text = submit.getText(); + + //2、 调用DFA查看敏感词 + Set dirtyWords = DFAUtil.getDirtyWord(text); + + //4、 根据返回的set集合,判断是否包含敏感词 + if (dirtyWords != null && dirtyWords.size() > 0) { + //5、 如果有敏感词,抛出异常 / 其他操作。。 + log.info("【策略模块-敏感词校验】 短信内容包含敏感词信息, dirtyWords = {}", dirtyWords); + // 还需要做其他处理 + } + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordHutoolDFAStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordHutoolDFAStrategyFilter.java new file mode 100644 index 0000000..4813e4c --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordHutoolDFAStrategyFilter.java @@ -0,0 +1,67 @@ +package com.mashibing.strategy.filter.impl; + +import com.alibaba.cloud.commons.lang.StringUtils; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.RabbitMQConstants; +import com.mashibing.common.constant.SmsConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardReport; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import com.mashibing.strategy.util.HutoolDFAUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import java.util.List; + +/** + * @author dch + * @create 2024-03-23 11:50 + */ +@Service(value = "hutooldfadirtyword") +@Slf4j +public class DirtyWordHutoolDFAStrategyFilter implements StrategyFilter { + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private ErrorSendMsgUtil errorSendMsgUtil; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-敏感词校验】 校验ing…………"); + //1、 获取短信内容 + String text = submit.getText(); + + //2、 调用DFA查看敏感词 + List dirtyWords = HutoolDFAUtil.getDirtyWord(text); + + //4、 根据返回的set集合,判断是否包含敏感词 + if (dirtyWords != null && dirtyWords.size() > 0) { + //5、 如果有敏感词,抛出异常 / 其他操作。。 + log.info("【策略模块-敏感词校验】 短信内容包含敏感词信息, dirtyWords = {}", dirtyWords); + // 封装错误信息 + // ================================发送写日志================================ + submit.setErrorMsg(ExceptionEnums.HAVE_DIRTY_WORD.getMsg() + "dirtyWords = " + dirtyWords.toString()); + errorSendMsgUtil.sendWriteLog(submit); + + // ================================发送状态报告的消息前,需要将report对象数据封装,回调================================ + errorSendMsgUtil.sendPushReport(submit); + + // // ================================抛出异常================================ + throw new StrategyException(ExceptionEnums.HAVE_DIRTY_WORD); + + } + + log.info("【策略模块-敏感词校验】 校验通过,没有敏感词信息"); + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java index 0ccbbd2..d9f72f6 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java @@ -1,9 +1,20 @@ package com.mashibing.strategy.filter.impl; +import com.mashibing.common.constant.CacheConstant; import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; import com.mashibing.strategy.filter.StrategyFilter; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.wltea.analyzer.core.IKSegmenter; +import org.wltea.analyzer.core.Lexeme; + +import java.io.IOException; +import java.io.StringReader; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; /** * @author dch @@ -14,9 +25,39 @@ import org.springframework.stereotype.Service; @Slf4j public class DirtyWordStrategyFilter implements StrategyFilter { + @Autowired + private BeaconCacheClient cacheClient; + @Override public void strategy(StandardSubmit submit) { - log.info("【策略模块-敏感词过滤】 过滤ing…………"); + log.info("【策略模块-敏感词校验】 校验ing…………"); + //1、 获取短信内容 + String text = submit.getText(); + + //2、 对短信内容进行分词,并且将分析内容存储到集合中 + Set contents = new HashSet<>(); + StringReader reader = new StringReader(text); + IKSegmenter ik = new IKSegmenter(reader, false); + Lexeme lex = null; + while (true) { + try { + if ((lex = ik.next()) == null) break; + } catch (IOException e) { + log.info("【策略模块-敏感词校验】 IK分词器在处理短信内容时,出现异常 e = {}", e.getMessage()); + } + contents.add(lex.getLexemeText()); + } + + + //3、 调用Cache缓存模块的交集方法,拿到结果 + + Set dirtyWords = cacheClient.sinterStr(UUID.randomUUID().toString(), CacheConstant.DIRTY_WORD, contents.toArray(new String[]{})); + //4、 根据返回的set集合,判断是否包含敏感词 + if (dirtyWords != null && dirtyWords.size() > 0) { + //5、 如果有敏感词,抛出异常 / 其他操作。。 + log.info("【策略模块-敏感词校验】 短信内容包含敏感词信息, dirtyWords = {}", dirtyWords); + // 还需要做其他处理 + } } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/FeeStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/FeeStrategyFilter.java new file mode 100644 index 0000000..7e495e3 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/FeeStrategyFilter.java @@ -0,0 +1,55 @@ +package com.mashibing.strategy.filter.impl; + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ClientBalanceUtil; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author dch + * @create 2024-03-24 23:21 + */ +@Service(value = "fee") +@Slf4j +public class FeeStrategyFilter implements StrategyFilter { + + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + private final String BALANCE = "balance"; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-扣费校验】 校验ing…………"); + //1、获取submit中封装的金额 + Long fee = submit.getFee(); + Long clientId = submit.getClientId(); + //2、调用Redis的decr扣减具体的金额 + Long amount = cacheClient.hIncrBy(CacheConstant.CLIENT_BALANCE + clientId, BALANCE, -fee); + + //3、获取当前客户的欠费金额的限制(外部方法调用,暂时写死为10000厘) + Long amountLimit = ClientBalanceUtil.getClientAmountLimit(submit.getClientId()); + + //4、判断扣减过后的金额,是否超出了金额限制 + if(amount < amountLimit) { + log.info("【策略模块-扣费校验】 扣除费用后,超过欠费余额的限制,无法发送短信!!"); + //5、如果超过了,需要将扣除的费用增加回去,并且做后续处理 + cacheClient.hIncrBy(CacheConstant.CLIENT_BALANCE + clientId, BALANCE, fee); + submit.setErrorMsg(ExceptionEnums.BALANCE_NOT_ENOUGH.getMsg()); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.BALANCE_NOT_ENOUGH); + } + log.info("【策略模块-扣费校验】 扣费成功!!"); + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneHourStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneHourStrategyFilter.java new file mode 100644 index 0000000..c229f52 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneHourStrategyFilter.java @@ -0,0 +1,101 @@ +package com.mashibing.strategy.filter.impl; + +/** + * @author dch + * @create 2024-03-24 22:16 + */ + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.SmsConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +/** + * 1小时发送3条的限流规则 + * @author zjw + * @description + */ +@Service("limitonehour") +@Slf4j +public class LimitOneHourStrategyFilter implements StrategyFilter { + + private final String UTC = "+8"; + + private final long ONE_HOUR = 60 * 1000 * 60 - 1; + + private final int RETRY_COUNT = 2; + + private final int LIMIT_HOUR = 3; + + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + + @Override + public void strategy(StandardSubmit submit) { + + // 判断短信类型不是验证码类的,直接结束方法 + if(submit.getState() != SmsConstant.CODE_TYPE){ + return; + } + log.info("【策略模块-一小时限流策略】 开始校验ing......"); + + //1、基于submit获取短信的发送时间 + LocalDateTime sendTime = submit.getSendTime(); + //2、基于LocalDateTime获取到时间的毫秒值 + long sendTimeMilli = sendTime.toInstant(ZoneOffset.of(UTC)).toEpochMilli(); + submit.setOneHourLimitMilli(sendTimeMilli); + //3、基于submit获取客户标识以及手机号信息 + Long clientId = submit.getClientId(); + String mobile = submit.getMobile(); + + //4、优先将当前短信发送信息插入到Redis的ZSet结构中 zadd + String key = CacheConstant.LIMIT_HOURS + clientId + CacheConstant.SEPARATE + mobile; + + //5、如果插入失败,需要重新的将毫秒值做改变,尝试重新插入 + int retry = 0; + while(!cacheClient.zadd(key, submit.getOneHourLimitMilli(), submit.getOneHourLimitMilli())){ + // 发送失败,尝试重试 + if(retry == RETRY_COUNT) break; + retry++; + // 插入失败,是因为存储的member不允许重复,既然重复了,将时间向后移动,移动到当前系统时间 + submit.setOneHourLimitMilli(System.currentTimeMillis()); + } + // 如果retry为2,代表已经重试了2次,但是依然没有成功 + if(retry == RETRY_COUNT){ + log.info("【策略模块-一小时限流策略】 插入失败! 满足一小时限流规则,无法发送!"); + submit.setErrorMsg(ExceptionEnums.ONE_HOUR_LIMIT + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.ONE_HOUR_LIMIT); + } + // 没有重试2次,3次之内,将数据正常的插入了。基于zrangebyscore做范围查询 + long start = submit.getOneHourLimitMilli() - ONE_HOUR; + int count = cacheClient.zRangeByScoreCount(key, Double.parseDouble(start + ""), Double.parseDouble(submit.getOneHourLimitMilli() + "")); + + if(count > LIMIT_HOUR){ + log.info("【策略模块-一小时限流策略】 插入失败! 满足一小时限流规则,无法发送!"); + cacheClient.zRemove(key,submit.getOneHourLimitMilli() + ""); + submit.setErrorMsg(ExceptionEnums.ONE_HOUR_LIMIT + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.ONE_HOUR_LIMIT); + } + + log.info("【策略模块-一小时限流策略】 一小时限流规则通过,可以发送!"); + + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneMinuteStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneMinuteStrategyFilter.java new file mode 100644 index 0000000..04d6d42 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/LimitOneMinuteStrategyFilter.java @@ -0,0 +1,85 @@ +package com.mashibing.strategy.filter.impl; + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.SmsConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ErrorSendMsgUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +/** + * @author dch + * @create 2024-03-24 21:30 + */ +@Service("limitoneminute") +@Slf4j +public class LimitOneMinuteStrategyFilter implements StrategyFilter { + + private final String UTC = "+8"; + + private final long ONE_MINUTE = 60 * 1000 - 1; + + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + + @Override + public void strategy(StandardSubmit submit) { + + // 判断短信类型不是验证码类的,直接结束方法 + if(submit.getState() != SmsConstant.CODE_TYPE){ + return; + } + log.info("【策略模块-一分钟时限流策略】 开始校验ing......"); + + //1、基于submit获取短信的发送时间 + LocalDateTime sendTime = submit.getSendTime(); + //2、基于LocalDateTime获取到时间的毫秒值 + long sendTimeMilli = sendTime.toInstant(ZoneOffset.of(UTC)).toEpochMilli(); + + //3、基于submit获取客户标识以及手机号信息 + Long clientId = submit.getClientId(); + String mobile = submit.getMobile(); + + //4、优先将当前短信发送信息插入到Redis的ZSet结构中 zadd + String key = CacheConstant.LIMIT_MINUTES + clientId + CacheConstant.SEPARATE + mobile; + Boolean addOk = cacheClient.zadd(key, sendTimeMilli, sendTimeMilli); + + //5、如果查询失败,直接告辞,有并发情况,60s不能发送两条,直接告辞 + if(!addOk){ + log.info("【策略模块-一分钟限流策略】 插入失败! 满足一分钟限流规则,无法发送!"); + submit.setErrorMsg(ExceptionEnums.ONE_MINUTE_LIMIT + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.ONE_MINUTE_LIMIT); + } + + //6、基于zrangebyscore查询1分钟直接,是否只有当前查询的发送短信信息 + long start = sendTimeMilli - ONE_MINUTE; + int count = cacheClient.zRangeByScoreCount(key, Double.parseDouble(start + ""), Double.parseDouble(sendTimeMilli + "")); + + //7、如果大于等于2条短信信息,达到了60s一条的短信限流规则,直接告辞。 + if(count > 1){ + // 一分钟之前,发送过短信,限流规则生效 + log.info("【策略模块-一分钟限流策略】 插入失败! 满足一分钟限流规则,无法发送!"); + cacheClient.zRemove(key,sendTimeMilli + ""); + submit.setErrorMsg(ExceptionEnums.ONE_MINUTE_LIMIT + ",mobile = " + mobile); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.ONE_MINUTE_LIMIT); + } + + log.info("【策略模块-一分钟限流策略】 一分钟限流规则通过,可以发送!"); + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/PhaseStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/PhaseStrategyFilter.java index ce43c7b..1d12c8e 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/PhaseStrategyFilter.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/PhaseStrategyFilter.java @@ -1,8 +1,17 @@ package com.mashibing.strategy.filter.impl; +import com.alibaba.cloud.commons.lang.StringUtils; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.RabbitMQConstants; +import com.mashibing.common.enums.MobileOperatorEnum; import com.mashibing.common.model.StandardSubmit; +import com.mashibing.common.util.OperatorUtil; +import com.mashibing.strategy.client.BeaconCacheClient; import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.MobileOperatorUtil; import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -14,9 +23,60 @@ import org.springframework.stereotype.Service; @Slf4j public class PhaseStrategyFilter implements StrategyFilter { + + /** + * 切分手机号前7位 + */ + private final int MOBILE_START = 0; + private final int MOBILE_END = 7; + /** + * 校验的长度 + */ + private final int LENGTH = 2; + /** + * 分割区域和运营商的标识 + */ + private final String SEPARATE = ","; + /** + * 未知的情况 + */ +// private final String UNKNOWN = "未知 未知,未知"; + + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private MobileOperatorUtil mobileOperatorUtil; + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Override public void strategy(StandardSubmit submit) { - log.info("【策略模块-号段补全】 补全ing…………"); + log.info("【策略模块-号段补齐】 补全ing…………"); + //1、根据手机号前7位,查询手机号信息 + String mobile = submit.getMobile().substring(MOBILE_START, MOBILE_END); + String mobileInfo = cacheClient.getString(CacheConstant.PHASE + mobile); + + getMobileInfo: + if (StringUtils.isEmpty(mobileInfo)) { + //2、查询不到,需要调用三方接口,查询手机号对应信息 + mobileInfo = mobileOperatorUtil.getMobileInfoBy360(mobile); + if (!StringUtils.isEmpty(mobileInfo)) { + //3、调用三方查到信息后,发送消息到MQ,并且同步到MySQL和Redis + rabbitTemplate.convertAndSend(RabbitMQConstants.MOBILE_AREA_OPERATIR, submit.getMobile()); + break getMobileInfo; + } + mobileInfo = MobileOperatorEnum.UNKNOWN.getOperatorName(); + } + + //4、无论是Redis还是三方接口查询到之后,封装到StandardSubmit对象中 + String[] areaAndOperator = mobileInfo.split(SEPARATE); + if (areaAndOperator.length == LENGTH) { + submit.setArea(areaAndOperator[0]); + submit.setOperatorId(OperatorUtil.getOperatorIdByOperatorName(areaAndOperator[1])); + } } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/RouteStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/RouteStrategyFilter.java index dec5c28..39e063b 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/RouteStrategyFilter.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/RouteStrategyFilter.java @@ -1,10 +1,27 @@ package com.mashibing.strategy.filter.impl; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.RabbitMQConstants; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.StrategyException; import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; import com.mashibing.strategy.filter.StrategyFilter; +import com.mashibing.strategy.util.ChannelTransferUtil; +import com.mashibing.strategy.util.ErrorSendMsgUtil; import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.AmqpException; +import org.springframework.amqp.core.AmqpAdmin; +import org.springframework.amqp.core.QueueBuilder; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + /** * @author dch * @create 2024-03-21 10:21 @@ -14,9 +31,99 @@ import org.springframework.stereotype.Service; @Slf4j public class RouteStrategyFilter implements StrategyFilter { + @Autowired + private BeaconCacheClient cacheClient; + + @Autowired + private ErrorSendMsgUtil sendMsgUtil; + + @Autowired + private AmqpAdmin amqpAdmin; + + @Autowired + private RabbitTemplate rabbitTemplate; + @Override public void strategy(StandardSubmit submit) { - log.info("【策略模块-通道选择】 通道选择ing…………"); + log.info("【策略模块-路由策略】 校验ing…………"); + //1、拿到客户id + Long clientId = submit.getClientId(); + + //2、基于redis获取当前客户绑定的所有通道信息 + Set clientChannels = cacheClient.smemberMap(CacheConstant.CLIENT_CHANNEL + clientId); + + //3、将获取到的客户通道信息根据权重做好排序 + TreeSet clientWeightChannels = new TreeSet<>(new Comparator() { + @Override + public int compare(Map o1, Map o2) { + int o1Weight = Integer.parseInt(o1.get("clientChannelWeight") + ""); + int o2Weight = Integer.parseInt(o2.get("clientChannelWeight") + ""); + return o2Weight - o1Weight; + } + }); + clientWeightChannels.addAll(clientChannels); + + boolean ok = false; + Map channel = null; + Map clientChannel = null; + //4、基于排好序的通道选择,权重更高的 + for (Map clientWeightChannel : clientWeightChannels) { + //5、如果客户和通道的绑定关系可用,直接去基于Redis查询具体的通道信息 + if((int)(clientWeightChannel.get("isAvailable")) != 0){ + // 当前关系不可用,直接进行下次循环,选择权重相对更低一点的 + continue; + } + + //6、如果通道信息查询后,判断通道睡否可用,其次运营商可以匹配。 + channel = cacheClient.hGetAll(CacheConstant.CHANNEL + clientWeightChannel.get("channelId")); + if((int)(channel.get("isAvailable")) != 0){ + // 当前通道不可用,选择权重更低的通道~ + continue; + } + // 获取通道的通讯方式 + Integer channelType = (Integer) channel.get("channelType"); + if (channelType != 0 && submit.getOperatorId() != channelType){ + // 通道不是全网通,并且和当前手机号运营商不匹配 + continue; + } + + //7、如果后期涉及到的通道的转换,这里留一个口子 + Map transferChannel = ChannelTransferUtil.transfer(submit, channel); + + // 找到可以使用的通道了 + ok = true; + clientChannel = clientWeightChannel; + break; + } + + if(!ok){ + log.info("【策略模块-路由策略】 没有选择到可用的通道!!"); + submit.setErrorMsg(ExceptionEnums.NO_CHANNEL.getMsg()); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(ExceptionEnums.NO_CHANNEL); + } + + + //8、基于选择的通道封装submit的信息 + submit.setChannelId(Long.parseLong(channel.get("id") + "")); + submit.setSrcNumber("" + channel.get("channelNumber") + clientChannel.get("clientChannelNumber")); + + try { + //9、声明好队列名称,并构建队列 + String queueName = RabbitMQConstants.SMS_GATEWAY + submit.getChannelId(); + amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build()); + + //10、发送消息到声明好的队列中 + rabbitTemplate.convertAndSend(queueName,submit); + } catch (AmqpException e) { + log.info("【策略模块-路由策略】 声明通道对应队列以及发送消息时出现了问题!"); + submit.setErrorMsg(e.getMessage()); + sendMsgUtil.sendWriteLog(submit); + sendMsgUtil.sendPushReport(submit); + throw new StrategyException(e.getMessage(),ExceptionEnums.UNKNOWN_ERROR.getCode()); + } + } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/TransferStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/TransferStrategyFilter.java new file mode 100644 index 0000000..121d411 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/TransferStrategyFilter.java @@ -0,0 +1,47 @@ +package com.mashibing.strategy.filter.impl; + +import com.alibaba.cloud.commons.lang.StringUtils; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import com.mashibing.strategy.filter.StrategyFilter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author dch + * @create 2024-03-24 19:51 + */ +@Service(value = "transfer") +@Slf4j +public class TransferStrategyFilter implements StrategyFilter { + + // 代表携号转网了! + private final Boolean TRANSFER = true; + + @Autowired + private BeaconCacheClient cacheClient; + + @Override + public void strategy(StandardSubmit submit) { + log.info("【策略模块-携号转网策略】 ing…………"); + //1、获取用户手机号 + String mobile = submit.getMobile(); + + //2、直接基于Redis查询携号转网信息 + String value = cacheClient.getString(CacheConstant.TRANSFER + mobile); + + //3、如果存在携号转网,设置运营商信息 + if(!StringUtils.isEmpty(value)){ + // 代表携号转网了 + submit.setOperatorId(Integer.valueOf(value)); + submit.setIsTransfer(TRANSFER); + log.info("【策略模块-携号转网策略】 当前手机号携号转网了!"); + return; + } + + log.info("【策略模块-携号转网策略】 当前手机号不涉及携号转网!"); + + } +} \ No newline at end of file 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 674e2da..fe909ce 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 @@ -1,6 +1,7 @@ package com.mashibing.strategy.mq; import com.mashibing.common.constant.RabbitMQConstants; +import com.mashibing.common.exception.StrategyException; import com.mashibing.common.model.StandardSubmit; import com.mashibing.strategy.filter.StrategyFilterContext; import com.rabbitmq.client.Channel; @@ -34,9 +35,10 @@ public class PreSendListener { filterContext.strategy(submit); log.info("【策略模块-消费完毕】手动ack"); channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); - } catch (IOException e) { - e.printStackTrace(); - log.error("【策略模块-消费失败】凉凉~~~"); + } catch (StrategyException e) { +// e.printStackTrace(); + log.info("【策略模块-消费失败】校验未通过,msg={}",e.getMessage()); + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); } } } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/ChannelTransferUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ChannelTransferUtil.java new file mode 100644 index 0000000..e43cf5b --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ChannelTransferUtil.java @@ -0,0 +1,17 @@ +package com.mashibing.strategy.util; + +import com.mashibing.common.model.StandardSubmit; + +import java.util.Map; + +/** + * 通道转换留的口子,暂时什么都不做 + * @author dch + * @create 2024-03-25 0:41 + */ +public class ChannelTransferUtil { + + public static Map transfer(StandardSubmit submit,Map channel){ + return channel; + } +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/ClientBalanceUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ClientBalanceUtil.java new file mode 100644 index 0000000..bba6a7e --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ClientBalanceUtil.java @@ -0,0 +1,18 @@ +package com.mashibing.strategy.util; + +/** + * @author dch + * @create 2024-03-24 23:35 + */ +public class ClientBalanceUtil { + /** + * 后期如果要给客户指定欠费的额度等级,再重写方法 + * + * @param clientId + * @return + */ + public static Long getClientAmountLimit(Long clientId) { + return -10000L; + + } +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/DFAUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/DFAUtil.java new file mode 100644 index 0000000..171dd9c --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/DFAUtil.java @@ -0,0 +1,144 @@ +package com.mashibing.strategy.util; + +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.strategy.client.BeaconCacheClient; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author dch + * @create 2024-03-23 1:43 + */ +public class DFAUtil { + // 敏感词树 + private static Map dfaMap = new HashMap<>(); + + private static final String IS_END = "isEnd"; + + private static final String NOT_END = "0"; + private static final String ALREADY_END = "1"; + + /** + * 初始化敏感词树 + * 不autowired BeaconCacheClient因为 + * Autowired members must be defined in valid Spring bean (@Component|@Service|...) + */ + static { + // 获取Spring容器中的cacheClient + BeaconCacheClient cacheClient = (BeaconCacheClient) SpringUtil.getBeanByClass(BeaconCacheClient.class); + // 获取存储在Redis中的全部敏感词 + Set dirtyWords = cacheClient.smember(CacheConstant.DIRTY_WORD); + // 调用create,将dfaMap的敏感词树构建 + create(dirtyWords); + } + + + public static void main(String[] args) { + // 敏感词库 + Set dirtyWords = new HashSet<>(); + dirtyWords.add("三胖"); + dirtyWords.add("山炮"); + dirtyWords.add("三胖啊啊"); + dirtyWords.add("瘦啊"); + // 测试敏感词树的生成 + create(dirtyWords); + // 测试效果 + String text = "你三瘦啊山炮"; + System.out.println(getDirtyWord(text)); + } + + /** + * 构建敏感词树 + * @param dirtyWords + */ + public static void create(Set dirtyWords) { + //1、 声明一个Map作为临时存储 + Map nowMap; + + //2、遍历敏感词库 + for (String dirtyWord : dirtyWords) { + nowMap = dfaMap; + // 每个词,依次获取 + for (int i = 0; i < dirtyWord.length(); i++) { + // 获取敏感词的每个字 + String word = String.valueOf(dirtyWord.charAt(i)); + // 判断当前的敏感词树中是否包含当前的字 + Map map = (Map) nowMap.get(word); + if (map == null) { + // 当前敏感词树中没有这个字 + map = new HashMap(); + // 将当前的敏感词存入 + nowMap.put(word, map); + } + // 操作当前key对应的value的map + nowMap = map; + // 如果当前的字,已经有IS_END了,并且值为1,直接不管, + if (nowMap.containsKey(IS_END) && nowMap.get(IS_END).equals(ALREADY_END)) { + continue; + } + // 如果当前的isEnd没有,或者是0,需要考虑需要改为1 + if (i == dirtyWord.length() - 1) { + // 到最后一个字了。 + nowMap.put(IS_END, ALREADY_END); + } else { + // isEnd之前就是0,或者压根就没有isEnd + nowMap.putIfAbsent(IS_END, NOT_END); + } + } + } + } + + /** + * 基于敏感词树,对文字进行敏感词获取 + * @param text + * @return + */ + public static Set getDirtyWord(String text) { + // 1、作为返回结果存储敏感词的位置 + Set dirtyWords = new HashSet<>(); + // 2、循环遍历文本内容 + for (int i = 0; i < text.length(); i++) { + // 临时存储索引位置的变量 + int nextLength = 0; + int dirtyLength = 0; + // 获取最外层key的map + Map nowMap = dfaMap; + // 外层是索引向后动,匹配最外层的key + // 内层是在匹配上一个后,继续向内部匹配内部的key + for (int j = i; j < text.length(); j++) { + // 获取当前索引位置的字 + String word = String.valueOf(text.charAt(j)); + // 先匹配最外层的key + nowMap = (Map) nowMap.get(word); + // 判断 + if (nowMap == null) { + // 没有这个字开头的敏感词 + break; + } else { + // 敏感词长度,从i开始算,现在的是dirtyLength + dirtyLength++; + // 出口即是,当前的map的isEnd是1,代表结束了。已经找到完整的敏感词 + if (ALREADY_END.equals(nowMap.get(IS_END))) { + // 代表敏感词匹配到一个完整的 + nextLength = dirtyLength; + break; + } + } + + } + // 判断是否匹配上了敏感词 + if (nextLength > 0) { + // 匹配上了,添加敏感词到set,同时移动外层索引 + dirtyWords.add(text.substring(i, i + nextLength)); + // -1的原因是,外层for循环,会对i进行++ + i = i + nextLength - 1; + } + } + // 返回 + return dirtyWords; + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/ErrorSendMsgUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ErrorSendMsgUtil.java new file mode 100644 index 0000000..32382c7 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/ErrorSendMsgUtil.java @@ -0,0 +1,65 @@ +package com.mashibing.strategy.util; + +import com.alibaba.cloud.commons.lang.StringUtils; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.common.constant.RabbitMQConstants; +import com.mashibing.common.constant.SmsConstant; +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.model.StandardReport; +import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 17:37 + */ +@Component +public class ErrorSendMsgUtil { + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Autowired + private BeaconCacheClient cacheClient; + + /** + * 策略模块校验未通过,发送写日志操作 + * @param submit + * + */ + public void sendWriteLog(StandardSubmit submit) { + submit.setReportState(SmsConstant.REPORT_FAIL); + // 发送消息到写日志队列 + rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_WRITE_LOG,submit); + } + + /** + * 策略模块校验未通过,发送状态报告操作 + */ + public void sendPushReport(StandardSubmit submit) { + // 查询当前客户的isCallback + Integer isCallback = cacheClient.hgetInteger(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "isCallback"); + // 查看是否需要给客户一个回调 + if(isCallback == 1){ + // 如果需要回调,再查询客户的回调地址 + String callbackUrl = cacheClient.hget(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "callbackUrl"); + // 如果回调地址不为空。 + if(!StringUtils.isEmpty(callbackUrl)){ + // 封装客户的报告推送的信息,开始封装StandardReport + StandardReport report = new StandardReport(); + BeanUtils.copyProperties(submit,report); + report.setIsCallback(isCallback); + report.setCallbackUrl(callbackUrl); + // 发送消息到RabbitMQ + rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_PUSH_REPORT,report); + } + + } + } +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/HutoolDFAUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/HutoolDFAUtil.java new file mode 100644 index 0000000..0004d48 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/HutoolDFAUtil.java @@ -0,0 +1,35 @@ +package com.mashibing.strategy.util; + +import cn.hutool.dfa.WordTree; +import com.mashibing.common.constant.CacheConstant; +import com.mashibing.strategy.client.BeaconCacheClient; + +import java.util.List; +import java.util.Set; + +/** + * @author dch + * @create 2024-03-23 11:48 + */ +public class HutoolDFAUtil { + + private static WordTree wordTree = new WordTree(); + + /** + * 初始化敏感词树 + */ + static { + // 获取Spring容器中的cacheClient + BeaconCacheClient cacheClient = (BeaconCacheClient) SpringUtil.getBeanByClass(BeaconCacheClient.class); + // 获取存储在Redis中的全部敏感词 + Set dirtyWords = cacheClient.smember(CacheConstant.DIRTY_WORD); + // 调用WordTree的add方法,将dfaMap的敏感词树构建 + wordTree.addWords(dirtyWords); + } + + + public static List getDirtyWord(String text){ + return wordTree.matchAll(text); + } + +} \ No newline at end of file diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/MobileOperatorUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/MobileOperatorUtil.java new file mode 100644 index 0000000..59e5e75 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/MobileOperatorUtil.java @@ -0,0 +1,61 @@ +package com.mashibing.strategy.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +/** + * @author dch + * @create 2024-03-22 22:29 + * 获取手机号归属地和运营商的工具 + */ +@Component +public class MobileOperatorUtil { + + @Autowired + private RestTemplate restTemplate; + @Autowired + private ObjectMapper objectMapper; + private final String urll = "https://cx.shouji.360.cn/phonearea.php?number="; + + private final String CODE = "code"; + private final String DATA = "data"; + private final String PROVINCE = "province"; + private final String CITY = "city"; + private final String SP = "sp"; + private final String SPACE = " "; + private final String SEPARATE = ","; + + + public String getMobileInfoBy360(String mobile) { + String url = urll; + //1.发送请求获取信息 + String mobileInfoJSON = restTemplate.getForObject(url + mobile, String.class); + + //2.解析json + Map map = null; + try { + map = objectMapper.readValue(mobileInfoJSON, Map.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + Integer code = (Integer) map.get(CODE); + if (code != 0) { + return null; + } + Map areaAndOperator = (Map) map.get(DATA); + String province = areaAndOperator.get(PROVINCE); + String city = areaAndOperator.get(CITY); + String sp = areaAndOperator.get(SP); + + //3.封装为 省 市,运营商 的格式返回 + + return province + SPACE + city + SEPARATE + sp; + } + + +} diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/util/SpringUtil.java b/beacon-strategy/src/main/java/com/mashibing/strategy/util/SpringUtil.java new file mode 100644 index 0000000..73534a1 --- /dev/null +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/util/SpringUtil.java @@ -0,0 +1,29 @@ +package com.mashibing.strategy.util; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +/** + * @author dch + * @create 2024-03-23 10:50 + */ +@Component +public class SpringUtil implements ApplicationContextAware { + + private static ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + SpringUtil.applicationContext = applicationContext; + } + + public static Object getBeanByName(String beanName){ + return SpringUtil.applicationContext.getBean(beanName); + } + + public static Object getBeanByClass(Class clazz){ + return SpringUtil.applicationContext.getBean(clazz); + } +} diff --git a/beacon-strategy/src/main/resources/bootstrap.yml b/beacon-strategy/src/main/resources/bootstrap.yml index 4d70379..4a04b3e 100644 --- a/beacon-strategy/src/main/resources/bootstrap.yml +++ b/beacon-strategy/src/main/resources/bootstrap.yml @@ -9,9 +9,9 @@ spring: cloud: nacos: discovery: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 # nacos配置中心地址: config: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 file-extension: yml # beacon-strategy-dev.yml diff --git a/beacon-test/src/main/java/com/mashibing/test/client/CacheClient.java b/beacon-test/src/main/java/com/mashibing/test/client/CacheClient.java index c0f7280..294ebfd 100644 --- a/beacon-test/src/main/java/com/mashibing/test/client/CacheClient.java +++ b/beacon-test/src/main/java/com/mashibing/test/client/CacheClient.java @@ -24,4 +24,11 @@ public interface CacheClient { @PostMapping(value = "/cache/sadd/{key}") void sadd(@PathVariable(value = "key")String key, @RequestBody Map... maps); + @PostMapping(value = "/cache/saddstr/{key}") + void saddStr(@PathVariable(value = "key")String key, @RequestBody String... value); + + @PostMapping("/cache/pipeline/string") + void pipelineString(@RequestBody Map map); + + } diff --git a/beacon-test/src/main/java/com/mashibing/test/entity/Channel.java b/beacon-test/src/main/java/com/mashibing/test/entity/Channel.java new file mode 100644 index 0000000..40f3064 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/entity/Channel.java @@ -0,0 +1,125 @@ +package com.mashibing.test.entity; + +/** + * @author dch + * @create 2024-03-24 23:52 + */ +public class Channel { + private Long id; + private String channelName; + private Integer channelType; + private String channelArea; + private String channelAreaCode; + private Long channelPrice; + private Integer channelProtocal; + private String channelIp; + private Integer channelPort; + private String channelUsername; + private String channelPassword; + private String channelNumber; + private Integer isAvailable; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getChannelName() { + return channelName; + } + + public void setChannelName(String channelName) { + this.channelName = channelName; + } + + public Integer getChannelType() { + return channelType; + } + + public void setChannelType(Integer channelType) { + this.channelType = channelType; + } + + public String getChannelArea() { + return channelArea; + } + + public void setChannelArea(String channelArea) { + this.channelArea = channelArea; + } + + public String getChannelAreaCode() { + return channelAreaCode; + } + + public void setChannelAreaCode(String channelAreaCode) { + this.channelAreaCode = channelAreaCode; + } + + public Long getChannelPrice() { + return channelPrice; + } + + public void setChannelPrice(Long channelPrice) { + this.channelPrice = channelPrice; + } + + public Integer getChannelProtocal() { + return channelProtocal; + } + + public void setChannelProtocal(Integer channelProtocal) { + this.channelProtocal = channelProtocal; + } + + public String getChannelIp() { + return channelIp; + } + + public void setChannelIp(String channelIp) { + this.channelIp = channelIp; + } + + public Integer getChannelPort() { + return channelPort; + } + + public void setChannelPort(Integer channelPort) { + this.channelPort = channelPort; + } + + public String getChannelUsername() { + return channelUsername; + } + + public void setChannelUsername(String channelUsername) { + this.channelUsername = channelUsername; + } + + public String getChannelPassword() { + return channelPassword; + } + + public void setChannelPassword(String channelPassword) { + this.channelPassword = channelPassword; + } + + public String getChannelNumber() { + return channelNumber; + } + + public void setChannelNumber(String channelNumber) { + this.channelNumber = channelNumber; + } + + public Integer getIsAvailable() { + return isAvailable; + } + + public void setIsAvailable(Integer isAvailable) { + this.isAvailable = isAvailable; + } +} diff --git a/beacon-test/src/main/java/com/mashibing/test/entity/ClientChannel.java b/beacon-test/src/main/java/com/mashibing/test/entity/ClientChannel.java new file mode 100644 index 0000000..10f3c0e --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/entity/ClientChannel.java @@ -0,0 +1,50 @@ +package com.mashibing.test.entity; + + +public class ClientChannel { + private Long clientId; + private Long channelId; + private Integer clientChannelWeight; + private String clientChannelNumber; + private Integer isAvailable; + + public Long getClientId() { + return clientId; + } + + public void setClientId(Long clientId) { + this.clientId = clientId; + } + + public Long getChannelId() { + return channelId; + } + + public void setChannelId(Long channelId) { + this.channelId = channelId; + } + + public Integer getClientChannelWeight() { + return clientChannelWeight; + } + + public void setClientChannelWeight(Integer clientChannelWeight) { + this.clientChannelWeight = clientChannelWeight; + } + + public String getClientChannelNumber() { + return clientChannelNumber; + } + + public void setClientChannelNumber(String clientChannelNumber) { + this.clientChannelNumber = clientChannelNumber; + } + + public Integer getIsAvailable() { + return isAvailable; + } + + public void setIsAvailable(Integer isAvailable) { + this.isAvailable = isAvailable; + } +} diff --git a/beacon-test/src/main/java/com/mashibing/test/entity/MobileArea.java b/beacon-test/src/main/java/com/mashibing/test/entity/MobileArea.java new file mode 100644 index 0000000..f1d9a52 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/entity/MobileArea.java @@ -0,0 +1,36 @@ +package com.mashibing.test.entity; + +/** + * @author dch + * @create 2024-03-22 21:27 + */ +public class MobileArea { + + private String mobileNumber; + private String mobileArea; + private String mobileType; + + public String getMobileNumber() { + return mobileNumber; + } + + public void setMobileNumber(String mobileNumber) { + this.mobileNumber = mobileNumber; + } + + public String getMobileArea() { + return mobileArea; + } + + public void setMobileArea(String mobileArea) { + this.mobileArea = mobileArea; + } + + public String getMobileType() { + return mobileType; + } + + public void setMobileType(String mobileType) { + this.mobileType = mobileType; + } +} diff --git a/beacon-test/src/main/java/com/mashibing/test/entity/MobileBlack.java b/beacon-test/src/main/java/com/mashibing/test/entity/MobileBlack.java new file mode 100644 index 0000000..d428e2a --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/entity/MobileBlack.java @@ -0,0 +1,30 @@ +package com.mashibing.test.entity; + +/** + * @author dch + * @create 2024-03-22 21:27 + */ +public class MobileBlack { + + private String blackNumber; + + private Integer clientId; + + public String getBlackNumber() { + return blackNumber; + } + + public void setBlackNumber(String blackNumber) { + this.blackNumber = blackNumber; + } + + public Integer getClientId() { + return clientId; + } + + public void setClientId(Integer clientId) { + this.clientId = clientId; + } + + +} diff --git a/beacon-test/src/main/java/com/mashibing/test/entity/MobileTransfer.java b/beacon-test/src/main/java/com/mashibing/test/entity/MobileTransfer.java new file mode 100644 index 0000000..0e4f245 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/entity/MobileTransfer.java @@ -0,0 +1,28 @@ +package com.mashibing.test.entity; + +/** + * @author dch + * @create 2024-03-24 19:27 + */ +public class MobileTransfer { + + private String transferNumber; + + private Integer nowIsp; + + public String getTransferNumber() { + return transferNumber; + } + + public void setTransferNumber(String transferNumber) { + this.transferNumber = transferNumber; + } + + public Integer getNowIsp() { + return nowIsp; + } + + public void setNowIsp(Integer nowIsp) { + this.nowIsp = nowIsp; + } +} diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/ChannelMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/ChannelMapper.java new file mode 100644 index 0000000..9b0dba4 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/ChannelMapper.java @@ -0,0 +1,16 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.entity.Channel; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 23:54 + */ +public interface ChannelMapper { + @Select("select * from channel where is_delete = 0") + List findAll(); + +} \ No newline at end of file diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/ClientChannelMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/ClientChannelMapper.java new file mode 100644 index 0000000..66753a7 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/ClientChannelMapper.java @@ -0,0 +1,17 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.entity.Channel; +import com.mashibing.test.entity.ClientChannel; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 23:54 + */ +public interface ClientChannelMapper { + @Select("select * from client_channel where is_delete = 0") + List findAll(); + +} \ No newline at end of file diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/MobileAreaMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileAreaMapper.java new file mode 100644 index 0000000..b5a0e78 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileAreaMapper.java @@ -0,0 +1,18 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.entity.ClientTemplate; +import com.mashibing.test.entity.MobileArea; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-22 21:26 + */ +public interface MobileAreaMapper { + @Select("select mobile_number,mobile_area,mobile_type from mobile_area") + List findAll(); + +} diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/MobileBlackMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileBlackMapper.java new file mode 100644 index 0000000..b4251ad --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileBlackMapper.java @@ -0,0 +1,17 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.entity.MobileBlack; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 18:16 + */ +public interface MobileBlackMapper { + + @Select("select black_number,client_id from mobile_black where is_delete = 0") + List findAll(); + +} \ No newline at end of file diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/MobileDirtyWordMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileDirtyWordMapper.java new file mode 100644 index 0000000..dd77d07 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileDirtyWordMapper.java @@ -0,0 +1,16 @@ +package com.mashibing.test.mapper; + +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-23 0:05 + */ +public interface MobileDirtyWordMapper { + + @Select("select dirtyword from mobile_dirtyword") + List findDirtyWord(); + +} diff --git a/beacon-test/src/main/java/com/mashibing/test/mapper/MobileTransferMapper.java b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileTransferMapper.java new file mode 100644 index 0000000..3888888 --- /dev/null +++ b/beacon-test/src/main/java/com/mashibing/test/mapper/MobileTransferMapper.java @@ -0,0 +1,17 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.entity.MobileTransfer; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 19:26 + */ +public interface MobileTransferMapper { + + @Select("select transfer_number,now_isp from mobile_transfer where is_transfer = 1 and is_delete = 0") + List findAll(); + +} \ No newline at end of file diff --git a/beacon-test/src/main/resources/application.yml b/beacon-test/src/main/resources/application.yml index e862b17..c04b196 100644 --- a/beacon-test/src/main/resources/application.yml +++ b/beacon-test/src/main/resources/application.yml @@ -6,13 +6,13 @@ spring: cloud: nacos: discovery: - server-addr: 172.20.10.2:8848 + server-addr: 192.168.43.132:8848 # datasource datasource: driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/beacon_cloud?characterEncoding=utf-8&useSSL=false username: root - password: 123456 + password: 1234 # 端口号 server: port: 20000 diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/ChannelMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/ChannelMapperTest.java new file mode 100644 index 0000000..2ba4b7e --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/ChannelMapperTest.java @@ -0,0 +1,43 @@ +package com.mashibing.test.mapper; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.mashibing.test.client.CacheClient; +import com.mashibing.test.entity.Channel; +import com.mashibing.test.entity.MobileBlack; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.rmi.CORBA.ValueHandler; +import java.util.List; +import java.util.Map; + +/** + * @author dch + * @create 2024-03-24 18:17 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class ChannelMapperTest { + + @Autowired + private ChannelMapper mapper; + + @Autowired + private CacheClient cacheClient; + + + @Test + public void findAll() throws JsonProcessingException { + List list = mapper.findAll(); + for (Channel channel : list) { + ObjectMapper objectMapper = new ObjectMapper(); + Map map = objectMapper.readValue(objectMapper.writeValueAsString(channel),Map.class); + cacheClient.hmset("channel:"+channel.getId(),map); + } + } +} \ No newline at end of file diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/ClientChannelMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/ClientChannelMapperTest.java new file mode 100644 index 0000000..3168cb1 --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/ClientChannelMapperTest.java @@ -0,0 +1,41 @@ +package com.mashibing.test.mapper; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mashibing.test.client.CacheClient; +import com.mashibing.test.entity.Channel; +import com.mashibing.test.entity.ClientChannel; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; +import java.util.Map; + +/** + * @author dch + * @create 2024-03-24 18:17 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class ClientChannelMapperTest { + + @Autowired + private ClientChannelMapper mapper; + + @Autowired + private CacheClient cacheClient; + + + @Test + public void findAll() throws JsonProcessingException { + List list = mapper.findAll(); + for (ClientChannel clientChannel : list) { + ObjectMapper objectMapper = new ObjectMapper(); + Map map = objectMapper.readValue(objectMapper.writeValueAsString(clientChannel),Map.class); + cacheClient.sadd("client_channel:"+clientChannel.getClientId(),map); + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..2d11564 --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileAreaMapperTest.java @@ -0,0 +1,40 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.client.CacheClient; +import com.mashibing.test.entity.MobileArea; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * @author dch + * @create 2024-03-22 21:28 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class MobileAreaMapperTest { + + @Autowired + private MobileAreaMapper mapper; + + @Autowired + private CacheClient cacheClient; + + @Test + public void findAll() { + List list = mapper.findAll(); + Map map = new HashMap(list.size()); + for(MobileArea mobileArea : list){ + map.put("phase:"+mobileArea.getMobileNumber(),mobileArea.getMobileArea()+","+mobileArea.getMobileType()); + } + cacheClient.pipelineString(map); + } +} \ No newline at end of file diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/MobileBlackMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileBlackMapperTest.java new file mode 100644 index 0000000..cb686e2 --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileBlackMapperTest.java @@ -0,0 +1,41 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.client.CacheClient; +import com.mashibing.test.entity.MobileBlack; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 18:17 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class MobileBlackMapperTest { + + @Autowired + private MobileBlackMapper mapper; + + @Autowired + private CacheClient cacheClient; + + + @Test + public void findAll() { + List mobileBlackList = mapper.findAll(); + for (MobileBlack mobileBlack : mobileBlackList) { + if(mobileBlack.getClientId() == 0){ + // 平台级别的黑名单 black:手机号 作为key + cacheClient.set("black:" + mobileBlack.getBlackNumber(),"1"); + }else{ + // 客户级别的黑名单 black:clientId:手机号 + cacheClient.set("black:" + mobileBlack.getClientId() + ":" +mobileBlack.getBlackNumber(),"1"); + } + } + } +} \ No newline at end of file diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/MobileDirtyWordMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileDirtyWordMapperTest.java new file mode 100644 index 0000000..3ca7628 --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileDirtyWordMapperTest.java @@ -0,0 +1,34 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.client.CacheClient; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * @author dch + * @create 2024-03-23 0:05 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class MobileDirtyWordMapperTest { + + @Autowired + private MobileDirtyWordMapper mapper; + + @Autowired + private CacheClient cacheClient; + + @Test + public void findAll() { + List dirtyWords = mapper.findDirtyWord(); + + cacheClient.saddStr("dirty_word", dirtyWords.toArray(new String[]{})); + } +} \ No newline at end of file diff --git a/beacon-test/src/test/java/com/mashibing/test/mapper/MobileTransferMapperTest.java b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileTransferMapperTest.java new file mode 100644 index 0000000..b060d8e --- /dev/null +++ b/beacon-test/src/test/java/com/mashibing/test/mapper/MobileTransferMapperTest.java @@ -0,0 +1,36 @@ +package com.mashibing.test.mapper; + +import com.mashibing.test.client.CacheClient; +import com.mashibing.test.entity.MobileTransfer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +/** + * @author dch + * @create 2024-03-24 19:33 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class MobileTransferMapperTest { + + @Autowired + private MobileTransferMapper mapper; + + @Autowired + private CacheClient cacheClient; + + + @Test + public void findAll() { + List list = mapper.findAll(); + + for (MobileTransfer mobileTransfer : list) { + cacheClient.set("transfer:" + mobileTransfer.getTransferNumber(),mobileTransfer.getNowIsp()); + } + } +} \ No newline at end of file