瀏覽代碼

优化短信发送逻辑

woody 1 年之前
父節點
當前提交
c6157934b9

+ 8 - 9
framework-sms/src/main/java/com/chelvc/framework/sms/TemplateSmsHandler.java

@@ -14,6 +14,14 @@ public interface TemplateSmsHandler {
      */
     String SMS_SEND_FREQUENTLY = "SMS_SEND_FREQUENTLY";
 
+    /**
+     * 发送短信验证码
+     *
+     * @param template 模版标识
+     * @param captcha  验证码
+     */
+    void send(String template, Captcha captcha);
+
     /**
      * 发送短信
      *
@@ -31,13 +39,4 @@ public interface TemplateSmsHandler {
      * @param parameters 模版参数
      */
     void send(String mobile, String template, Map<String, ?> parameters);
-
-    /**
-     * 发送短信验证码
-     *
-     * @param mobile   手机号
-     * @param template 模版标识
-     * @param code     验证码
-     */
-    void sendCaptcha(String mobile, String template, String code);
 }

+ 9 - 8
framework-sms/src/main/java/com/chelvc/framework/sms/support/AliyunSmsHandler.java

@@ -9,6 +9,7 @@ import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
 import com.chelvc.framework.base.context.JacksonContextHolder;
 import com.chelvc.framework.base.exception.ResourceUnavailableException;
+import com.chelvc.framework.sms.Captcha;
 import com.chelvc.framework.sms.TemplateSmsHandler;
 import com.chelvc.framework.sms.config.AliyunSmsProperties;
 import com.google.common.collect.ImmutableMap;
@@ -33,6 +34,11 @@ public class AliyunSmsHandler implements TemplateSmsHandler {
     private final Client client;
     private final AliyunSmsProperties properties;
 
+    @Override
+    public void send(@NonNull String template, @NonNull Captcha captcha) {
+        this.send(captcha.getMobile(), template, ImmutableMap.of("code", captcha.getCode()));
+    }
+
     @Override
     public void send(String mobile, String template, String... parameters) {
         throw new UnsupportedOperationException("Not support method");
@@ -51,24 +57,19 @@ public class AliyunSmsHandler implements TemplateSmsHandler {
         try {
             response = this.client.sendSms(request);
         } catch (Exception e) {
-            log.warn("Aliyun sms send failed: {}", e.getMessage());
+            log.warn("Aliyun sms send failed: {}, {}", mobile, e.getMessage());
             return;
         }
         if (debug) {
-            log.debug("Aliyun sms response: {}", JacksonContextHolder.serialize(response));
+            log.debug("Aliyun sms response: {}, {}", mobile, JacksonContextHolder.serialize(response));
         }
         SendSmsResponseBody body = response.getBody();
         if (!Objects.equals(body.getCode(), "OK")) {
-            log.warn("Aliyun sms send failed: [{}] {}", body.getCode(), body.getMessage());
+            log.warn("Aliyun sms send failed[{}]: {}, {}", body.getCode(), mobile, body.getMessage());
             if (Objects.equals(body.getCode(), "isv.BUSINESS_LIMIT_CONTROL")) {
                 throw new ResourceUnavailableException(SMS_SEND_FREQUENTLY, body.getMessage());
             }
             throw new ResourceUnavailableException(body.getMessage());
         }
     }
-
-    @Override
-    public void sendCaptcha(@NonNull String mobile, @NonNull String template, @NonNull String code) {
-        this.send(mobile, template, ImmutableMap.of("code", code));
-    }
 }

+ 3 - 3
framework-sms/src/main/java/com/chelvc/framework/sms/support/DefaultCaptchaSmsHandler.java

@@ -90,11 +90,11 @@ public class DefaultCaptchaSmsHandler implements CaptchaSmsHandler {
         String code = testing ? test : String.valueOf(ThreadLocalRandom.current().nextInt(100000, 1000000));
         Duration duration = Duration.ofSeconds(this.properties.getInterval());
         return RedisUtils.tryLockAround("sms:interval:" + mobile, duration, () -> {
-            if (!testing) {
-                this.getTemplateSmsHandler().sendCaptcha(mobile, this.properties.getTemplate(), code);
-            }
             String key = this.key(mobile), token = StringUtils.uuid();
             Captcha captcha = Captcha.builder().token(token).code(code).mobile(mobile).build();
+            if (!testing) {
+                this.getTemplateSmsHandler().send(this.properties.getTemplate(), captcha);
+            }
             this.redisTemplate.opsForValue().set(key, captcha, this.properties.getExpiration(), TimeUnit.SECONDS);
             return SmsSession.builder().token(token).expiration(this.properties.getExpiration()).build();
         }, () -> {

+ 9 - 8
framework-sms/src/main/java/com/chelvc/framework/sms/support/TencentSmsHandler.java

@@ -5,6 +5,7 @@ import java.util.Map;
 
 import com.chelvc.framework.base.context.JacksonContextHolder;
 import com.chelvc.framework.base.exception.ResourceUnavailableException;
+import com.chelvc.framework.sms.Captcha;
 import com.chelvc.framework.sms.TemplateSmsHandler;
 import com.chelvc.framework.sms.config.TencentSmsProperties;
 import com.github.qcloudsms.SmsSingleSender;
@@ -30,6 +31,11 @@ public class TencentSmsHandler implements TemplateSmsHandler {
     private final SmsSingleSender sender;
     private final TencentSmsProperties properties;
 
+    @Override
+    public void send(@NonNull String template, @NonNull Captcha captcha) {
+        this.send(captcha.getMobile(), template, captcha.getCode());
+    }
+
     @Override
     public void send(@NonNull String mobile, @NonNull String template, @NonNull String... parameters) {
         boolean debug = log.isDebugEnabled();
@@ -41,14 +47,14 @@ public class TencentSmsHandler implements TemplateSmsHandler {
             result = this.sender.sendWithParam(this.properties.getRegion(), mobile, Integer.parseInt(template),
                     parameters, this.properties.getSignature(), null, null);
         } catch (Exception e) {
-            log.warn("Tencent sms send failed: {}", e.getMessage());
+            log.warn("Tencent sms send failed: {}, {}", mobile, e.getMessage());
             return;
         }
         if (debug) {
-            log.debug("Tencent sms response: {}", JacksonContextHolder.serialize(result));
+            log.debug("Tencent sms response: {}, {}", mobile, JacksonContextHolder.serialize(result));
         }
         if (result.result != 0) {
-            log.warn("Tencent sms send failed: [{}] {}", result.result, result.errMsg);
+            log.warn("Tencent sms send failed[{}]: {}, {}", result.result, mobile, result.errMsg);
             if (result.result == 1023) {
                 throw new ResourceUnavailableException(SMS_SEND_FREQUENTLY, result.errMsg);
             }
@@ -60,9 +66,4 @@ public class TencentSmsHandler implements TemplateSmsHandler {
     public void send(String mobile, String template, Map<String, ?> parameters) {
         throw new UnsupportedOperationException("Not support method");
     }
-
-    @Override
-    public void sendCaptcha(@NonNull String mobile, @NonNull String template, @NonNull String code) {
-        this.send(mobile, template, code);
-    }
 }