Jelajahi Sumber

Merge branch 'master' of http://111.47.6.224:3000/udream-cxs/lift-server.git

Wei Ruifeng 4 tahun lalu
induk
melakukan
9fb755467f

+ 2 - 2
lift-ud-service/src/main/java/cn/com/ty/lift/ud/chat/controller/query/ChatMsgQuery.java

@@ -41,13 +41,13 @@ public final class ChatMsgQuery extends BasePageQuery {
      * 来自
      */
     @ApiModelProperty("来自")
-    private Integer fromUser;
+    private Long fromUser;
 
     /**
      * 到达
      */
     @ApiModelProperty("到达")
-    private Integer toUser;
+    private Long toUser;
 
     /**
      * 信息 聊天信息

+ 11 - 4
lift-ud-service/src/main/java/cn/com/ty/lift/ud/chat/service/impl/ChatSessionServiceImpl.java

@@ -1,8 +1,10 @@
 package cn.com.ty.lift.ud.chat.service.impl;
 
 import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import cn.com.ty.lift.common.model.PushMessage;
 import cn.com.ty.lift.common.model.PushUserInfo;
@@ -128,6 +130,8 @@ public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatS
     public RestResponse findDBListByUserId(ChatSessionQuery query) {
         IPage page = query.page();
         List<ChatSessionEntity> list = sessionMapper.findDBListByUserId(query.getUserId());
+        Set<String> keys = redis.keys("ROOM:");
+
         page.setRecords(list);
         return RestResponse.success(page);
     }
@@ -148,8 +152,9 @@ public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatS
 
         if (StringUtils.isBlank(sessionId)) {
             ChatSessionEntity record = new ChatSessionEntity();
+            //添加自己到房间
             sessionId = UUIDUtil.getUuidByTime9();
-            record.setUserId(request.getUserId());
+            record.setUserId(request.getCreateUserId());
             record.setCreateUserId(request.getCreateUserId());
             record.setSessionid(sessionId);
             record.setDataTable(3);
@@ -157,9 +162,9 @@ public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatS
             record.setStatuz(1);
             record.setCreateTime(LocalDateTime.now());
             sessionMapper.insertSelective(record);
-            
+            //添加对方用户到房间
             record = new ChatSessionEntity();
-            record.setUserId(request.getCreateUserId());
+            record.setUserId(request.getUserId());
             record.setSessionid(sessionId);
             record.setDataTable(3);
             record.setEntrance(1);
@@ -173,7 +178,8 @@ public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatS
             RoomInfo roomInfo = new RoomInfo();
             roomInfo.setSessionid(record.getSessionid());
             roomInfo.setDataTable(record.getDataTable());
-            roomInfo.setLastTime(record.getCreateTime());
+            roomInfo.setLastTime(record.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
+
             UserInfo userInfo1 = new UserInfo();
             userInfo1.setUserId(request.getUserId());
             UserInfo userInfo2 = new UserInfo();
@@ -181,6 +187,7 @@ public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatS
             List<UserInfo> userList = new ArrayList<>();
             userList.add(userInfo1);
             userList.add(userInfo2);
+
             roomInfo.setUserLIst(userList);
             redis.setValue("ROOM:"+vo.getRoom(),roomInfo);
         } else {

+ 14 - 6
lift-ud-service/src/main/java/cn/com/ty/lift/ud/neety/process/ChatProcessor.java

@@ -41,16 +41,25 @@ public class ChatProcessor extends WebSoketSupport implements ProcessorInterface
         String message = SensitivewordFilter.replaceSensitiveWord(request.getMsg(), 1, "**");
         request.setMsg(message);
 
+        //redis只存当天的消息,RedisTask中synRedisChatToDB定时任务会在每天凌晨把消息同步到数据库
         //每次发消息,把消息累加到redis中,推送全量数据给客户端
-        List<IMMessage> chats = redis.get("ALLCHATS:" + sessionId, List.class) == null ?
-                new ArrayList() :
-                redis.get("ALLCHATS:" + sessionId, List.class);
+        boolean exists = redis.exists("ALLCHATS:" + sessionId);
+        List<IMMessage> chats = null;
+        if (exists){
+            chats = redis.get("ALLCHATS:" + sessionId, List.class);
+        } else {
+            chats  = new ArrayList() ;
+        }
         chats.add(request);
-
         redis.setValue("ALLCHATS:" + sessionId, chats);
-        //通过redis获取房间信息
 
+        //更新聊天室中的最后一条记录及时间,在app中获取聊天室排序有用
+        RoomInfo roomInfo = redis.get("ROOM:" + sessionId, RoomInfo.class);
+        roomInfo.setLastChat(request.getMsg());
+        roomInfo.setLastTime(request.getTime());
+        redis.setValue("ROOM:" + sessionId, roomInfo);
 
+        //通过redis获取房间信息
         //用户正常登录的时候链接就会调用login协议,login会加载聊天室与用户关系到数据
         //当用户已经登录了,附近的人聊天的时候,接收人是不知道自己已经有一个新的会话。
         //出诊问诊时,专家没有接单的情况下是不能进入聊天室的。
@@ -66,7 +75,6 @@ public class ChatProcessor extends WebSoketSupport implements ProcessorInterface
             //发给自己
             sendMessage(client, request);
         }else{
-            RoomInfo roomInfo = redis.get("ROOM:" + sessionId, RoomInfo.class);
             for (int i = 0; roomInfo.getUserLIst() != null
                     && !roomInfo.getUserLIst().isEmpty()
                     &&i < roomInfo.getUserLIst().size(); i++) {

+ 16 - 23
lift-ud-service/src/main/java/cn/com/ty/lift/ud/neety/process/InitRoomProcessor.java

@@ -44,48 +44,41 @@ public class InitRoomProcessor extends WebSoketSupport implements ProcessorInter
 
         String SESSION_ID = request.getSessionid();
         String USER_ID = request.getUserId();
-        /*
-        在出诊问诊,附近的人做业务操作的时候,会再redis中生成房间
-        //如果聊天室不存在,就生成聊天室,并把当前用户放入到聊天室中
-        boolean roomExists = redis.exists("ROOM:" + SESSION_ID);
-        if(!roomExists){
-            List list1 = new ArrayList<>();
-            Map newUser = new HashMap();
-            newUser.put("userId", USER_ID);
-            list1.add(newUser);
-            redis.setValue("ROOM:" + SESSION_ID, list1);
-        }*/
-        //如果聊天室存在,并且当前用户没有在聊天室中,把用户添加到聊天室中
+        //聊天室不存在返回消息
         boolean exists = redis.exists("ROOM:" + SESSION_ID);
         if (!exists){
             IMMessage im = new IMMessage();
             im.setCmd(IMP.CHAT.getName());
             im.setMsg("房间不存在");
             sendMessage(client, im);
-            logout(client);
+            return;
         }
-        RoomInfo roomInfo = redis.get("ROOM:" + SESSION_ID, RoomInfo.class);
 
-        //list1 防止用户重复添加到房间
-        Set<String> list1 = new HashSet<>();
+        //用户每次进来更新自己的信息
+        //如果自己的信息不存在就把用户添加到聊天室中
+        //如果聊天室存在,并且当前用户没有在聊天室中,把用户添加到聊天室中------start
+        RoomInfo roomInfo = redis.get("ROOM:" + SESSION_ID, RoomInfo.class);
+        boolean userExists = false;
         for (int i = 0; roomInfo.getUserLIst() != null
                 && !roomInfo.getUserLIst().isEmpty()
                 && i < roomInfo.getUserLIst().size(); i++) {
-            String userId = roomInfo.getUserLIst().get(i).getUserId();
-            list1.add(userId);
+            if (currentUserId.equals(roomInfo.getUserLIst().get(i).getUserId())){
+                userExists = true;
+                roomInfo.getUserLIst().get(i).setAvatarUrl(currentUserHead);
+                roomInfo.getUserLIst().get(i).setUserName(currentUserName);
+            }
         }
-
-        if (!list1.contains(currentUserId)) {
+        if (!userExists) {
             UserInfo userInfo = new UserInfo();
             userInfo.setUserId(currentUserId);
             userInfo.setUserName(currentUserName);
             userInfo.setAvatarUrl(currentUserHead);
             roomInfo.getUserLIst().add(userInfo);
-            redis.setValue("ROOM:" + SESSION_ID, roomInfo);
         }
+        redis.setValue("ROOM:" + SESSION_ID, roomInfo);
+        //如果聊天室存在,并且当前用户没有在聊天室中,把用户添加到聊天室中------end
 
-
-        //用户每日次进来把聊天记录通过后台全部推送过去
+        //用户每次进来把当天聊天记录通过后台全部推送过去
         List charts = redis.get("ALLCHATS:" + SESSION_ID, List.class);
         IMMessage im = new IMMessage();
         im.setCmd(IMP.INITROOM.getName());

+ 0 - 6
lift-ud-service/src/main/java/cn/com/ty/lift/ud/neety/process/LoginProcessor.java

@@ -47,14 +47,8 @@ public class LoginProcessor extends WebSoketSupport implements ProcessorInterfac
         client.attr(HEAD_PIC).getAndSet(AVATAR_URL);
         client.attr(IP_ADDR).getAndSet(getAddress(client));
 
-        //用户第一次进来,或者是断线重连,只有基本用户信息,session_id没有
-//        String SESSION_ID = request.getSessionid();
-//        client.attr(ROOM).getAndSet(SESSION_ID);
-
-        //----------------------------xson start----------------------
         //连接成功之后把链接与用户相关联,方便以后推送消息。
         userChannel.put(USER_ID, client);
-        //----------------------------xson end----------------------
 
 
 //        // room为总得大房间,map中的key为小房间的房间号,值为房间内对应每个用户的对象

+ 2 - 1
lift-ud-service/src/main/java/cn/com/ty/lift/ud/neety/task/RoomInfo.java

@@ -16,6 +16,7 @@ public class RoomInfo {
     private Integer caseType;
     private Integer acceptStatus;
     private String expression;
-    private LocalDateTime lastTime;
+    private String lastChat;
+    private long lastTime;
     private List<UserInfo> userLIst;
 }