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>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<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.Message;
import io.vertx.core.Vertx;
import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
@ -12,23 +13,19 @@ import lombok.extern.slf4j.Slf4j;
* description:
**/
@Slf4j
public class DingTalkEventListener implements EventListener{
public class DingTalkEventListener implements EventListener {
private String baseUrl = "https://oapi.dingtalk.com/robot/send?access_token=%s";
@Override
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());
DingTalkMessage qyWechatMessage = DingTalkMessage.builder()
.msgtype("text")
.text(DingTalkMessage.MessageContent.builder().content(message.getContent()).build())
.build();
client.post(url).sendJson(qyWechatMessage, rs -> {
if (rs.succeeded()) {
log.info("发送成功.");
}
if (rs.failed()) {
log.info("发送失败.");
}
});
client.post(url).sendJson(qyWechatMessage)
.onSuccess(response -> log.info("Received response with status code" + response.statusCode()))
.onFailure(err -> log.error("Something went wrong " + err.getMessage()));
}
}

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.notify.EventListener;
/**
* author:
* date: 2023-05-08 15:09
* description:
**/
public class MessageEventListener implements EventListener{
public class MessageEventListener implements EventListener {
@Override
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.QyWechatMessage;
import io.vertx.core.Vertx;
import cc.iotkit.message.notify.EventListener;
import io.vertx.ext.web.client.WebClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@ -20,19 +21,14 @@ public class QyWechatEventListener implements EventListener {
@Override
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());
QyWechatMessage qyWechatMessage = QyWechatMessage.builder()
.msgtype("text")
.text(QyWechatMessage.MessageContent.builder().content(message.getContent()).build())
.build();
client.post(url).sendJson(qyWechatMessage, rs -> {
if (rs.succeeded()) {
log.info("发送成功.");
}
if (rs.failed()) {
log.info("发送失败.");
}
});
client.post(url).sendJson(qyWechatMessage)
.onSuccess(response -> log.info("Received response with status code" + response.statusCode()))
.onFailure(err -> log.error("Something went wrong " + err.getMessage()));
}
}

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;
import lombok.Data;
import java.io.Serializable;
/**
@ -7,5 +9,6 @@ import java.io.Serializable;
* date: 2023-05-08 15:58
* description:
**/
@Data
public class EmailMessage implements Serializable {
}

View File

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