diff --git a/litemall-core/pom.xml b/litemall-core/pom.xml
index 9855ec35..8137e305 100644
--- a/litemall-core/pom.xml
+++ b/litemall-core/pom.xml
@@ -47,6 +47,15 @@
qcloudsms
1.0.5
+
+ com.github.binarywang
+ weixin-java-miniapp
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java
index 143fc191..6a7c9c0c 100644
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/ExecutorConfig.java
@@ -19,11 +19,11 @@ import java.util.concurrent.ThreadPoolExecutor;
@EnableAsync
class ExecutorConfig {
- @Value("${spring.notify.corePoolSize}")
+ @Value("${NotifyPoolConfig.corePoolSize}")
private int corePoolSize;
- @Value("${spring.notify.maxPoolSize}")
+ @Value("${NotifyPoolConfig.maxPoolSize}")
private int maxPoolSize;
- @Value("${spring.notify.queueCapacity}")
+ @Value("${NotifyPoolConfig.queueCapacity}")
private int queueCapacity;
@Bean(name = "notifyAsync")
@@ -37,4 +37,4 @@ class ExecutorConfig {
executor.initialize();
return executor;
}
-}
+}
\ No newline at end of file
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java
index fcdaecbd..ffc78d2e 100644
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/LitemallNotifyService.java
@@ -1,55 +1,62 @@
package org.linlinjava.litemall.core.notify;
+import org.linlinjava.litemall.core.notify.util.ConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.core.env.Environment;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* Litemall商城通知服务类
*/
-@PropertySource(value = "classpath:notify.properties")
@Service("litemallNotifyService")
public class LitemallNotifyService {
@Autowired
- MailSendService mailSendService;
+ private MailSendService mailSendService;
@Autowired
- SMSSendService smsSendService;
+ private SMSSendService smsSendService;
@Autowired
- Environment environment;
+ private WXTemplateSendService wxTemplateSendService;
- @Value("${sprint.mail.enable}")
- private boolean sendMailEnable;
- @Value("${spring.sms.enable}")
- private boolean sendSMSEnable;
-
- public void notifySMSMessage(String phoneNumber,String message) {
- if (!sendSMSEnable)
+ @Async("notifyAsync")
+ public void notifySMSMessage(String phoneNumber, String message) {
+ if (!smsSendService.config.isEnable())
return;
smsSendService.sendSMS(phoneNumber, message);
}
/**
- * 短信模版通知
- * @param phoneNumber 接收通知的电话号码
+ * 微信模版消息通知
+ * @param token 通过wxMAService获取token或者通过url请求token
+ * @param touser 接收者openId
+ * @param formId 表单ID或者 prepayId
* @param notifyType 通知类别,通过该枚举值在配置文件中获取相应的模版ID
* @param params 通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
*/
- public void notifySMSTemplate(String phoneNumber, NotifyUtils.NotifyType notifyType, String[] params) {
- if (!sendSMSEnable)
+ @Async("notifyAsync")
+ public void notifyWXTemplate(String token,String touser, String formId, ConfigUtil.NotifyType notifyType, String[] params) {
+ if (!wxTemplateSendService.config.isEnable())
return;
- int templateId = -1;
- switch (notifyType) {
- case PAY_SUCCEED:
- templateId = Integer.parseInt(environment.getProperty("spring.sms.template.paySucceed"));
- break;
- case CAPTCHA:
- templateId = Integer.parseInt(environment.getProperty("spring.sms.template.captcha"));
- break;
- }
+ String templateId = ConfigUtil.getTemplateId(notifyType, wxTemplateSendService.config.getTemplate());
+
+ if (templateId != "")
+ wxTemplateSendService.sendWechatMsg(token,touser, templateId, formId, "", "", params);
+ }
+
+ /**
+ * 短信模版通知
+ *
+ * @param phoneNumber 接收通知的电话号码
+ * @param notifyType 通知类别,通过该枚举值在配置文件中获取相应的模版ID
+ * @param params 通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
+ */
+ @Async("notifyAsync")
+ public void notifySMSTemplate(String phoneNumber, ConfigUtil.NotifyType notifyType, String[] params) {
+ if (!smsSendService.config.isEnable())
+ return;
+
+ int templateId = Integer.parseInt(ConfigUtil.getTemplateId(notifyType, smsSendService.config.getTemplate()));
if (templateId != -1)
smsSendService.sendSMSWithTemplate(phoneNumber, templateId, params);
@@ -57,24 +64,28 @@ public class LitemallNotifyService {
/**
* 短信模版通知
- * @param phoneNumber 接收通知的电话号码
- * @param templateId 模板ID
- * @param params 通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
+ *
+ * @param phoneNumber 接收通知的电话号码
+ * @param templateId 模板ID
+ * @param params 通知模版内容里的参数,类似"您的验证码为{1}"中{1}的值
*/
+ @Async("notifyAsync")
public void notifySMSTemplate(String phoneNumber, int templateId, String[] params) {
- if (!sendSMSEnable)
+ if (!smsSendService.config.isEnable())
return;
- smsSendService.sendSMSWithTemplate(phoneNumber, templateId, params);
+ smsSendService.sendSMSWithTemplate(phoneNumber, templateId, params);
}
/**
* 发送邮件通知,接收者在spring.mail.sendto中指定
- * @param setSubject 邮件标题
- * @param setText 邮件内容
+ *
+ * @param setSubject 邮件标题
+ * @param setText 邮件内容
*/
+ @Async("notifyAsync")
public void notifyMailMessage(String setSubject, String setText) {
- if(!sendMailEnable)
+ if (!mailSendService.config.isEnable())
return;
mailSendService.sendEmail(setSubject, setText);
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/MailSendService.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/MailSendService.java
index 85a0311d..6966dbda 100644
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/MailSendService.java
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/MailSendService.java
@@ -1,43 +1,47 @@
package org.linlinjava.litemall.core.notify;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.mail.javamail.JavaMailSender;
+import org.linlinjava.litemall.core.notify.config.MailNotifyConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
-import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
-import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
-@PropertySource(value = "classpath:notify.properties")
@Service("mailSendService")
class MailSendService {
- @Resource
- private JavaMailSender mailSender;
+ @Autowired
+ MailNotifyConfig config;
- @Value("${spring.mail.username}")
- private String from;
+ private JavaMailSenderImpl mailSender;
- @Value("${spring.mail.sendto}")
- private String sendto;
+ private JavaMailSenderImpl getMailSender() {
+ if (mailSender == null) {
+ mailSender = new JavaMailSenderImpl();
+ mailSender.setHost(config.getHost());
+ mailSender.setUsername(config.getUsername());
+ mailSender.setPassword(config.getPassword());
+ }
+
+ return mailSender;
+ }
/**
- * 异步发送邮件通知
+ * 发送邮件通知
+ *
* @param setSubject 邮件标题
- * @param setText 邮件内容
+ * @param setText 邮件内容
*/
- @Async("notifyAsync")
public void sendEmail(String setSubject, String setText) {
try {
- final MimeMessage mimeMessage = mailSender.createMimeMessage();
+ final MimeMessage mimeMessage = getMailSender().createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
- message.setFrom(from);
- message.setTo(sendto);
+ message.setFrom(config.getUsername());
+ message.setTo(config.getSendto());
message.setSubject(setSubject);
message.setText(setText);
- mailSender.send(mimeMessage);
+ getMailSender().send(mimeMessage);
} catch (Exception ex) {
ex.printStackTrace();
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/NotifyUtils.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/NotifyUtils.java
deleted file mode 100644
index 6ffaf236..00000000
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/NotifyUtils.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.linlinjava.litemall.core.notify;
-
-public class NotifyUtils {
- /**
- * 该枚举定义了所有的需要通知的事件,调用通知时作为参数
- *
- * PAY_SUCCEED 支付成功,通常用于用户支付成功
- * CAPTCHA 验证码,通常用于登录、注册、找回密码
- */
- public enum NotifyType {
- PAY_SUCCEED,
- CAPTCHA
- }
-}
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/SMSSendService.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/SMSSendService.java
index 6b2375fd..4bc7ee8d 100644
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/SMSSendService.java
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/SMSSendService.java
@@ -4,29 +4,21 @@ import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.scheduling.annotation.Async;
+import org.linlinjava.litemall.core.notify.config.SMSNotifyConfig;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
-@PropertySource(value = "classpath:notify.properties")
+
@Service("smsSendService")
class SMSSendService {
- @Value("${spring.sms.appid}")
- private int appid;
+ @Autowired
+ SMSNotifyConfig config;
- @Value("${spring.sms.appkey}")
- private String appkey;
-
- @Value("${spring.sms.sign}")
- private String smsSign;
-
- @Async("notifyAsync")
public void sendSMS(String phoneNumber, String content) {
try {
- SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
+ SmsSingleSender ssender = new SmsSingleSender(config.getAppid(), config.getAppkey());
SmsSingleSenderResult result = ssender.send(0, "86", phoneNumber,
content, "", "");
@@ -43,12 +35,17 @@ class SMSSendService {
}
}
- @Async("notifyAsync")
+ /**
+ * 通过模版发送短信息
+ * @param phoneNumber
+ * @param templateId
+ * @param params
+ */
public void sendSMSWithTemplate(String phoneNumber, int templateId, String[] params) {
try {
- SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
+ SmsSingleSender ssender = new SmsSingleSender(config.getAppid(), config.getAppkey());
SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,
- templateId, params, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
+ templateId, params, config.getSign(), "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
// System.out.println(result);
} catch (HTTPException e) {
// HTTP响应码错误
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateMsgSendService.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateSendService.java
similarity index 78%
rename from litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateMsgSendService.java
rename to litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateSendService.java
index cdaca41f..1b55237b 100644
--- a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateMsgSendService.java
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/WXTemplateSendService.java
@@ -1,7 +1,8 @@
package org.linlinjava.litemall.core.notify;
import org.json.JSONObject;
-import org.springframework.context.annotation.PropertySource;
+import org.linlinjava.litemall.core.notify.config.WXNotifyConfig;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.net.ssl.*;
@@ -16,11 +17,13 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
- * 微信模版消息通知,未完成
+ * 微信模版消息通知
*/
-@PropertySource(value = "classpath:notify.properties")
@Service("wxTemplateMsgSendService")
-public class WXTemplateMsgSendService {
+class WXTemplateSendService {
+ @Autowired
+ WXNotifyConfig config;
+
/**
* 发送微信消息(模板消息)
*
@@ -29,19 +32,20 @@ public class WXTemplateMsgSendService {
* @param formId payId或者表单ID
* @param clickurl URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。
* @param topcolor 标题颜色
- * @param data 详细内容
+ * @param parms 详细内容
* @return
*/
- public String sendWechatMsgToUser(String token, String touser, String templatId, String formId, String clickurl, String topcolor, JSONObject data) {
- String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + token;
- JSONObject json = new JSONObject();
- json.put("touser", touser);
- json.put("template_id", templatId);
- json.put("form_id", formId);
- json.put("url", clickurl);
- json.put("topcolor", topcolor);
- json.put("data", data);
+ public String sendWechatMsg(String token, String touser, String templatId, String formId, String clickurl, String topcolor, String[] parms) {
try {
+ String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + token;
+ JSONObject json = new JSONObject();
+ json.put("touser", touser);
+ json.put("template_id", templatId);
+ json.put("form_id", formId);
+ json.put("url", clickurl);
+ json.put("topcolor", topcolor);
+ json.put("data", createParmData(parms));
+
JSONObject result = httpsRequest(tmpurl, "POST", json.toString());
// log.info("发送微信消息返回信息:" + resultJson.get("errcode"));
String errmsg = (String) result.get("errmsg");
@@ -55,6 +59,23 @@ public class WXTemplateMsgSendService {
return "success";
}
+ /**
+ * 根据参数生成对应的 json 数据
+ * @param parms
+ * @return
+ */
+ private JSONObject createParmData(String[] parms) {
+ JSONObject json = new JSONObject();
+ for (int i = 1; i <= parms.length; i++) {
+ JSONObject json2 = new JSONObject();
+ json2.put("value", parms[i-1]);
+
+ json.put("keyword" + i, json2);
+ }
+
+ return json;
+ }
+
/**
* 发送https请求
*
@@ -125,7 +146,6 @@ public class WXTemplateMsgSendService {
@Override
public X509Certificate[] getAcceptedIssuers() {
- // return new X509Certificate[0];
return null;
}
}
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/MailNotifyConfig.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/MailNotifyConfig.java
new file mode 100644
index 00000000..5e32ab61
--- /dev/null
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/MailNotifyConfig.java
@@ -0,0 +1,54 @@
+package org.linlinjava.litemall.core.notify.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "MailNotifyConfig")
+public class MailNotifyConfig {
+ private boolean enable;
+ private String host;
+ private String username;
+ private String password;
+ private String sendto;
+
+ public boolean isEnable() {
+ return enable;
+ }
+
+ public void setEnable(boolean enable) {
+ this.enable = enable;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getSendto() {
+ return sendto;
+ }
+
+ public void setSendto(String sendto) {
+ this.sendto = sendto;
+ }
+}
diff --git a/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/SMSNotifyConfig.java b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/SMSNotifyConfig.java
new file mode 100644
index 00000000..56e0ff76
--- /dev/null
+++ b/litemall-core/src/main/java/org/linlinjava/litemall/core/notify/config/SMSNotifyConfig.java
@@ -0,0 +1,58 @@
+package org.linlinjava.litemall.core.notify.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+@Component
+@ConfigurationProperties(prefix = "SMSNotifyConfig")
+public class SMSNotifyConfig {
+ private boolean enable;
+ private int appid;
+ private String appkey;
+ private String sign;
+ private List