Răsfoiți Sursa

优化-改造短信发送模块

xiaof 2 ani în urmă
părinte
comite
063820432b

+ 3 - 3
paper-library-common/src/main/java/com/qmth/paper/library/common/service/impl/BasicVerifyCodeServiceImpl.java

@@ -10,6 +10,7 @@ import com.qmth.paper.library.common.enums.ExceptionResultEnum;
 import com.qmth.paper.library.common.mapper.BasicVerifyCodeMapper;
 import com.qmth.paper.library.common.service.BasicVerifyCodeService;
 import com.qmth.paper.library.common.service.CommonCacheService;
+import com.qmth.paper.library.common.util.CodeUtil;
 import com.qmth.paper.library.common.util.SmsSendUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
@@ -58,9 +59,8 @@ public class BasicVerifyCodeServiceImpl extends ServiceImpl<BasicVerifyCodeMappe
         }
         try {
             // 验证码
-            StringBuilder sb = new StringBuilder();
-            sb.append((int) ((Math.random() * 9 + 1) * 1000));
-            String verifyCode = sb.toString();
+            String normalCode = dictionaryConfig.smsDomain().getSmsNormalCode();
+            String verifyCode = CodeUtil.getVerificationCode(normalCode);
 
             String templateCode = dictionaryConfig.smsDomain().getVerificationCode();
             Map<String, Object> templateParam = new HashMap<>();

+ 21 - 0
paper-library-common/src/main/java/com/qmth/paper/library/common/util/CodeUtil.java

@@ -0,0 +1,21 @@
+package com.qmth.paper.library.common.util;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * 生成随机数
+ */
+public class CodeUtil {
+
+    public static String getVerificationCode(String normalCode) {
+        StringBuilder sb = new StringBuilder();
+        sb.append((int) ((Math.random() * 9 + 1) * 1000));
+        String verifyCode = sb.toString();
+
+        if (StringUtils.isNotBlank(normalCode) && verifyCode.equals(normalCode)) {
+            getVerificationCode(normalCode);
+        }
+
+        return verifyCode;
+    }
+}

+ 15 - 7
paper-library-common/src/main/java/com/qmth/paper/library/common/util/SmsSendUtil.java

@@ -1,5 +1,6 @@
 package com.qmth.paper.library.common.util;
 
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.boot.core.sms.model.SmsSendRequest;
 import com.qmth.boot.core.sms.model.SmsSendResponse;
 import com.qmth.boot.core.sms.service.SmsService;
@@ -11,6 +12,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.io.UnsupportedEncodingException;
 import java.util.Map;
 
 /**
@@ -18,7 +20,8 @@ import java.util.Map;
  */
 @Component
 public class SmsSendUtil {
-    private final static Logger log = LoggerFactory.getLogger(SmsSendUtil.class);
+
+    private static final Logger log = LoggerFactory.getLogger(SmsSendUtil.class);
 
     @Resource
     private SmsService smsService;
@@ -35,13 +38,14 @@ public class SmsSendUtil {
      * @param templateParam 模版变量
      * @return bizId 本次操作业务标识
      */
-    public String sendSms(String phoneNumber, String templateCode, Map<String, Object> templateParam) {
+    public String sendSms(String phoneNumber, String templateCode, Map<String, Object> templateParam) throws UnsupportedEncodingException {
         validDictionaryConfig(dictionaryConfig);
         SmsSendRequest smsSendRequest = new SmsSendRequest();
         // 必填,发送手机号
         smsSendRequest.setPhoneNumber(phoneNumber);
         // 必填,短信签名
-        smsSendRequest.setSignName(dictionaryConfig.smsDomain().getSmsSignName());
+        String signName = dictionaryConfig.smsDomain().getSmsSignName();
+        smsSendRequest.setSignName(signName);
         // 必填,短信模版编号
         smsSendRequest.setTemplateCode(templateCode);
         //模版变量,可选
@@ -49,11 +53,15 @@ public class SmsSendUtil {
             smsSendRequest.setTemplateParam(templateParam);
         }
 
-        SmsSendResponse smsSendResponse = smsService.sendSms(smsSendRequest);
-        if (smsSendResponse == null) {
-            throw ExceptionResultEnum.ERROR.exception("短信发送失败");
+        try {
+            SmsSendResponse smsSendResponse = smsService.sendSms(smsSendRequest);
+            if (smsSendResponse == null) {
+                throw ExceptionResultEnum.ERROR.exception("短信发送失败");
+            }
+            return smsSendResponse.getBizId();
+        } catch (ApiException e) {
+            throw new RuntimeException(e.getMessage());
         }
-        return smsSendResponse.getBizId();
     }
 
     /**

+ 37 - 0
paper-library-common/src/main/java/com/qmth/paper/library/common/util/StrUtil.java

@@ -0,0 +1,37 @@
+package com.qmth.paper.library.common.util;
+
+/**
+ * 字符串工具类
+ */
+public class StrUtil {
+
+
+    /**
+     * 中文转换为unicode编码
+     *
+     * @param inStr 中文
+     */
+    public static String getStrUnicode(String inStr) {
+        StringBuffer unicode = new StringBuffer();
+        char c;
+        int bit;
+        String tmp;
+        for (int i = 0; i < inStr.length(); i++) {
+            c = inStr.charAt(i);
+            if (c > 255) {
+                unicode.append("\\u");
+                bit = (c >>> 8);
+                tmp = Integer.toHexString(bit);
+                if (tmp.length() == 1) unicode.append("0");
+                unicode.append(tmp);
+                bit = (c & 0xFF);
+                tmp = Integer.toHexString(bit);
+                if (tmp.length() == 1) unicode.append("0");
+                unicode.append(tmp);
+            } else {
+                unicode.append(c);
+            }
+        }
+        return (new String(unicode));
+    }
+}

+ 4 - 4
paper-library/src/main/resources/application.properties

@@ -42,8 +42,8 @@ com.qmth.cache.expire-after-write=8h
 com.qmth.logging.root-level=info
 com.qmth.logging.file-path=logs/paper-library.log
 
-com.qmth.solar.access-key=274f823e5f59410f8b3bb6edcd8e2b6e
-com.qmth.solar.access-secret=y7AO6W0TOdTF8HpWBwGHbp3wfIHsmUKr
+#com.qmth.solar.access-key=274f823e5f59410f8b3bb6edcd8e2b6e
+#com.qmth.solar.access-secret=y7AO6W0TOdTF8HpWBwGHbp3wfIHsmUKr
 #com.qmth.solar.license=/Volumes/extend/\u542F\u660E/\u5206\u5E03\u5F0F\u5370\u5237/\u5206\u5E03\u5F0F\u5370\u5237&\u6559\u7814\u5206\u6790v3.1.0/tc-dev-wl.lic
 
 spring.cache.ehcache.config=classpath:ehcache/ehcache.xml
@@ -52,6 +52,6 @@ spring.jackson.time-zone=GMT+8
 
 #-----------------sms config--------------
 com.qmth.sms.server=https://solar.qmth.com.cn
-sms.config.smsNormalCode=qmth
-sms.config.smsSignName=\u9038\u6559\u4E91
+sms.config.smsNormalCode=3577
+sms.config.smsSignName=\u9038\u6559\u4e91
 sms.config.verificationCode=SMS_147416565

+ 37 - 0
paper-library/src/test/java/com/qmth/paper/library/SmsTest.java

@@ -0,0 +1,37 @@
+package com.qmth.paper.library;
+
+import com.qmth.paper.library.common.config.DictionaryConfig;
+import com.qmth.paper.library.common.util.CodeUtil;
+import com.qmth.paper.library.common.util.SmsSendUtil;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.annotation.Resource;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 短信工具测试类
+ */
+@SpringBootTest
+@RunWith(SpringRunner.class)
+public class SmsTest {
+
+    @Resource
+    SmsSendUtil smsSendUtil;
+
+    @Resource
+    DictionaryConfig dictionaryConfig;
+
+//    @Test
+    public void smsSend() throws UnsupportedEncodingException {
+        String phoneNumber = "19945020703";
+        String templateCode = dictionaryConfig.smsDomain().getVerificationCode();
+        Map<String, Object> map = new HashMap<>();
+        String normalCode = dictionaryConfig.smsDomain().getSmsNormalCode();
+        map.put("code", CodeUtil.getVerificationCode(normalCode));
+        smsSendUtil.sendSms(phoneNumber, templateCode, map);
+    }
+}