diff --git a/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java b/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java index d7eb50e2..21f187ca 100644 --- a/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java +++ b/sms4j-core/src/main/java/org/dromara/sms4j/core/proxy/processor/RestrictedProcessor.java @@ -64,39 +64,57 @@ public class RestrictedProcessor implements CoreMethodProcessor, SmsDaoAware { throw new SmsBlendException("The smsDao tool could not be found"); } SmsConfig config = BeanFactory.getSmsConfig(); - // 如果未开始限制则不做处理 - if (!config.getRestricted()){ - return; - } // 每日最大发送量 Integer accountMax = config.getAccountMax(); // 每分钟最大发送量 Integer minuteMax = config.getMinuteMax(); + // 配置了每日最大发送量 + boolean dailyMaxLimitExists = SmsUtils.isNotEmpty(accountMax); + // 配置了每分钟最大发送量 + boolean perMinuteLimitExists = SmsUtils.isNotEmpty(minuteMax); + // 如果未开启限制或未配置任何限制发送量,不做处理 + boolean isNoProcessing = !config.getRestricted() || (!dailyMaxLimitExists && !perMinuteLimitExists); + if (isNoProcessing) { + return; + } for (String phone : phones) { + // 分钟发送量缓存key + String minuteMaxKey = REDIS_KEY + phone; + // 天发送量缓存key + String accountMaxKey = minuteMaxKey.concat("max"); // 是否配置了每日限制 - if (SmsUtils.isNotEmpty(accountMax)) { - Integer i = (Integer) smsDao.get(REDIS_KEY + phone + "max"); - if (SmsUtils.isEmpty(i)) { - smsDao.set(REDIS_KEY + phone + "max", 1, accTimer / 1000); - } else if (i >= accountMax) { + if (dailyMaxLimitExists) { + Integer dailyCount = (Integer) smsDao.get(accountMaxKey); + if (SmsUtils.isEmpty(dailyCount)) { + smsDao.set(accountMaxKey, 1, accTimer / 1000); + } else if (dailyCount >= accountMax) { log.info("The phone: {},number of short messages reached the maximum today", phone); throw new SmsBlendException("The phone: {},number of short messages reached the maximum today", phone); } else { - smsDao.set(REDIS_KEY + phone + "max", i + 1, accTimer / 1000); + smsDao.set(accountMaxKey, dailyCount + 1, accTimer / 1000); } } // 是否配置了每分钟最大限制 - if (SmsUtils.isNotEmpty(minuteMax)) { - Integer o = (Integer) smsDao.get(REDIS_KEY + phone); - if (SmsUtils.isNotEmpty(o)) { - if (o < minuteMax) { - smsDao.set(REDIS_KEY + phone, o + 1, minTimer / 1000); + if (perMinuteLimitExists) { + Integer minuteCount = (Integer) smsDao.get(REDIS_KEY + phone); + if (SmsUtils.isNotEmpty(minuteCount)) { + if (minuteCount < minuteMax) { + smsDao.set(minuteMaxKey, minuteCount + 1, minTimer / 1000); } else { - log.info("The phone: {},number of short messages reached the maximum today", phone); + //如果能走到这里且存在每日限制,说明每日限制已经计数,这里将之前的计数减一次 + if (dailyMaxLimitExists) { + Integer dailyCount = (Integer) smsDao.get(accountMaxKey); + if (dailyCount > 1) { + smsDao.set(accountMaxKey, dailyCount - 1, accTimer / 1000); + } else { + smsDao.remove(accountMaxKey); + } + } + log.info("The phone: {} Text messages are sent too often!", phone); throw new SmsBlendException("The phone: {} Text messages are sent too often!", phone); } } else { - smsDao.set(REDIS_KEY + phone, 1, minTimer / 1000); + smsDao.set(minuteMaxKey, 1, minTimer / 1000); } } }