更新发送短信逻辑
This commit is contained in:
@@ -15,7 +15,7 @@ public enum SmsTemplateEnum {
|
||||
/**
|
||||
* 通用验证码
|
||||
*/
|
||||
UNIVERSAL("油梨科技", "SMS_501981064", 5, UniversalSmsVariable.class);
|
||||
UNIVERSAL("油梨科技", "SMS_501981064", 5,1, UniversalSmsVariable.class);
|
||||
|
||||
/**
|
||||
* 模板签名
|
||||
@@ -34,6 +34,13 @@ public enum SmsTemplateEnum {
|
||||
*/
|
||||
private final Integer effectiveTime;
|
||||
|
||||
/**
|
||||
* 重发限制时间,单位分钟。发送后需间隔该时间才允许重发。
|
||||
* NULL 或 <= 0:不限制重复发送
|
||||
* > 0:发送后需等待指定分钟后才可重新发送
|
||||
*/
|
||||
private final Integer retryTime;
|
||||
|
||||
/**
|
||||
* 短信参数对象类
|
||||
*/
|
||||
|
||||
@@ -51,9 +51,8 @@ public class SmsService {
|
||||
|
||||
// 校验参数类型是否匹配
|
||||
if (!template.getVariableClass().isInstance(variable)) {
|
||||
log.error("短信参数类型不匹配: template={}, expectedClass={}, actualClass={}",
|
||||
template.name(), template.getVariableClass().getName(), variable.getClass().getName());
|
||||
return false;
|
||||
log.error("短信参数类型不匹配: template={}, expectedClass={}, actualClass={}", template.name(), template.getVariableClass().getName(), variable.getClass().getName());
|
||||
throw new RuntimeException("短信发送失败");
|
||||
}
|
||||
|
||||
// 判断是否激活验证码模式
|
||||
@@ -178,10 +177,19 @@ public class SmsService {
|
||||
private boolean sendVerifyCode(String phone, SmsTemplateEnum template, UniversalSmsVariable variable) {
|
||||
String key = buildVerifyCodeKey(template, phone);
|
||||
|
||||
// 检查是否重复发送
|
||||
// 检查是否允许重发
|
||||
if (redisServerTool.hasKey(key)) {
|
||||
log.warn("验证码尚未过期,请勿重复发送: phone={}, template={}", phone, template.name());
|
||||
return false;
|
||||
if (template.getRetryTime() != null && template.getRetryTime() > 0) {
|
||||
// 配置了重发间隔且大于0:判断已过时间是否超过 retryTime
|
||||
Long remainSeconds = redisServerTool.getExpire(key);
|
||||
long elapsedSeconds = template.getEffectiveTime() * 60L - (remainSeconds != null ? remainSeconds : 0L);
|
||||
if (elapsedSeconds < template.getRetryTime() * 60L) {
|
||||
log.warn("重发间隔未到,请{}分钟后再试: phone={}, template={}", template.getRetryTime(), phone, template.name());
|
||||
throw new RuntimeException("请勿重复发送");
|
||||
}
|
||||
// 已超过重发间隔,允许重新发送,后续会覆盖旧验证码
|
||||
}
|
||||
// retryTime 为 null 或 <= 0 时不限制重复发送
|
||||
}
|
||||
|
||||
// 转换参数对象为 Map<String, String>
|
||||
|
||||
Reference in New Issue
Block a user