Parcourir la source

优化短信逻辑

woody il y a 1 an
Parent
commit
70b6198fd4

+ 38 - 2
framework-base/src/main/java/com/chelvc/framework/base/context/ApplicationContextHolder.java

@@ -298,7 +298,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
     }
 
     /**
-     * 获取可以为空的对象实例
+     * 获取对象实例
      *
      * @param type     对象类型
      * @param required 对象实例是否必须存在
@@ -328,7 +328,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
     }
 
     /**
-     * 获取可以为空的对象实例
+     * 获取对象实例
      *
      * @param name     对象名称
      * @param required 对象实例是否必须存在
@@ -347,6 +347,42 @@ public class ApplicationContextHolder implements ApplicationContextAware {
         }
     }
 
+    /**
+     * 获取对象实例
+     *
+     * @param name 对象名称
+     * @param <T>  对象类型泛型
+     * @return 对象实例
+     */
+    public static <T> T getSafeBean(String name) {
+        return getSafeBean(name, true);
+    }
+
+    /**
+     * 获取对象实例
+     *
+     * @param name     对象名称
+     * @param required 对象实例是否必须存在
+     * @param <T>      对象类型泛型
+     * @return 对象实例
+     */
+    public static <T> T getSafeBean(String name, boolean required) {
+        T bean = getBean(name, false);
+        if (bean != null) {
+            return bean;
+        }
+
+        // 如果名称包含包路径,则尝试获取简单类名获取
+        int separator = name.lastIndexOf('.');
+        if (separator >= 0 && (bean = getBean(StringUtils.hump(name.substring(separator + 1)), false)) != null) {
+            return bean;
+        }
+        if (required) {
+            throw new NoSuchBeanDefinitionException(name);
+        }
+        return null;
+    }
+
     /**
      * 获取所有application.yml文件资源
      *

+ 17 - 0
framework-base/src/main/java/com/chelvc/framework/base/util/StringUtils.java

@@ -884,6 +884,23 @@ public final class StringUtils {
         return isEmpty(original) ? original : getPattern(regex).matcher(original).replaceAll(EMPTY);
     }
 
+    /**
+     * 字符串转驼峰格式
+     *
+     * @param original 原始字符串
+     * @return 转换后字符串
+     */
+    public static String hump(String original) {
+        if (isEmpty(original)) {
+            return original;
+        }
+        char c = original.charAt(0);
+        if (Character.isLowerCase(c)) {
+            return original;
+        }
+        return Character.toLowerCase(c) + original.substring(1);
+    }
+
     /**
      * 驼峰格式转下划线格式
      *

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

@@ -2,6 +2,7 @@ package com.chelvc.framework.sms.config;
 
 import com.aliyun.dysmsapi20170525.Client;
 import com.aliyun.teaopenapi.models.Config;
+import com.chelvc.framework.base.util.StringUtils;
 import com.chelvc.framework.sms.support.SwitchableCaptchaSmsHandler;
 import com.github.qcloudsms.SmsSingleSender;
 import com.github.qcloudsms.httpclient.PoolingHTTPClient;
@@ -75,7 +76,9 @@ public class SmsConfigurer implements BeanPostProcessor {
     private void initializeSwitchableSmsHandler() {
         // 初始化可切换验证码短信处理器
         if (this.applicationContext.containsBean(AliyunSmsProperties.class.getName())
-                || this.applicationContext.containsBean(TencentSmsProperties.class.getName())) {
+                || this.applicationContext.containsBean(StringUtils.hump(AliyunSmsProperties.class.getSimpleName()))
+                || this.applicationContext.containsBean(TencentSmsProperties.class.getName())
+                || this.applicationContext.containsBean(StringUtils.hump(TencentSmsProperties.class.getSimpleName()))) {
             this.applicationContext.registerBean(SwitchableCaptchaSmsHandler.class);
         }
     }

+ 2 - 2
framework-sms/src/main/java/com/chelvc/framework/sms/support/SwitchableCaptchaSmsHandler.java

@@ -62,7 +62,7 @@ public class SwitchableCaptchaSmsHandler implements CaptchaSmsHandler {
      * @return 模版短信处理器
      */
     private TemplateSmsHandler getTemplateSmsHandler() {
-        return ApplicationContextHolder.getBean(ApplicationContextHolder.getSafeProperty(
+        return ApplicationContextHolder.getSafeBean(ApplicationContextHolder.getSafeProperty(
                 "platform.sms.captcha.handler", AliyunSmsHandler.class.getName()
         ));
     }
@@ -103,7 +103,7 @@ public class SwitchableCaptchaSmsHandler implements CaptchaSmsHandler {
             }
             this.redisTemplate.opsForValue().set(key, captcha, this.properties.getExpiration(), TimeUnit.SECONDS);
             if (log.isDebugEnabled()) {
-                log.debug(HttpUtils.message(SessionContextHolder.getRequest(), "CAPTCHA", code));
+                log.debug(HttpUtils.message(SessionContextHolder.getRequest(), mobile, code));
             }
             return SmsSession.builder().token(token).expiration(this.properties.getExpiration()).build();
         } catch (RuntimeException e) {