Browse Source

短信逻辑优化

woody 1 year ago
parent
commit
cfd5f26ed4

+ 2 - 1
framework-sms/src/main/java/com/chelvc/framework/sms/NormalSmsHandler.java

@@ -12,6 +12,7 @@ public interface NormalSmsHandler {
      *
      * @param content 短信内容
      * @param mobiles 手机号数组
+     * @return true/false
      */
-    void send(String content, String... mobiles);
+    boolean send(String content, String... mobiles);
 }

+ 10 - 0
framework-sms/src/main/java/com/chelvc/framework/sms/PromotionSmsHandler.java

@@ -0,0 +1,10 @@
+package com.chelvc.framework.sms;
+
+/**
+ * 营销短信操作接口
+ *
+ * @author Woody
+ * @date 2021/11/23
+ */
+public interface PromotionSmsHandler extends NormalSmsHandler {
+}

+ 1 - 1
framework-sms/src/main/java/com/chelvc/framework/sms/config/AliyunSmsProperties.java

@@ -14,7 +14,7 @@ import org.springframework.context.annotation.Configuration;
 @Data
 @Configuration
 @ConfigurationProperties("platform.sms.aliyun")
-@ConditionalOnProperty(prefix = "platform.sms.aliyun", value = {"id", "secret"})
+@ConditionalOnProperty(prefix = "platform.sms.aliyun", value = {"key", "secret"})
 public class AliyunSmsProperties {
     /**
      * AccessKey ID

+ 32 - 1
framework-sms/src/main/java/com/chelvc/framework/sms/config/SmsConfigurer.java

@@ -2,13 +2,17 @@ package com.chelvc.framework.sms.config;
 
 import com.aliyun.dysmsapi20170525.Client;
 import com.aliyun.teaopenapi.models.Config;
+import com.chelvc.framework.sms.support.SwitchableCaptchaSmsHandler;
 import com.github.qcloudsms.SmsSingleSender;
 import com.github.qcloudsms.httpclient.PoolingHTTPClient;
 import lombok.RequiredArgsConstructor;
+import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.support.GenericApplicationContext;
 
 /**
  * 短信配置
@@ -18,7 +22,10 @@ import org.springframework.context.annotation.Configuration;
  */
 @Configuration
 @RequiredArgsConstructor(onConstructor = @__(@Autowired))
-public class SmsConfigurer {
+public class SmsConfigurer implements BeanPostProcessor {
+    private volatile boolean initialized;
+    private final GenericApplicationContext applicationContext;
+
     /**
      * 构建阿里云短信配置实例
      *
@@ -61,4 +68,28 @@ public class SmsConfigurer {
     public SmsSingleSender tencentSmsSender(TencentSmsProperties properties) {
         return new SmsSingleSender(properties.getAppid(), properties.getSecret(), new PoolingHTTPClient());
     }
+
+    /**
+     * 初始化可切换短信处理器
+     */
+    private void initializeSwitchableSmsHandler() {
+        // 初始化可切换验证码短信处理器
+        if (this.applicationContext.containsBean(AliyunSmsProperties.class.getName())
+                || this.applicationContext.containsBean(TencentSmsProperties.class.getName())) {
+            this.applicationContext.registerBean(SwitchableCaptchaSmsHandler.class);
+        }
+    }
+
+    @Override
+    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+        if (!this.initialized) {
+            synchronized (this) {
+                if (!this.initialized) {
+                    this.initializeSwitchableSmsHandler();
+                    this.initialized = true;
+                }
+            }
+        }
+        return bean;
+    }
 }

+ 8 - 7
framework-sms/src/main/java/com/chelvc/framework/sms/support/DefaultCaptchaSmsHandler.java → framework-sms/src/main/java/com/chelvc/framework/sms/support/SwitchableCaptchaSmsHandler.java

@@ -23,20 +23,18 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 /**
- * 短信验证码操作默认实现
+ * 可切换验证码短信处理器实现
  *
  * @author Woody
  * @date 2023/4/5
  */
 @Slf4j
-@Component
 @ConditionalOnBean(CaptchaSmsProperties.class)
 @RequiredArgsConstructor(onConstructor = @__(@Autowired))
-public class DefaultCaptchaSmsHandler implements CaptchaSmsHandler {
+public class SwitchableCaptchaSmsHandler implements CaptchaSmsHandler {
     /**
      * 验证码配置引用
      */
@@ -110,9 +108,12 @@ public class DefaultCaptchaSmsHandler implements CaptchaSmsHandler {
     @Override
     public void using(@NonNull String mobile, @NonNull Consumer<Captcha> consumer) {
         Captcha captcha = this.getCaptcha(mobile);
-        consumer.accept(captcha);
-        if (captcha != null) {
-            this.redisTemplate.delete(this.key(mobile));
+        try {
+            consumer.accept(captcha);
+        } finally {
+            if (captcha != null) {
+                this.redisTemplate.delete(this.key(mobile));
+            }
         }
     }
 

+ 10 - 4
framework-wechat/src/main/java/com/chelvc/framework/wechat/config/WechatConfigurer.java

@@ -27,13 +27,13 @@ public class WechatConfigurer implements BeanPostProcessor {
     private final WechatHandler wechatHandler;
     private final WechatProperties properties;
     private final GenericApplicationContext applicationContext;
-    private boolean initialized;
+    private volatile boolean initialized;
 
     /**
      * 初始化微信公众号处理器
      */
     private void initializeWechatPublicHandler() {
-        if (!this.initialized && !CollectionUtils.isEmpty(this.properties.getPublics())) {
+        if (!CollectionUtils.isEmpty(this.properties.getPublics())) {
             this.properties.getPublics().forEach(config -> {
                 WechatPublicHandler handler = new DefaultWechatPublicHandler(
                         this.restTemplate, this.wechatHandler, config
@@ -42,13 +42,19 @@ public class WechatConfigurer implements BeanPostProcessor {
                 RootBeanDefinition definition = new RootBeanDefinition(WechatPublicHandler.class, () -> handler);
                 this.applicationContext.registerBeanDefinition(name, definition);
             });
-            this.initialized = true;
         }
     }
 
     @Override
     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
-        this.initializeWechatPublicHandler();
+        if (!this.initialized) {
+            synchronized (this) {
+                if (!this.initialized) {
+                    this.initializeWechatPublicHandler();
+                    this.initialized = true;
+                }
+            }
+        }
         return bean;
     }
 }