diff --git a/campus-common/src/main/java/com/oddfar/campus/common/enums/NoticeEnum.java b/campus-common/src/main/java/com/oddfar/campus/common/enums/NoticeEnum.java new file mode 100644 index 00000000..963cbc38 --- /dev/null +++ b/campus-common/src/main/java/com/oddfar/campus/common/enums/NoticeEnum.java @@ -0,0 +1,19 @@ +package com.oddfar.campus.common.enums; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +public enum NoticeEnum { + PUSH_PLUS, WEB_HOOk + ; + + public static NoticeEnum getNoticeType(int type) { + for (NoticeEnum value : NoticeEnum.values()) { + if (value.ordinal() == type) { + return value; + } + } + return WEB_HOOk; + } +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/controller/TestController.java b/campus-modular/src/main/java/com/oddfar/campus/business/controller/TestController.java index 25090c55..04958d2c 100644 --- a/campus-modular/src/main/java/com/oddfar/campus/business/controller/TestController.java +++ b/campus-modular/src/main/java/com/oddfar/campus/business/controller/TestController.java @@ -1,6 +1,9 @@ package com.oddfar.campus.business.controller; +import com.oddfar.campus.business.entity.ILog; +import com.oddfar.campus.business.entity.IUser; import com.oddfar.campus.business.mapper.IUserMapper; +import com.oddfar.campus.business.notice.NoticeHelper; import com.oddfar.campus.business.service.IMTService; import com.oddfar.campus.business.service.IShopService; import com.oddfar.campus.common.annotation.Anonymous; @@ -8,6 +11,7 @@ import com.oddfar.campus.common.annotation.Log; import com.oddfar.campus.common.domain.R; import com.oddfar.campus.common.enums.ResBizTypeEnum; +import com.oddfar.campus.framework.manager.AsyncManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.GetMapping; @@ -37,6 +41,25 @@ public R test1() { return R.ok(); } + /** + * 需要接口权限 + */ + @PreAuthorize("@ss.resourceAuth()") + @GetMapping(value = "/sendNotice", name = "发送通知的测试") + public R sendNotice() { + IUser iUser = new IUser(); + iUser.setToken("11111"); + iUser.setPushPlusToken("222"); + iUser.setMobile(131313131L); + iUser.setRemark("zs"); + ILog iLog = new ILog(); + iLog.setStatus(0); + iLog.setMobile(131313131L); + iLog.setLogContent("test ~~~~"); + NoticeHelper.sendNotice(iUser, iLog); + return R.ok(); + } + /** * 需要 'campus:test' 权限字符串 */ diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/AbstractNoticeHandler.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/AbstractNoticeHandler.java new file mode 100644 index 00000000..9ee7ad80 --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/AbstractNoticeHandler.java @@ -0,0 +1,19 @@ +package com.oddfar.campus.business.notice; + +import com.oddfar.campus.common.enums.NoticeEnum; +import org.springframework.beans.factory.InitializingBean; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +public abstract class AbstractNoticeHandler implements NoticeHandler, InitializingBean { + + + public abstract NoticeEnum getBizType(); + + @Override + public void afterPropertiesSet() throws Exception { + NoticeFactory.addService(getBizType(), this); + } +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeConfig.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeConfig.java new file mode 100644 index 00000000..3ef6b6e1 --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeConfig.java @@ -0,0 +1,32 @@ +package com.oddfar.campus.business.notice; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +@ConfigurationProperties(prefix = "notice.config") +@Component +@Data +public class NoticeConfig { + + /** + * 发送类型 + */ + private Integer noticeType = 1; + + /** + * api url + */ + private String apiUrl = ""; + + /** + * 请求体 + * + */ + private String requestBody; + +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeFactory.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeFactory.java new file mode 100644 index 00000000..9e2113ef --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeFactory.java @@ -0,0 +1,32 @@ +package com.oddfar.campus.business.notice; + +import com.oddfar.campus.common.enums.NoticeEnum; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import javax.xml.ws.Service; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +public class NoticeFactory { + + private static final Map SERVICE_MAP = Collections.synchronizedMap(new HashMap<>()); + + public static void addService(NoticeEnum noticeEnum, NoticeHandler service) { + if (SERVICE_MAP.containsKey(noticeEnum)) { + return; + } + SERVICE_MAP.put(noticeEnum, service); + } + + public static NoticeHandler getService(NoticeEnum noticeEnum) { + NoticeHandler handler = SERVICE_MAP.get(noticeEnum); + if (handler == null) { + throw new RuntimeException("服务未找到"); + } + return handler; + } + +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHandler.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHandler.java new file mode 100644 index 00000000..00aef967 --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHandler.java @@ -0,0 +1,22 @@ +package com.oddfar.campus.business.notice; + +import com.oddfar.campus.business.entity.ILog; +import com.oddfar.campus.business.entity.IUser; +import java.util.TimerTask; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +public interface NoticeHandler { + + /** + * 发送通知 + * @param token + * @param title + * @param content + * @param template + */ + TimerTask sendNotice(String token, String title, String content, String template); + +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHelper.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHelper.java new file mode 100644 index 00000000..772dbee9 --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/NoticeHelper.java @@ -0,0 +1,33 @@ +package com.oddfar.campus.business.notice; + +import com.oddfar.campus.business.entity.ILog; +import com.oddfar.campus.business.entity.IUser; +import com.oddfar.campus.common.enums.NoticeEnum; +import com.oddfar.campus.common.utils.SpringUtils; +import com.oddfar.campus.common.utils.StringUtils; +import com.oddfar.campus.framework.manager.AsyncManager; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +public class NoticeHelper { + + public static void sendNotice(IUser iUser, ILog operLog) { + String token = iUser.getPushPlusToken(); + String title, content; + if (operLog.getStatus() == 0) { + //预约成功 + title = iUser.getRemark() + "-i茅台执行成功"; + content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent(); + } else { + //预约失败 + title = iUser.getRemark() + "-i茅台执行失败"; + content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent(); + } + AsyncManager.me().execute(NoticeFactory.getService(NoticeEnum.getNoticeType(SpringUtils.getBean(NoticeConfig.class) + .getNoticeType())) + .sendNotice(token, title, content, "txt")); + } + +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/api/PushPlusApi.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/PushPlusApiHandlerImpl.java similarity index 53% rename from campus-modular/src/main/java/com/oddfar/campus/business/api/PushPlusApi.java rename to campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/PushPlusApiHandlerImpl.java index 479e438b..04de4bf8 100644 --- a/campus-modular/src/main/java/com/oddfar/campus/business/api/PushPlusApi.java +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/PushPlusApiHandlerImpl.java @@ -1,42 +1,25 @@ -package com.oddfar.campus.business.api; +package com.oddfar.campus.business.notice.impl; import cn.hutool.http.HttpUtil; import com.oddfar.campus.business.entity.ILog; import com.oddfar.campus.business.entity.IUser; +import com.oddfar.campus.business.notice.AbstractNoticeHandler; +import com.oddfar.campus.common.enums.NoticeEnum; import com.oddfar.campus.common.utils.StringUtils; import com.oddfar.campus.framework.manager.AsyncManager; import java.util.HashMap; import java.util.Map; import java.util.TimerTask; +import org.springframework.stereotype.Component; /** * @author zhiyuan */ -public class PushPlusApi { +@Component +public class PushPlusApiHandlerImpl extends AbstractNoticeHandler { - public static void sendNotice(IUser iUser, ILog operLog) { - String token = iUser.getPushPlusToken(); - if (StringUtils.isEmpty(token)) { - return; - } - String title, content; - if (operLog.getStatus() == 0) { - //预约成功 - title = iUser.getRemark() + "-i茅台执行成功"; - content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent(); - AsyncManager.me().execute(sendNotice(token, title, content, "txt")); - } else { - //预约失败 - title = iUser.getRemark() + "-i茅台执行失败"; - content = iUser.getMobile() + System.lineSeparator() + operLog.getLogContent(); - AsyncManager.me().execute(sendNotice(token, title, content, "txt")); - } - - - } - /** * push推送 * @@ -45,7 +28,7 @@ public static void sendNotice(IUser iUser, ILog operLog) { * @param content 具体消息内容 * @param template 发送消息模板 */ - public static TimerTask sendNotice(String token, String title, String content, String template) { + public TimerTask sendNotice(String token, String title, String content, String template) { return new TimerTask() { @Override public void run() { @@ -62,4 +45,8 @@ public void run() { }; } + @Override + public NoticeEnum getBizType() { + return NoticeEnum.PUSH_PLUS; + } } diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/WebhookHandlerImpl.java b/campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/WebhookHandlerImpl.java new file mode 100644 index 00000000..20928d41 --- /dev/null +++ b/campus-modular/src/main/java/com/oddfar/campus/business/notice/impl/WebhookHandlerImpl.java @@ -0,0 +1,47 @@ +package com.oddfar.campus.business.notice.impl; + +import cn.hutool.http.HttpUtil; +import com.oddfar.campus.business.notice.AbstractNoticeHandler; +import com.oddfar.campus.business.notice.NoticeConfig; +import com.oddfar.campus.common.enums.NoticeEnum; +import com.oddfar.campus.common.utils.StringUtils; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.TimerTask; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @author gitsilence + * @date 2024-03-17 + */ +@Component +public class WebhookHandlerImpl extends AbstractNoticeHandler { + + @Autowired + private NoticeConfig noticeConfig; + + @Override + public NoticeEnum getBizType() { + return NoticeEnum.WEB_HOOk; + } + + @Override + public TimerTask sendNotice(String token, String title, String content, String template) { + return new TimerTask() { + @Override + public void run() { + String apiUrl = StringUtils.replace(noticeConfig.getApiUrl(), "{token}", token); + String requestBody = null; + try { + requestBody = StringUtils.replace(noticeConfig.getRequestBody(), "{title}", title) + .replace("{content}", URLEncoder.encode(content, "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + HttpUtil.post(apiUrl, requestBody); + } + }; + } +} diff --git a/campus-modular/src/main/java/com/oddfar/campus/business/service/IMTLogFactory.java b/campus-modular/src/main/java/com/oddfar/campus/business/service/IMTLogFactory.java index 3eb49b31..3e352942 100644 --- a/campus-modular/src/main/java/com/oddfar/campus/business/service/IMTLogFactory.java +++ b/campus-modular/src/main/java/com/oddfar/campus/business/service/IMTLogFactory.java @@ -1,6 +1,6 @@ package com.oddfar.campus.business.service; -import com.oddfar.campus.business.api.PushPlusApi; +import com.oddfar.campus.business.notice.NoticeHelper; import com.oddfar.campus.business.entity.ILog; import com.oddfar.campus.business.entity.IUser; import com.oddfar.campus.common.utils.SpringUtils; @@ -33,7 +33,7 @@ public static void reservation(IUser iUser, String logContent) { AsyncManager.me().execute(recordOper(operLog)); //推送 - PushPlusApi.sendNotice(iUser, operLog); + NoticeHelper.sendNotice(iUser, operLog); } /** diff --git a/campus-modular/src/main/resources/application.yml b/campus-modular/src/main/resources/application.yml index d60ac71c..5ed1d382 100644 --- a/campus-modular/src/main/resources/application.yml +++ b/campus-modular/src/main/resources/application.yml @@ -95,6 +95,11 @@ mybatis-plus: logic-delete-value: 1 logic-not-delete-value: 0 +notice: + config: + notice-type: 1 + api-url: http://192.168.0.106:8080/bot/msg/send?access_token={token}&group_id=xxx + request-body: '{"message":"{content}"}' # Swagger配置