feat: 通知

This commit is contained in:
裴浩宇 2024-05-06 11:00:23 +08:00
parent e595c0e0e6
commit 82536d2ff1
7 changed files with 12 additions and 105 deletions

View File

@ -57,7 +57,7 @@ spring:
# 邮箱配置 # 邮箱配置
mail: mail:
host: smtp.qq.com host: smtp.qq.com
userName: 617594538@qq.com username: 617594538@qq.com
#邮箱授权码 #邮箱授权码
password: gormkqlrtleobecb password: gormkqlrtleobecb
properties: properties:
@ -106,8 +106,3 @@ xss:
excludes: /system/notice/* excludes: /system/notice/*
# 匹配链接 # 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/* urlPatterns: /system/*,/monitor/*,/tool/*
feishu:
# 飞书机器人的webhook地址
webhook:
url: https://open.feishu.cn/open-apis/bot/v2/hook/ae03114d-aa0d-4348-b12e-9bd7e2911399

View File

@ -1,10 +1,5 @@
import com.pnkx.framework.notify.NotifyContext; import com.pnkx.common.notify.FeishuISysNotify;
import com.pnkx.framework.notify.impl.EmailNotifyStrategy;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import javax.annotation.Resource;
/** /**
* NotifyTest * NotifyTest
@ -17,8 +12,7 @@ import javax.annotation.Resource;
public class NotifyTest { public class NotifyTest {
@Test @Test
public void notifyTest() throws Exception { public void notifyTest() {
NotifyContext notifyContext = new NotifyContext(new EmailNotifyStrategy()); FeishuISysNotify.sendNotification("测试通知");
notifyContext.send("617594538@qq.com", "测试通知");
} }
} }

View File

@ -1,11 +1,7 @@
package com.pnkx.framework.notify.impl; package com.pnkx.common.notify;
import com.pnkx.common.core.controller.BaseController;
import com.pnkx.framework.notify.NotifyStrategy;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -14,23 +10,21 @@ import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
/** /**
* FeishuNotifyStrategy * FeishuISysNotify
* *
* @author 裴浩宇 * @author 裴浩宇
* @version 1.0 * @version 1.0
* @date 2024/4/30 17:04 * @date 2024/4/30 17:04
* @description 飞书通知 * @description 飞书通知
*/ */
@Component public class FeishuISysNotify {
public class FeishuNotifyStrategy implements NotifyStrategy {
protected final Logger logger = LoggerFactory.getLogger(FeishuNotifyStrategy.class); protected static final Logger logger = LoggerFactory.getLogger(FeishuISysNotify.class);
@Value("${feishu.webhook.url}") // 飞书机器人的webhook地址
private String webhookUrl; private static final String webhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/ae03114d-aa0d-4348-b12e-9bd7e2911399";
@Override public static void sendNotification(String message) {
public void sendNotification(String recipient, String message) {
try { try {
URL url = new URL(webhookUrl); URL url = new URL(webhookUrl);
HttpURLConnection connection = getHttpURLConnection(message, url); HttpURLConnection connection = getHttpURLConnection(message, url);

View File

@ -141,9 +141,8 @@ public class LogAspect {
* 获取请求的参数放到log中 * 获取请求的参数放到log中
* *
* @param operLog 操作日志 * @param operLog 操作日志
* @throws Exception 异常
*/ */
private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception { private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) {
String requestMethod = operLog.getRequestMethod(); String requestMethod = operLog.getRequestMethod();
if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) { if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
String params = argsArrayToString(joinPoint.getArgs()); String params = argsArrayToString(joinPoint.getArgs());

View File

@ -1,21 +0,0 @@
package com.pnkx.framework.notify;
/**
* NotifyContext
*
* @author 裴浩宇
* @version 1.0
* @date 2024/4/30 17:09
* @description 通知策略上下文
*/
public class NotifyContext {
private final NotifyStrategy strategy;
public NotifyContext(NotifyStrategy strategy) {
this.strategy = strategy;
}
public void send(String recipient, String message) throws Exception {
strategy.sendNotification(recipient, message);
}
}

View File

@ -1,18 +0,0 @@
package com.pnkx.framework.notify;
/**
* NotifyStrategy
*
* @author 裴浩宇
* @version 1.0
* @date 2024/4/30 16:50
* @description 通知接口
*/
public interface NotifyStrategy {
/**
* 发送通知
* @param recipient 接收者
* @param message 消息内容
*/
void sendNotification(String recipient, String message) throws Exception;
}

View File

@ -1,36 +0,0 @@
package com.pnkx.framework.notify.impl;
import com.pnkx.common.exception.ServiceException;
import com.pnkx.common.utils.StringUtils;
import com.pnkx.common.utils.email.EmailUtils;
import com.pnkx.framework.notify.NotifyStrategy;
import com.pnkx.system.domain.SysEmail;
import com.pnkx.system.service.ISysEmailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* EmailNotifyStrategy
*
* @author 裴浩宇
* @version 1.0
* @date 2024/4/30 16:56
* @description 邮箱方式
*/
@Component
public class EmailNotifyStrategy implements NotifyStrategy {
@Override
public void sendNotification(String recipient, String message) throws Exception {
}
}