Browse Source

匿名聊天代码更新

fangzhen 4 months ago
parent
commit
704ea097d6

+ 1 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/webSocket/MessageType.java

@@ -5,7 +5,7 @@ package com.ruoyi.framework.webSocket;
  */
 public enum MessageType {
 
-    SYS("sys", "系统消息"), CHAT("chat", "聊天消息");
+    SYS("sys", "系统消息"), CHAT("chat", "聊天消息"),ANONYMOUS("anonymous", "返图聊天消息");
 
     private String type;
     private String value;

+ 5 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/webSocket/WebSocketServer.java

@@ -243,13 +243,17 @@ public class WebSocketServer {
         int messageType = jsonObject.getInteger("messageType");     //消息类型
 //        JSONObject sendText = jsonObject.getJSONObject("sendText");
         CommunityChatMsg chatMsg = null;
-        if (type.equals(MessageType.CHAT.getType())) {
+        if (type.equals(MessageType.CHAT.getType()) || type.equals(MessageType.ANONYMOUS.getType())) {
             LOGGER.debug("聊天消息推送");
 //            sendToUser(String.valueOf(receiveUserId), JSONObject.toJSONString(jsonObject));
 
             chatMsg = new CommunityChatMsg();
             chatMsg.setSenderId(senderId);
             chatMsg.setReceiverId(receiverId);
+            if (type.equals(MessageType.ANONYMOUS.getType())) {
+                chatMsg.setSenderAnonName(jsonObject.getString("senderAnonName"));
+                chatMsg.setReceiverAnonName(jsonObject.getString("receiverAnonName"));
+            }
             chatMsg.setCreateBy(senderId);
             chatMsg.setSenderIsRead(true);
             chatMsg.setReceiverIsRead(false);

+ 42 - 8
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityChatMsgController.java

@@ -6,9 +6,11 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.user.ProjectException;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.generator.domain.Community.CommunityUserBlock;
+import com.ruoyi.generator.domain.ReqEntity.UpdateAnonName;
 import com.ruoyi.generator.mapper.community.CommunityUserBlockMapper;
 import com.ruoyi.system.domain.CommunityChatMsg;
 import com.ruoyi.system.domain.vo.CommunityChatMsgVo;
@@ -18,10 +20,7 @@ import com.ruoyi.system.mapper.SysUserMapper;
 import com.ruoyi.system.service.ICommunityChatMsgService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
@@ -50,6 +49,7 @@ public class CommunityChatMsgController extends BaseController {
 
     @Resource
     private CommunityChatMsgMapper communitychatMsgMapper;
+
     /**
      * 获取当前登录用户聊天记录
      */
@@ -104,9 +104,9 @@ public class CommunityChatMsgController extends BaseController {
                         chatMsgVo.setReceiverIsRead(true);
                     }
                 }
-                chatMsgVo.setSenderNickName(senderUser.getNickName());
+                chatMsgVo.setSenderNickName("chat".equals(chatMsg.getType()) ? senderUser.getNickName() : chatMsg.getSenderAnonName());
                 chatMsgVo.setSenderAvatar(senderUser.getAvatar());
-                chatMsgVo.setReceiverNickName(receiveUser.getNickName());
+                chatMsgVo.setReceiverNickName("chat".equals(chatMsg.getType()) ? receiveUser.getNickName() : chatMsg.getReceiverAnonName());
                 chatMsgVo.setReceiverAvatar(receiveUser.getAvatar());
                 chatMsgVos.add(chatMsgVo);
             }
@@ -120,7 +120,7 @@ public class CommunityChatMsgController extends BaseController {
     }
 
     /**
-     * 获取当前登录用户聊天记录
+     * 获取当前登录用户聊天列表
      */
     @ApiOperation("获取当前登录用户聊天列表")
     @GetMapping("/list")
@@ -178,7 +178,7 @@ public class CommunityChatMsgController extends BaseController {
             communityChatMsgService.update(
                     null,
                     new UpdateWrapper<CommunityChatMsg>()
-                            .eq("sender_id", otherUserId )
+                            .eq("sender_id", otherUserId)
                             .eq("receiver_id", userId)
                             .set("receiver_is_delete", true)
                             .and((wrapper) -> {
@@ -191,4 +191,38 @@ public class CommunityChatMsgController extends BaseController {
         }
         return AjaxResult.success("清空聊天记录成功");
     }
+
+    /**
+     * 更新对方聊天匿名名称
+     */
+    @ApiOperation("更新对方聊天匿名名称")
+    @PostMapping("/updateAnonName")
+    public AjaxResult updateAnonName(@RequestBody UpdateAnonName updateAnonName) {
+        Long userId = SecurityUtils.getUserId();
+        long otherUserId = updateAnonName.getOtherUserId();
+        String newAnonName = updateAnonName.getNewAnonName();
+        String type = updateAnonName.getType();
+        try {
+            if ("anonymous".equals(type)) {
+                List<CommunityChatMsg> chatMsgList = communitychatMsgMapper.getChatMsgRecord(userId, otherUserId, type);
+                for (CommunityChatMsg chatMsg : chatMsgList) {
+                    if (chatMsg.getSenderId().equals(otherUserId)) {
+                        chatMsg.setSenderAnonName(newAnonName);
+                    }
+
+                    if (chatMsg.getReceiverId().equals(otherUserId)) {
+                        chatMsg.setReceiverAnonName(newAnonName);
+                    }
+                    chatMsg.setUpdateBy(SecurityUtils.getUserId());
+                    chatMsg.setUpdateTime(DateUtils.parseDate(DateUtils.getDate()));
+                }
+                communityChatMsgService.updateBatchById(chatMsgList);
+            }
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+            throw new ProjectException();
+        }
+
+        return success("更新成功!");
+    }
 }

+ 14 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/ReqEntity/UpdateAnonName.java

@@ -0,0 +1,14 @@
+package com.ruoyi.generator.domain.ReqEntity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class UpdateAnonName {
+    private long otherUserId;
+    private String newAnonName;
+    private String type;
+}

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/domain/CommunityChatMsg.java

@@ -38,10 +38,18 @@ public class CommunityChatMsg implements Serializable {
     * 消息发送人id
     */
     private Long senderId;
+    /**
+     * 发送人匿名名称
+     */
+    private String senderAnonName;
     /**
     * 消息接收人id
     */
     private Long receiverId;
+    /**
+     * 接收人匿名名称
+     */
+    private String receiverAnonName;
     /**
      * 地址名称
      */

+ 5 - 3
ruoyi-system/src/main/resources/mapper/system/CommunityChatMsgMapper.xml

@@ -6,8 +6,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 	<select id="getChatListWithLatestMessage" resultType="com.ruoyi.system.domain.vo.SysUserVo">
         WITH LatestMessages AS (
             SELECT r.id                                                                  as id,
-                   u.nick_name                                                           as nickName,
-                   u.user_name                                                           as username,
+                   IF('anonymous' = #{type}, r.sender_anon_name, u.nick_name) AS nickName,
+                   IF('anonymous' = #{type}, r.sender_anon_name, u.user_name) AS username,
                    u.avatar                                                              as avatar,
                    (SELECT u2.nick_name
                     FROM sys_user u2
@@ -76,7 +76,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             id	                as id,
             content	            as content,
             sender_id	        as senderId,
+            sender_anon_name    as senderAnonName,
             receiver_id	        as receiverId,
+            receiver_anon_name  as receiverAnonName,
             sender_is_read	    as senderIsRead,
             receiver_is_read	as receiverIsRead,
             address	            as address,
@@ -104,7 +106,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                         sender_id = #{otherUserId} AND receiver_id = #{userId}
                         )
                 )
-          AND type = 'chat'
+          AND type = #{type}
           AND (
                 (
                             sender_id = #{userId} AND (