feat: 通知公告临时提交
通知公告临时提交
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.youlai.boot.common.enums;
|
||||
|
||||
/**
|
||||
* 消息类型枚举
|
||||
* @author Theo
|
||||
* @since 2024-9-2 14:32:58
|
||||
*/
|
||||
public enum MessageTypeEnum {
|
||||
WEBSOCKET("webScoket", "websocket消息");
|
||||
|
||||
private String value;
|
||||
|
||||
private String label;
|
||||
|
||||
MessageTypeEnum(String value, String label) {
|
||||
this.value = value;
|
||||
this.label = label;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* 通知类型枚举
|
||||
* 1-系统通知 0-系统消息
|
||||
* 0-系统消息
|
||||
*
|
||||
* @since 2024-9-1 17:33:06
|
||||
* @author Theo
|
||||
@@ -15,8 +15,10 @@ import lombok.RequiredArgsConstructor;
|
||||
@RequiredArgsConstructor
|
||||
public enum NoticeTypeEnum implements IBaseEnum<Integer> {
|
||||
|
||||
SYSTEM_NOTICE(1, "系统通知"),
|
||||
SYSTEM_MESSAGE (0, "系统消息");
|
||||
/**
|
||||
* 通知类型
|
||||
*/
|
||||
SYSTEM_MESSAGE(0, "系统消息");
|
||||
|
||||
@Getter
|
||||
private Integer value;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.youlai.boot.common.enums;
|
||||
|
||||
import com.youlai.boot.common.base.IBaseEnum;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* 通知方式枚举
|
||||
* @author Theo
|
||||
* @since 2024-9-2 14:32:58
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum NoticeWayEnum implements IBaseEnum<Integer> {
|
||||
/**
|
||||
* 通知方式
|
||||
*/
|
||||
WEBSOCKET("webSocket", "发送websocket消息");
|
||||
|
||||
private String value;
|
||||
|
||||
private String label;
|
||||
|
||||
NoticeWayEnum(String value, String label) {
|
||||
this.value = value;
|
||||
this.label = label;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.youlai.boot.platform.websocket.controller;
|
||||
|
||||
import com.youlai.boot.common.enums.NoticeTypeEnum;
|
||||
import com.youlai.boot.system.model.dto.ChatMessage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -56,7 +57,7 @@ public class WebsocketController {
|
||||
|
||||
log.info("发送人:{}; 接收人:{}", sender, receiver);
|
||||
// 发送消息给指定用户,拼接后路径 /user/{receiver}/queue/greeting
|
||||
messagingTemplate.convertAndSendToUser(receiver, "/queue/greeting", new ChatMessage(sender, message));
|
||||
messagingTemplate.convertAndSendToUser(receiver, "/queue/greeting", new ChatMessage(sender, message, NoticeTypeEnum.SYSTEM_MESSAGE));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.youlai.boot.platform.websocket.service;
|
||||
|
||||
import com.youlai.boot.common.enums.MessageTypeEnum;
|
||||
import com.youlai.boot.common.enums.NoticeWayEnum;
|
||||
import com.youlai.boot.system.model.dto.MessageDTO;
|
||||
|
||||
/**
|
||||
@@ -18,7 +18,7 @@ public interface MessageService {
|
||||
* @param messageType 消息类型
|
||||
* @return 是否支持
|
||||
*/
|
||||
boolean check(MessageTypeEnum messageType);
|
||||
boolean check(NoticeWayEnum messageType);
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
package com.youlai.boot.platform.websocket.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.youlai.boot.common.enums.MessageTypeEnum;
|
||||
import com.youlai.boot.common.enums.NoticeWayEnum;
|
||||
import com.youlai.boot.common.enums.NoticeTypeEnum;
|
||||
import com.youlai.boot.platform.websocket.service.MessageService;
|
||||
import com.youlai.boot.system.event.UserConnectionEvent;
|
||||
import com.youlai.boot.system.model.dto.ChatMessage;
|
||||
import com.youlai.boot.system.model.dto.MessageDTO;
|
||||
import com.youlai.boot.system.model.entity.User;
|
||||
import com.youlai.boot.system.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.catalina.security.SecurityUtil;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
@@ -61,6 +58,7 @@ public class WebsocketServiceImpl implements MessageService {
|
||||
*/
|
||||
@Scheduled(fixedRate = 5000)
|
||||
public void sendOnlineUserCount() {
|
||||
log.info("Send online user count: {}", onlineUsers.size());
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUsers.size());
|
||||
}
|
||||
|
||||
@@ -68,12 +66,12 @@ public class WebsocketServiceImpl implements MessageService {
|
||||
/**
|
||||
* 策略模式检查
|
||||
*
|
||||
* @param messageType 消息类型
|
||||
* @return boolean
|
||||
* @param noticeWayEnum 通知方式
|
||||
* @return boolean 是否支持
|
||||
*/
|
||||
@Override
|
||||
public boolean check(MessageTypeEnum messageType) {
|
||||
return messageType.equals(MessageTypeEnum.WEBSOCKET);
|
||||
public boolean check(NoticeWayEnum noticeWayEnum) {
|
||||
return noticeWayEnum.equals(NoticeWayEnum.WEBSOCKET);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +89,7 @@ public class WebsocketServiceImpl implements MessageService {
|
||||
users = message.getReceiver().stream().filter(onlineUsers::contains).collect(Collectors.toList());
|
||||
}
|
||||
//获取当前用户
|
||||
ChatMessage chatMessage = new ChatMessage(message.getSender(), message.getContent());
|
||||
ChatMessage chatMessage = new ChatMessage(message.getSender(), message.getContent(), NoticeTypeEnum.SYSTEM_MESSAGE);
|
||||
users.forEach(receiver -> {
|
||||
messagingTemplate.convertAndSendToUser(receiver, "/queue/message", chatMessage);
|
||||
});
|
||||
|
||||
@@ -33,8 +33,8 @@ public interface NoticeConverter{
|
||||
Page<NoticeVO> toPageVo(Page<NoticeBO> noticePage);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(target = "noticeTypeLabel", expression = "java(com.youlai.boot.common.base.IBaseEnum.getLabelByValue(bo.getNoticeType(), com.youlai.boot.common.enums.NoticeTypeEnum.class))")
|
||||
@Mapping(target = "noticeTypeLabel", expression = "java(com.youlai.boot.common.base.IBaseEnum.getLabelByValue(bo.getNoticeType(), com.youlai.boot.common.enums.NoticeWayEnum.class))")
|
||||
})
|
||||
NoticeVO toPageVo(NoticeBO bo);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,12 @@ public class MessageHandler {
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* 如果后面有多种消息发送方式,可以设置MessageDTO中的noticeWay,调用不同的消息发送方式,实现消息多种发送方式
|
||||
* @param messageDTO 消息载体
|
||||
*/
|
||||
public void sendMessage(MessageDTO messageDTO) {
|
||||
messageServices.forEach(messageService -> {
|
||||
if (messageService.check(messageDTO.getMessageType())) {
|
||||
if (messageService.check(messageDTO.getNoticeWay())) {
|
||||
messageService.sendMessage(messageDTO);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.youlai.boot.system.model.dto;
|
||||
|
||||
import com.youlai.boot.common.enums.NoticeTypeEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* WebSocket 消息体
|
||||
* 系统消息体
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@@ -22,4 +23,9 @@ public class ChatMessage {
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private NoticeTypeEnum noticeType;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.youlai.boot.system.model.dto;
|
||||
|
||||
import com.youlai.boot.common.enums.MessageTypeEnum;
|
||||
import com.youlai.boot.common.enums.NoticeWayEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -25,6 +25,6 @@ public class MessageDTO {
|
||||
@Schema(description = "接收者")
|
||||
private List<String> receiver;
|
||||
|
||||
@Schema(description = "消息类型")
|
||||
private MessageTypeEnum messageType;
|
||||
@Schema(description = "通知方式")
|
||||
private NoticeWayEnum noticeWay;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
package com.youlai.boot.system.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户公告状态VO
|
||||
*
|
||||
* @auther Theo
|
||||
* @author Theo
|
||||
* @since 2024-08-28 16:56
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户公告状态VO")
|
||||
public class NoticeStatusVO {
|
||||
|
||||
@Schema(description = "公告ID")
|
||||
private Long noticeId;
|
||||
|
||||
@Schema(description = "是否已读")
|
||||
private Boolean read;
|
||||
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.boot.common.constant.SymbolConstant;
|
||||
import com.youlai.boot.common.enums.MessageTypeEnum;
|
||||
import com.youlai.boot.common.util.CommonUtil;
|
||||
import com.youlai.boot.common.enums.NoticeWayEnum;
|
||||
import com.youlai.boot.core.security.util.SecurityUtils;
|
||||
import com.youlai.boot.system.converter.NoticeConverter;
|
||||
import com.youlai.boot.system.handler.MessageHandler;
|
||||
@@ -180,7 +179,7 @@ public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> impleme
|
||||
usernameList = userList.stream().map(User::getUsername).collect(Collectors.toList());
|
||||
}
|
||||
MessageDTO message = new MessageDTO();
|
||||
message.setMessageType(MessageTypeEnum.WEBSOCKET);
|
||||
message.setNoticeWay(NoticeWayEnum.WEBSOCKET);
|
||||
message.setReceiver(usernameList);
|
||||
message.setContent(getNoticeContent(notice));
|
||||
message.setSender(SecurityUtils.getUsername());
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.youlai.boot.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.boot.common.result.Result;
|
||||
import com.youlai.boot.system.mapper.NoticeStatusMapper;
|
||||
import com.youlai.boot.system.model.entity.NoticeStatus;
|
||||
import com.youlai.boot.system.model.vo.NoticeStatusVO;
|
||||
import com.youlai.boot.system.service.NoticeService;
|
||||
import com.youlai.boot.system.service.NoticeStatusService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -21,9 +23,13 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class NoticeStatusServiceImpl extends ServiceImpl<NoticeStatusMapper, NoticeStatus> implements NoticeStatusService {
|
||||
|
||||
private final NoticeService noticeService;
|
||||
|
||||
@Override
|
||||
public List<NoticeStatusVO> listNotices(Integer count) {
|
||||
|
||||
LambdaQueryWrapper<NoticeStatus> queryWrapper = new LambdaQueryWrapper<>();
|
||||
//获取当前用户
|
||||
queryWrapper.eq(NoticeStatus::getUserId, 1L);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user