From 889189e72652f07d77e14c6bd469c4bf1c0ef73d Mon Sep 17 00:00:00 2001 From: yyb <1416014977@qq.com> Date: Thu, 24 Apr 2025 16:10:39 +0800 Subject: [PATCH] =?UTF-8?q?update=20websocket=20=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=8F=98=E4=B8=BA=E5=90=8Cip=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=A4=9A=E4=B8=AA=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?,=E5=8D=95=E7=94=A8=E6=88=B7=E5=8F=AA=E8=83=BD=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E4=B8=80=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/GlobalExceptionHandler.java | 4 ++- .../controller/ChatMessageController.java | 4 --- .../chat/controller/ChatRoomController.java | 13 ++++++-- .../com/m2pool/chat/entity/ChatMessage.java | 2 +- .../chat/entity/ChatMessageHistory.java | 1 + .../WebsocketChannelInterceptor.java | 31 ++++++++++++------- .../m2pool/chat/mapper/ChatMessageMapper.java | 2 +- .../m2pool/chat/service/ChatRoomService.java | 7 ++--- .../service/impl/ChatMessageServiceImpl.java | 8 +++-- .../service/impl/ChatRoomServiceImpl.java | 17 +++++----- .../com/m2pool/chat/vo/MessagePageVo.java | 7 ----- .../java/com/m2pool/chat/vo/RoomPageVo.java | 20 ++++++++++++ .../mapper/chat/ChatMessageMapper.xml | 5 ++- .../resources/mapper/chat/ChatRoomMapper.xml | 14 +++++++-- 14 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/RoomPageVo.java diff --git a/m2pool-common/common-security/src/main/java/com/m2pool/common/security/handler/GlobalExceptionHandler.java b/m2pool-common/common-security/src/main/java/com/m2pool/common/security/handler/GlobalExceptionHandler.java index 9856b42..afef4fe 100644 --- a/m2pool-common/common-security/src/main/java/com/m2pool/common/security/handler/GlobalExceptionHandler.java +++ b/m2pool-common/common-security/src/main/java/com/m2pool/common/security/handler/GlobalExceptionHandler.java @@ -78,6 +78,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(ServiceException.class) public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) { + log.error(e.getMessage(), e); Integer code = e.getCode(); return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); @@ -89,9 +90,10 @@ public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { + System.out.println("异常抛出时间"+System.currentTimeMillis()); String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); - return AjaxResult.error(e.getMessage()); + return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage()); } /** diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatMessageController.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatMessageController.java index 825538b..7898908 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatMessageController.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatMessageController.java @@ -54,8 +54,4 @@ public class ChatMessageController { public AjaxResult readMessage(@RequestBody MessagePageVo pageVo) { return chatMessageService.readMessage(pageVo.getRoomId()); } - - - - } \ No newline at end of file diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatRoomController.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatRoomController.java index aed2c65..dd17a39 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatRoomController.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/controller/ChatRoomController.java @@ -2,7 +2,7 @@ package com.m2pool.chat.controller; import com.m2pool.chat.service.ChatRoomService; import com.m2pool.chat.vo.CharRoomVo; -import com.m2pool.chat.vo.MessagePageVo; +import com.m2pool.chat.vo.RoomPageVo; import com.m2pool.common.core.web.Result.AjaxResult; import com.m2pool.common.core.web.page.TableDataInfo; import com.m2pool.common.security.annotation.RequiresLogin; @@ -11,6 +11,8 @@ import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.time.LocalDateTime; + @Api(tags = "聊天室相关接口") @RestController @RequestMapping("/rooms") @@ -23,8 +25,13 @@ public class ChatRoomController { @PostMapping("/find/room/list") @ApiOperation(value = "查询客服人员的聊天室列表") @RequiresLogin - public TableDataInfo findRoomList(@RequestBody MessagePageVo messagePageVo) { - return chatRoomService.findRoomList(messagePageVo.getSendDateTime()); + public TableDataInfo findRoomList(@RequestBody(required = false) RoomPageVo roomPageVo) { + // 如果请求体为空,初始化一个默认的 RoomPageVo 对象 + if (roomPageVo == null) { + roomPageVo = new RoomPageVo(); + roomPageVo.setSendDateTime(LocalDateTime.now()); + } + return chatRoomService.findRoomList(roomPageVo); } @GetMapping("/find/room/by/userid") diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessage.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessage.java index 9329bbd..d7db68a 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessage.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessage.java @@ -17,7 +17,7 @@ public class ChatMessage { @TableId(value = "id", type = IdType.AUTO) private Long id; private Integer type; - private Integer read; + private Integer isRead; private String sendEmail; private String content; private Long roomId; diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessageHistory.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessageHistory.java index 2cfc417..e13add0 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessageHistory.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/entity/ChatMessageHistory.java @@ -15,6 +15,7 @@ public class ChatMessageHistory { @TableId(value = "id", type = IdType.AUTO) private Long id; private Integer type; + private Integer isRead; private String sendEmail; private String content; private Long roomId; diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/interceptor/WebsocketChannelInterceptor.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/interceptor/WebsocketChannelInterceptor.java index f9c6d2a..34859d6 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/interceptor/WebsocketChannelInterceptor.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/interceptor/WebsocketChannelInterceptor.java @@ -23,7 +23,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import static com.m2pool.chat.constant.CustomHeader.*; -import static com.m2pool.chat.constant.UserType.CUSTOMER; +import static com.m2pool.chat.constant.UserType.LOGIN_USER; +import static com.m2pool.chat.constant.UserType.TOURIST; /** @@ -37,9 +38,9 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(WebsocketChannelInterceptor.class); /** - * 当前加入链接的ip + * 当前加入链接的ip ,key 为 ip ,VALUE 为用户邮箱 */ - private static final ConcurrentHashMap ipConnectionCountMap = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap ipConnectionCountMap = new ConcurrentHashMap<>(); /** @@ -78,7 +79,7 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor { //最大链接数限制 maxConnectionsLimit(); //根据客服端ip + 用户类型限制连接数 - ipLimit(accessor,type); + ipLimit(accessor,type,email); //链接请求头中用户信息存入stomp中 @@ -117,14 +118,23 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor { * ip 链接个数限制 * @param accessor */ - private void ipLimit(StompHeaderAccessor accessor,int type){ + private void ipLimit(StompHeaderAccessor accessor,int type,String email){ Map sessionAttributes = accessor.getSessionAttributes(); if (sessionAttributes != null) { String ipAddr = (String) sessionAttributes.get(IPADDR); - if(type != CUSTOMER && ipConnectionCountMap.get(ipAddr) != null){ + System.out.println(accessor.getSessionId()); + String hasConnectionEmail = ipConnectionCountMap.get(ipAddr); + //两种ip限制情况 + //本次链接为游客 且ip上已经有人链接直接拒绝本次链接 + if(type == TOURIST && hasConnectionEmail != null){ throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.IP_LIMIT_CONNECT)); } - ipConnectionCountMap.put(ipAddr,true); + //本次链接为登录用户,并且已经链接.直接拒绝本次链接(多用户登录) + if ( type == LOGIN_USER && email.equals(hasConnectionEmail)) { + throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.IP_LIMIT_CONNECT)); + } + ipConnectionCountMap.put(ipAddr,email); + }else{ throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.GET_PRINCIPAL_FAIL)); } @@ -137,7 +147,7 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor { * @return */ private String getConnectCustomHeaders(Object raw,String headerKey){ - String headerValue = ""; + String headerValue = ""; if (raw instanceof Map) { Object value = ((Map) raw).get(headerKey); if(value instanceof ArrayList){ @@ -157,15 +167,12 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor { Map sessionAttributes = accessor.getSessionAttributes(); if (sessionAttributes != null) { String ipAddr = (String) sessionAttributes.get(IPADDR); - if(ipConnectionCountMap.get(ipAddr) != null){ - ipConnectionCountMap.remove(ipAddr); - } + ipConnectionCountMap.remove(ipAddr); }else{ throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.GET_PRINCIPAL_FAIL)); } } - @Override public void postSend(Message message, MessageChannel channel, boolean sent) { LOGGER.info("------------WebsocketChannelInterceptor-postSend"); diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/mapper/ChatMessageMapper.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/mapper/ChatMessageMapper.java index 92d800e..f52cd2b 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/mapper/ChatMessageMapper.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/mapper/ChatMessageMapper.java @@ -29,6 +29,6 @@ public interface ChatMessageMapper extends BaseMapper { * @return Map,键为用户邮箱,值为未读消息条数 */ @MapKey("userEmail") - Map findUnReadNums(@Param("userEmails") List userEmails); + Map> findUnReadNums(@Param("userEmails") List userEmails); } \ No newline at end of file diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/ChatRoomService.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/ChatRoomService.java index 22f470b..29a8557 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/ChatRoomService.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/ChatRoomService.java @@ -3,19 +3,18 @@ package com.m2pool.chat.service; import com.baomidou.mybatisplus.extension.service.IService; import com.m2pool.chat.entity.ChatRoom; import com.m2pool.chat.vo.CharRoomVo; +import com.m2pool.chat.vo.RoomPageVo; import com.m2pool.common.core.web.Result.AjaxResult; import com.m2pool.common.core.web.page.TableDataInfo; -import java.time.LocalDateTime; - public interface ChatRoomService extends IService { /** * 查询客服的聊天室列表 - * @param sendDateTime 最后一个发送者发送时间 + * @param roomPageVo * @return */ - TableDataInfo findRoomList(LocalDateTime sendDateTime); + TableDataInfo findRoomList(RoomPageVo roomPageVo); /** * 根据当前用户邮箱查询聊天室 diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatMessageServiceImpl.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatMessageServiceImpl.java index 6f77b79..c09d3b1 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatMessageServiceImpl.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatMessageServiceImpl.java @@ -62,9 +62,11 @@ public class ChatMessageServiceImpl implements ChatMessageService { }else if (username.equals(chatRoom.getUserTwoEmail())) { updateUserEmail = chatRoom.getUserOneEmail(); } - int update = chatMessageMapper.update(ChatMessage.builder().read(1).build(), - new LambdaUpdateWrapper().eq(ChatMessage::getSendEmail, updateUserEmail).eq(ChatMessage::getRoomId, roomId)); - return update > 0 ? AjaxResult.success("已读") : AjaxResult.error("消息读取失败"); + int update = chatMessageMapper.update(ChatMessage.builder().isRead(1).build(), + new LambdaUpdateWrapper() + .eq(ChatMessage::getSendEmail, updateUserEmail) + .eq(ChatMessage::getRoomId, roomId)); + return update >= 0 ? AjaxResult.success("已读") : AjaxResult.error("消息读取失败"); } return AjaxResult.error("聊天室不存在"); } diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatRoomServiceImpl.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatRoomServiceImpl.java index 2782824..a8e418b 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatRoomServiceImpl.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/service/impl/ChatRoomServiceImpl.java @@ -1,6 +1,7 @@ package com.m2pool.chat.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.m2pool.chat.dto.ChatRoomDto; import com.m2pool.chat.entity.ChatRoom; @@ -8,6 +9,7 @@ import com.m2pool.chat.mapper.ChatMessageMapper; import com.m2pool.chat.mapper.ChatRoomMapper; import com.m2pool.chat.service.ChatRoomService; import com.m2pool.chat.vo.CharRoomVo; +import com.m2pool.chat.vo.RoomPageVo; import com.m2pool.common.core.constant.HttpStatus; import com.m2pool.common.core.utils.PageUtils; import com.m2pool.common.core.web.Result.AjaxResult; @@ -21,7 +23,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.Random; @@ -40,18 +41,20 @@ public class ChatRoomServiceImpl extends ServiceImpl i private RemoteUserService remoteUserService; @Override - public TableDataInfo findRoomList(LocalDateTime sendDateTime) { + public TableDataInfo findRoomList(RoomPageVo roomPageVo) { String userEmail = SecurityUtils.getUsername(); - PageUtils.startPage(); + PageHelper.startPage(1, 20); + System.out.println("-----时间"+roomPageVo.getSendDateTime()+"-----------用户"+userEmail + "-----当前时间"+ System.currentTimeMillis()); //1.查找当前客服对应的聊天室 - List roomList = chatRoomMapper.findRoomList(userEmail,sendDateTime); + List roomList = chatRoomMapper.findRoomList(userEmail,roomPageVo.getSendDateTime()); List userEmails = roomList.stream().map(ChatRoomDto::getUserEmail).collect(Collectors.toList()); //2.查询未读数量 if (!userEmails.isEmpty()){ - Map unReadNums = chatMessageMapper.findUnReadNums(userEmails); + Map> unReadNums = chatMessageMapper.findUnReadNums(userEmails); for (ChatRoomDto room : roomList) { - Integer i = unReadNums.get(room.getUserEmail()); - room.setUnReadNumber(i == null ? 0 : i); + Map stringIntegerMap = unReadNums.get(room.getUserEmail()); + Integer i = stringIntegerMap != null ? stringIntegerMap.get("unReadNumber") : 0; + room.setUnReadNumber(i); } } diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/MessagePageVo.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/MessagePageVo.java index 2ab8d41..b8a9408 100644 --- a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/MessagePageVo.java +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/MessagePageVo.java @@ -1,10 +1,7 @@ package com.m2pool.chat.vo; -import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; -import java.time.LocalDateTime; - /** * @ClassName MessagePageVo * @Description TODO @@ -17,10 +14,6 @@ public class MessagePageVo { private Long id; private Integer pageNum; private Long roomId; - - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") - private LocalDateTime sendDateTime; - public MessagePageVo() { this.id = 1L; this.pageNum = 20; diff --git a/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/RoomPageVo.java b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/RoomPageVo.java new file mode 100644 index 0000000..aa77929 --- /dev/null +++ b/m2pool-modules/m2pool-chat/src/main/java/com/m2pool/chat/vo/RoomPageVo.java @@ -0,0 +1,20 @@ +package com.m2pool.chat.vo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.time.LocalDateTime; + +/** + * @ClassName MessagePageVo + * @Description TODO + * @Author yyb + * @Date 2025/4/22 17:09 + */ +@Data +public class RoomPageVo { + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime sendDateTime; +} diff --git a/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatMessageMapper.xml b/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatMessageMapper.xml index 9e4b204..05e6b00 100644 --- a/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatMessageMapper.xml +++ b/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatMessageMapper.xml @@ -15,14 +15,13 @@ LIMIT #{pageNum} diff --git a/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatRoomMapper.xml b/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatRoomMapper.xml index d4b4721..dd92c2e 100644 --- a/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatRoomMapper.xml +++ b/m2pool-modules/m2pool-chat/src/main/resources/mapper/chat/ChatRoomMapper.xml @@ -11,9 +11,17 @@ last_customer_send_time as lastCustomerSendTime FROM chat_room - WHERE - user_two_email = #{userEmail} AND last_user_send_time #{sendDateTime} - AND del = FALSE + + user_two_email = #{userEmail} AND del = FALSE + + + AND last_user_send_time #{sendDateTime} + + + AND last_user_send_time NOW() + + + ORDER BY flag DESC, last_user_send_time DESC