Merge branch 'dev-shiheng' of https://gitee.com/iotkit-open-source/iotkit-parent into dev-shiheng

V0.5.x
荭琪枫 2023-05-11 10:43:55 +08:00
commit 5d44b3498a
9 changed files with 117 additions and 25 deletions

View File

@ -22,6 +22,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -0,0 +1,19 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.message.config;
import io.vertx.core.Vertx;
public enum VertxManager {
INSTANCE;
public Vertx getVertx() {
return Vertx.vertx();
}
}

View File

@ -1,8 +1,9 @@
package cc.iotkit.message.notify; package cc.iotkit.message.listener;
import cc.iotkit.message.config.VertxManager;
import cc.iotkit.message.model.DingTalkMessage; import cc.iotkit.message.model.DingTalkMessage;
import cc.iotkit.message.model.Message; import cc.iotkit.message.model.Message;
import io.vertx.core.Vertx; import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient; import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -12,23 +13,19 @@ import lombok.extern.slf4j.Slf4j;
* description: * description:
**/ **/
@Slf4j @Slf4j
public class DingTalkEventListener implements EventListener{ public class DingTalkEventListener implements EventListener {
private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s"; private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s";
@Override @Override
public void doEvent(Message message) { public void doEvent(Message message) {
WebClient client = WebClient.create(Vertx.vertx()); WebClient client = WebClient.create(VertxManager.INSTANCE.getVertx());
String url = String.format(baseUrl, message.getKey()); String url = String.format(baseUrl, message.getKey());
DingTalkMessage qyWechatMessage = DingTalkMessage.builder() DingTalkMessage qyWechatMessage = DingTalkMessage.builder()
.msgtype("text") .msgtype("text")
.text(DingTalkMessage.MessageContent.builder().content(message.getContent()).build()) .text(DingTalkMessage.MessageContent.builder().content(message.getContent()).build())
.build(); .build();
client.post(url).sendJson(qyWechatMessage, rs -> { client.post(url).sendJson(qyWechatMessage)
if (rs.succeeded()) { .onSuccess(response -> log.info("Received response with status code" + response.statusCode()))
log.info("发送成功."); .onFailure(err -> log.error("Something went wrong " + err.getMessage()));
}
if (rs.failed()) {
log.info("发送失败.");
}
});
} }
} }

View File

@ -0,0 +1,50 @@
package cc.iotkit.message.listener;
import cc.iotkit.message.model.EmailConfig;
import cc.iotkit.message.model.Message;
import cc.iotkit.message.notify.EventListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
@Slf4j
public class EmailEventListener implements EventListener {
@Override
public void doEvent(Message message) {
EmailConfig emailConfig = new EmailConfig();
JavaMailSenderImpl jms = new JavaMailSenderImpl();
jms.setHost(emailConfig.getHost());
jms.setUsername(emailConfig.getUserName());
jms.setPassword(emailConfig.getPassWord());
jms.setDefaultEncoding("utf-8");
Properties p = new Properties();
p.setProperty("mail.smtp.auth", "true");
jms.setJavaMailProperties(p);
MimeMessage mimeMessage = jms.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
//收件人
String[] split = emailConfig.getTo().split(",");
messageHelper.setTo(split);
//标题
messageHelper.setSubject(emailConfig.getTitle());
//内容
messageHelper.setText(emailConfig.getContent(), true);
//发件人
messageHelper.setFrom(emailConfig.getFrom());
jms.send(mimeMessage);
} catch (MessagingException e) {
log.error("发送邮件失败.", e);
}
}
}

View File

@ -1,13 +1,14 @@
package cc.iotkit.message.notify; package cc.iotkit.message.listener;
import cc.iotkit.message.model.Message; import cc.iotkit.message.model.Message;
import cc.iotkit.message.notify.EventListener;
/** /**
* author: * author:
* date: 2023-05-08 15:09 * date: 2023-05-08 15:09
* description: * description:
**/ **/
public class MessageEventListener implements EventListener{ public class MessageEventListener implements EventListener {
@Override @Override
public void doEvent(Message message) { public void doEvent(Message message) {

View File

@ -1,8 +1,9 @@
package cc.iotkit.message.notify; package cc.iotkit.message.listener;
import cc.iotkit.message.config.VertxManager;
import cc.iotkit.message.model.Message; import cc.iotkit.message.model.Message;
import cc.iotkit.message.model.QyWechatMessage; import cc.iotkit.message.model.QyWechatMessage;
import io.vertx.core.Vertx; import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient; import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -20,19 +21,14 @@ public class QyWechatEventListener implements EventListener {
@Override @Override
public void doEvent(Message message) { public void doEvent(Message message) {
WebClient client = WebClient.create(Vertx.vertx()); WebClient client = WebClient.create(VertxManager.INSTANCE.getVertx());
String url = String.format(baseUrl, message.getKey()); String url = String.format(baseUrl, message.getKey());
QyWechatMessage qyWechatMessage = QyWechatMessage.builder() QyWechatMessage qyWechatMessage = QyWechatMessage.builder()
.msgtype("text") .msgtype("text")
.text(QyWechatMessage.MessageContent.builder().content(message.getContent()).build()) .text(QyWechatMessage.MessageContent.builder().content(message.getContent()).build())
.build(); .build();
client.post(url).sendJson(qyWechatMessage, rs -> { client.post(url).sendJson(qyWechatMessage)
if (rs.succeeded()) { .onSuccess(response -> log.info("Received response with status code" + response.statusCode()))
log.info("发送成功."); .onFailure(err -> log.error("Something went wrong " + err.getMessage()));
}
if (rs.failed()) {
log.info("发送失败.");
}
});
} }
} }

View File

@ -0,0 +1,21 @@
package cc.iotkit.message.model;
import lombok.Data;
import java.io.Serializable;
/**
* author:
* date: 2023-05-08 15:58
* description:
**/
@Data
public class EmailConfig implements Serializable {
private String host;
private String userName;
private String passWord;
private String from;
private String to;
private String title;
private String content;
}

View File

@ -1,5 +1,7 @@
package cc.iotkit.message.model; package cc.iotkit.message.model;
import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -7,5 +9,6 @@ import java.io.Serializable;
* date: 2023-05-08 15:58 * date: 2023-05-08 15:58
* description: * description:
**/ **/
@Data
public class EmailMessage implements Serializable { public class EmailMessage implements Serializable {
} }

View File

@ -11,4 +11,5 @@ import lombok.Data;
public class Message { public class Message {
private String key; private String key;
private String content; private String content;
private Long channelId;
} }