⚡ 通知推送部分提交
This commit is contained in:
@@ -39,6 +39,10 @@
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.hccake.ballcat.admin.constants;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* 通知接收方式
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum NotifyChannel {
|
||||
|
||||
// 站内
|
||||
STATION(1),
|
||||
// 短信
|
||||
SMS(2),
|
||||
// 邮件
|
||||
MAIL(3);
|
||||
|
||||
private final int value;
|
||||
|
||||
}
|
||||
@@ -1,28 +1,46 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.listener;
|
||||
|
||||
import com.hccake.ballcat.admin.modules.notify.event.AnnouncementPublishEvent;
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.notify.push.NotifyPushRunner;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 公告事件监听器
|
||||
*
|
||||
* @author Hccake 2020/12/17
|
||||
* @version 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AnnouncementEventListener {
|
||||
|
||||
private final NotifyPushRunner notifyPusherRunner;
|
||||
|
||||
/**
|
||||
* 公告发布事件处理
|
||||
* // TODO 同步处理,失败回滚 公告发布事件处理
|
||||
* @param event the AnnouncementPublishEvent
|
||||
*/
|
||||
@EventListener(AnnouncementPublishEvent.class)
|
||||
public void onAnnouncementPublishEvent(AnnouncementPublishEvent event) {
|
||||
// TODO 公告通知
|
||||
System.out.println(event.getAnnouncement());
|
||||
Announcement announcement = event.getAnnouncement();
|
||||
|
||||
// TODO 根据接收人条件筛选
|
||||
List<SysUser> userList = new ArrayList<>();
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setEmail("chengbohua@foxmail.com");
|
||||
userList.add(sysUser);
|
||||
|
||||
// 推送通知
|
||||
notifyPusherRunner.run(announcement, userList, announcement.getReceiveMode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.List;
|
||||
* @author hccake 2020-12-15 17:01:15
|
||||
*/
|
||||
@Data
|
||||
@TableName("notify_announcement")
|
||||
@TableName(value = "notify_announcement", autoResultMap = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel(value = "公告信息")
|
||||
public class Announcement extends Model<Announcement> {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.model.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
@@ -46,12 +48,14 @@ public class AnnouncementVO {
|
||||
* 对应接收人筛选方式的条件信息,多个用逗号分割。如角色标识,组织ID,用户类型,用户ID等
|
||||
*/
|
||||
@ApiModelProperty(value = "对应接收人筛选方式的条件信息。如角色标识,组织ID,用户类型,用户ID等")
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private List<Object> recipientFilterCondition;
|
||||
|
||||
/**
|
||||
* 接收方式
|
||||
*/
|
||||
@ApiModelProperty(value = "接收方式")
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private List<Integer> receiveMode;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.push;
|
||||
|
||||
import com.hccake.ballcat.admin.constants.NotifyChannel;
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
import com.hccake.ballcat.common.mail.sender.MailSender;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 通知邮件发布
|
||||
*
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MailNotifyPusher implements NotifyPusher {
|
||||
|
||||
private final MailSender mailSender;
|
||||
|
||||
/**
|
||||
* 当前发布者的推送方式
|
||||
* @return 推送方式
|
||||
*/
|
||||
@Override
|
||||
public Integer notifyChannel() {
|
||||
return NotifyChannel.MAIL.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(Announcement announcement, List<SysUser> userList) {
|
||||
List<String> emails = userList.stream().map(SysUser::getEmail).collect(Collectors.toList());
|
||||
mailSender.sendHtmlMail(announcement.getTitle(), announcement.getContent(), emails);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.push;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 通知消息推送执行器
|
||||
*
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class NotifyPushRunner {
|
||||
|
||||
private final Map<Integer, NotifyPusher> notifyPusherMap = new LinkedHashMap<>();
|
||||
|
||||
public NotifyPushRunner(List<NotifyPusher> notifyPusherList) {
|
||||
if (CollectionUtil.isNotEmpty(notifyPusherList)) {
|
||||
for (NotifyPusher notifyPusher : notifyPusherList) {
|
||||
this.addNotifyPusher(notifyPusher);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加通知推送者
|
||||
* @param notifyPusher 通知推送者
|
||||
*/
|
||||
public void addNotifyPusher(NotifyPusher notifyPusher) {
|
||||
this.notifyPusherMap.put(notifyPusher.notifyChannel(), notifyPusher);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行通知推送
|
||||
* @param announcement 公告信息
|
||||
* @param userList 用户列表
|
||||
* @param receiveModes 接收模式(推送渠道)
|
||||
*/
|
||||
public void run(Announcement announcement, List<SysUser> userList, List<Integer> receiveModes) {
|
||||
for (Integer notifyChannel : receiveModes) {
|
||||
try {
|
||||
NotifyPusher notifyPusher = notifyPusherMap.get(notifyChannel);
|
||||
|
||||
if (notifyPusher == null) {
|
||||
log.error("Unknown notify channel:[{}],announcement id:[{}]", notifyChannel, announcement.getId());
|
||||
}
|
||||
else {
|
||||
notifyPusher.push(announcement, userList);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("push notify error in channel:[{}],announcement id:[{}]", notifyChannel,
|
||||
announcement.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.push;
|
||||
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 通知发布者
|
||||
*
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface NotifyPusher {
|
||||
|
||||
/**
|
||||
* 当前发布者对应的推送渠道
|
||||
* @return 推送方式对应的标识
|
||||
*/
|
||||
Integer notifyChannel();
|
||||
|
||||
/**
|
||||
* 推送通知
|
||||
* @param announcement 公告
|
||||
* @param userList 用户列表
|
||||
*/
|
||||
void push(Announcement announcement, List<SysUser> userList);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.push;
|
||||
|
||||
import com.hccake.ballcat.admin.constants.NotifyChannel;
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 短信通知发布
|
||||
*
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
@Component
|
||||
public class SmsNotifyPusher implements NotifyPusher {
|
||||
|
||||
/**
|
||||
* 当前发布者对应的接收方式
|
||||
* @return 推送方式
|
||||
*/
|
||||
@Override
|
||||
public Integer notifyChannel() {
|
||||
return NotifyChannel.SMS.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(Announcement announcement, List<SysUser> userList) {
|
||||
List<String> phoneList = userList.stream().map(SysUser::getPhone).collect(Collectors.toList());
|
||||
System.out.println("短信推送");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.hccake.ballcat.admin.modules.notify.push;
|
||||
|
||||
import com.hccake.ballcat.admin.constants.NotifyChannel;
|
||||
import com.hccake.ballcat.admin.modules.notify.model.entity.Announcement;
|
||||
import com.hccake.ballcat.admin.modules.sys.model.entity.SysUser;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 消息通知站内推送
|
||||
*
|
||||
* @author Hccake 2020/12/21
|
||||
* @version 1.0
|
||||
*/
|
||||
@Component
|
||||
public class StationNotifyPusher implements NotifyPusher {
|
||||
|
||||
/**
|
||||
* 当前发布者对应的接收方式
|
||||
* @return 推送方式
|
||||
*/
|
||||
@Override
|
||||
public Integer notifyChannel() {
|
||||
return NotifyChannel.STATION.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(Announcement announcement, List<SysUser> userList) {
|
||||
System.out.println("站内信推送");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user