fangzhen 6 місяців тому
батько
коміт
d5d6610333

+ 42 - 8
ruoyi-framework/src/main/java/com/ruoyi/framework/webSocket/WebSocketServer.java

@@ -37,7 +37,7 @@ public class WebSocketServer {
     public static ICommunityChatMsgService chatMsgService;
 
     @Autowired
-    public void setChatMsgService(ICommunityChatMsgService chatMsgService){
+    public void setChatMsgService(ICommunityChatMsgService chatMsgService) {
         WebSocketServer.chatMsgService = chatMsgService;
     }
 
@@ -84,19 +84,53 @@ public class WebSocketServer {
     public void onMessage(String message) throws IOException {
         JSONObject jsonObject = JSONObject.parseObject(message);
         Long sendUserId = jsonObject.getLong("sendUserId");
-        Long userId = jsonObject.getLong("userId");
-        String type = jsonObject.getString("type");
-        String detail = jsonObject.getString("detail");
+        Long receiveUserId = jsonObject.getLong("receiveUserId");
+        String type = jsonObject.getString("type");     //消息分类 chat
+        int messageType = jsonObject.getInteger("messageType");     //消息类型
+        JSONObject sendText = jsonObject.getJSONObject("sendText");
         if (type.equals(MessageType.CHAT.getType())) {
             log.debug("聊天消息推送");
-            sendToUser(String.valueOf(userId), JSONObject.toJSONString(jsonObject));
-            //存储历史消息
+            sendToUser(String.valueOf(receiveUserId), JSONObject.toJSONString(jsonObject));
+
             CommunityChatMsg chatMsg = new CommunityChatMsg();
             chatMsg.setSenderId(sendUserId);
-            chatMsg.setReceiverId(userId);
-            chatMsg.setContent(detail);
+            chatMsg.setReceiverId(receiveUserId);
             chatMsg.setCreateBy(sendUserId);
+            chatMsg.setMessageType(messageType);
             chatMsg.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+            switch (messageType) {
+                case 0:
+                    //文字
+                    String context = sendText.getString("context");
+                    //存储历史消息
+                    chatMsg.setContent(context);
+                    break;
+                case 1:
+                    //图片
+                    chatMsg.setFileUrl(sendText.getString("fileUrl"));
+                    break;
+                case 2:
+                    //语音
+                    chatMsg.setFileUrl(sendText.getString("fileUrl"));
+                    chatMsg.setTime(sendText.getInteger("time"));
+                    break;
+                case 3:
+                    //定位
+                    String address = sendText.getString("address");
+                    Double latitude = sendText.getDouble("latitude");       //纬度
+                    Double longitude = sendText.getDouble("longitude");     //经度
+                    chatMsg.setAddress(address);
+                    chatMsg.setLatitude(latitude);
+                    chatMsg.setLongitude(longitude);
+                    break;
+                case 4:
+                    //视频
+                    chatMsg.setFileUrl(sendText.getString("fileUrl"));
+                    break;
+
+            }
+
+            //存储历史消息
             chatMsgService.save(chatMsg);
         }
     }

+ 44 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityChatMsgController.java

@@ -0,0 +1,44 @@
+package com.ruoyi.generator.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.system.domain.CommunityChatMsg;
+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.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 聊天记录
+ *
+ * @author fangzhen
+ */
+@Api(tags = "聊天记录")
+@RestController
+@RequestMapping("/community/chat")
+public class CommunityChatMsgController extends BaseController {
+
+    @Resource
+    private ICommunityChatMsgService communityChatMsgService;
+
+    /**
+     * 获取当前登录用户聊天记录
+     */
+    @ApiOperation("获取当前登录用户聊天记录")
+    @GetMapping()
+    public AjaxResult chatRecord() {
+        Long userId = SecurityUtils.getUserId();
+        List<CommunityChatMsg> chatMsgList = communityChatMsgService.list(new QueryWrapper<CommunityChatMsg>()
+                .eq("sender_id", userId)
+                .or()
+                .eq("receiver_id", userId).orderByDesc("create_time"));
+        return AjaxResult.success(chatMsgList);
+    }
+}

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

@@ -39,6 +39,30 @@ public class CommunityChatMsg implements Serializable {
     * 消息接收人id
     */
     private Long receiverId;
+    /**
+     * 地址
+     */
+    private String address;
+    /**
+     * 纬度
+     */
+    private Double latitude;
+    /**
+     * 经度
+     */
+    private Double longitude;
+    /**
+     * 文件地址(录音视频)
+     */
+    private String fileUrl;
+    /**
+     * 录音时长
+     */
+    private int time;
+    /**
+     * 消息类型
+     */
+    private int messageType;
     /**
     * 创建时间
     */