Browse Source

chore[litemall-core]:短信消息类型重新命名以及添加测试类

Junling Bu 7 years ago
parent
commit
80cf0a2443

+ 2 - 2
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java

@@ -26,8 +26,8 @@ class ExecutorConfig {
     @Value("${spring.notify.queueCapacity}")
     private int queueCapacity;
 
-    @Bean(name = "nofityAsync")
-    public Executor nofityAsync() {
+    @Bean(name = "notifyAsync")
+    public Executor notifyAsync() {
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
         executor.setCorePoolSize(corePoolSize);
         executor.setMaxPoolSize(maxPoolSize);

+ 19 - 6
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java

@@ -34,20 +34,20 @@ public class LitemallNotifyService {
     /**
      * 短信模版通知
      * @param phoneNumber   接收通知的电话号码
-     * @param params        通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
      * @param notifyType    通知类别,通过该枚举值在配置文件中获取相应的模版ID
+     * @param params        通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
      */
-    public void notifySMSTemplate(String phoneNumber, String[] params, NotifyUtils.NotifyType notifyType) {
+    public void notifySMSTemplate(String phoneNumber, NotifyUtils.NotifyType notifyType, String[] params) {
         if (!sendSMSEnable)
             return;
 
         int templateId = -1;
         switch (notifyType) {
-            case PAY_COMPLATED:
-                templateId = Integer.parseInt(environment.getProperty("spring.sms.template.pay.complated"));
+            case PAY_SUCCEED:
+                templateId = Integer.parseInt(environment.getProperty("spring.sms.template.paySucceed"));
                 break;
-            case VERIFICATIONCODE:
-                templateId = Integer.parseInt(environment.getProperty("spring.sms.template.verificationcode"));
+            case CAPTCHA:
+                templateId = Integer.parseInt(environment.getProperty("spring.sms.template.captcha"));
                 break;
         }
 
@@ -56,6 +56,19 @@ public class LitemallNotifyService {
     }
 
     /**
+     * 短信模版通知
+     * @param phoneNumber   接收通知的电话号码
+     * @param templateId    模板ID
+     * @param params        通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
+     */
+    public void notifySMSTemplate(String phoneNumber, int templateId, String[] params) {
+        if (!sendSMSEnable)
+            return;
+
+         smsSendService.sendSMSWithTemplate(phoneNumber, templateId, params);
+    }
+
+    /**
      * 发送邮件通知,接收者在spring.mail.sendto中指定
      * @param setSubject    邮件标题
      * @param setText       邮件内容

+ 2 - 2
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/MailSendService.java

@@ -27,7 +27,7 @@ class MailSendService {
      * @param setSubject 邮件标题
      * @param setText 邮件内容
      */
-    @Async("nofityAsync")
+    @Async("notifyAsync")
     public void sendEmail(String setSubject, String setText) {
         try {
             final MimeMessage mimeMessage = mailSender.createMimeMessage();
@@ -40,7 +40,7 @@ class MailSendService {
             mailSender.send(mimeMessage);
 
         } catch (Exception ex) {
-
+            ex.printStackTrace();
         }
     }
 }

+ 5 - 1
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/NotifyUtils.java

@@ -3,8 +3,12 @@ package org.linlinjava.litemall.core.notify;
 public class NotifyUtils {
     /**
      * 该枚举定义了所有的需要通知的事件,调用通知时作为参数
+     *
+     * PAY_SUCCEED 支付成功,通常用于用户支付成功
+     * CAPTCHA 验证码,通常用于登录、注册、找回密码
      */
     public enum NotifyType {
-        PAY_COMPLATED, REGISTER, VERIFICATIONCODE,
+        PAY_SUCCEED,
+        CAPTCHA
     }
 }

+ 4 - 4
litemall-core/src/main/java/org/linlinjava/litemall/core/notify/SMSSendService.java

@@ -23,14 +23,14 @@ class SMSSendService {
     @Value("${spring.sms.sign}")
     private String smsSign;
 
-    @Async("nofityAsync")
+    @Async("notifyAsync")
     public void sendSMS(String phoneNumber, String content) {
         try {
             SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
             SmsSingleSenderResult result = ssender.send(0, "86", phoneNumber,
                     content, "", "");
 
-            System.out.println(result);
+//            System.out.println(result);
         } catch (HTTPException e) {
             // HTTP响应码错误
             e.printStackTrace();
@@ -43,13 +43,13 @@ class SMSSendService {
         }
     }
 
-    @Async("nofityAsync")
+    @Async("notifyAsync")
     public void sendSMSWithTemplate(String phoneNumber, int templateId, String[] params) {
         try {
             SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
             SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,
                     templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
-            System.out.println(result);
+//            System.out.println(result);
         } catch (HTTPException e) {
             // HTTP响应码错误
             e.printStackTrace();

+ 5 - 5
litemall-core/src/main/resources/notify.properties

@@ -1,9 +1,9 @@
 # 邮件发送配置
 sprint.mail.enable=false
 spring.mail.host=smtp.exmail.qq.com
-spring.mail.username=ex@ex.com.cn
+spring.mail.username=xxxxxx
 spring.mail.password=xxxxxx
-spring.mail.sendto=ex@qq.com
+spring.mail.sendto=example@qq.com
 
 # 短信发送配置
 spring.sms.enable=false
@@ -12,10 +12,10 @@ spring.sms.appkey=xxxxxx
 spring.sms.sign=xxxxxx
 
 # 短信模板消息配置
-# 请在腾讯短信平台配置通知消息模板,然后模板ID这里参数
+# 请在腾讯短信平台配置通知消息模板,然后这里设置不同短信模板ID
 # 请参考LitemallNotifyService.notifySMSTemplate
-spring.sms.template.pay.complated=111111
-spring.sms.template.verificationcode=111111
+spring.sms.template.paySucceed=111111
+spring.sms.template.captcha=222222
 
 # 发送线程池配置
 spring.notify.corePoolSize=5