Bladeren bron

消息服务展示badge,展示消息一览(未完成).移动部分通用工具到common

nnkwrik 6 jaren geleden
bovenliggende
commit
cc9775a85e
25 gewijzigde bestanden met toevoegingen van 418 en 48 verwijderingen
  1. 5 0
      common/pom.xml
  2. 1 1
      common/src/main/java/io/github/nnkwrik/common/client/UserClient.java
  3. 2 0
      common/src/main/java/io/github/nnkwrik/common/dto/Response.java
  4. 1 1
      common/src/main/java/io/github/nnkwrik/common/util/BeanListUtils.java
  5. 4 4
      goods-service/pom.xml
  6. 1 1
      goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/TestController.java
  7. 2 2
      goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/GoodsServiceImpl.java
  8. 1 1
      goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/UserServiceImpl.java
  9. 6 0
      im-service/pom.xml
  10. 1 1
      im-service/src/main/java/io/github/nnkwrik/imservice/ImServiceApplication.java
  11. 14 0
      im-service/src/main/java/io/github/nnkwrik/imservice/constant/MessageType.java
  12. 62 0
      im-service/src/main/java/io/github/nnkwrik/imservice/controller/ChatController.java
  13. 44 0
      im-service/src/main/java/io/github/nnkwrik/imservice/dao/HistoryMapper.java
  14. 1 1
      im-service/src/main/java/io/github/nnkwrik/imservice/model/po/History.java
  15. 14 0
      im-service/src/main/java/io/github/nnkwrik/imservice/model/po/HistoryExample.java
  16. 18 0
      im-service/src/main/java/io/github/nnkwrik/imservice/model/vo/ChatIndex.java
  17. 1 0
      im-service/src/main/java/io/github/nnkwrik/imservice/model/vo/WsMessage.java
  18. 13 0
      im-service/src/main/java/io/github/nnkwrik/imservice/service/IndexService.java
  19. 3 0
      im-service/src/main/java/io/github/nnkwrik/imservice/service/WebSocketService.java
  20. 149 0
      im-service/src/main/java/io/github/nnkwrik/imservice/service/impl/IndexServiceImpl.java
  21. 26 23
      im-service/src/main/java/io/github/nnkwrik/imservice/service/impl/WebSocketServiceImpl.java
  22. 7 7
      im-service/src/main/resources/application.yml
  23. 2 1
      im-service/src/test/java/io/github/nnkwrik/imservice/TestChatMapper.java
  24. 35 0
      im-service/src/test/java/io/github/nnkwrik/imservice/TestRedisClient.java
  25. 5 5
      user-service/pom.xml

+ 5 - 0
common/pom.xml

@@ -33,6 +33,11 @@
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>

+ 1 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/client/UserClient.java → common/src/main/java/io/github/nnkwrik/common/client/UserClient.java

@@ -1,4 +1,4 @@
-package io.github.nnkwrik.goodsservice.client;
+package io.github.nnkwrik.common.client;
 
 import io.github.nnkwrik.common.dto.Response;
 import io.github.nnkwrik.common.dto.SimpleUser;

+ 2 - 0
common/src/main/java/io/github/nnkwrik/common/dto/Response.java

@@ -28,6 +28,8 @@ public class Response<T> {
     //im
     public static final int MESSAGE_FORMAT_IS_WRONG = 5001;
     public static final int MESSAGE_IS_INCOMPLETE = 5002;
+    public static final int SENDER_AND_WS_IS_NOT_MATCH = 5003;
+    public static final int UPDATE_HISTORY_TO_SQL_FAIL = 5004;
 
     private int errno;
     private String errmsg;

+ 1 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/util/BeanListUtils.java → common/src/main/java/io/github/nnkwrik/common/util/BeanListUtils.java

@@ -1,4 +1,4 @@
-package io.github.nnkwrik.goodsservice.util;
+package io.github.nnkwrik.common.util;
 
 import org.springframework.beans.BeanUtils;
 

+ 4 - 4
goods-service/pom.xml

@@ -31,10 +31,10 @@
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.springframework.cloud</groupId>
-            <artifactId>spring-cloud-starter-openfeign</artifactId>
-        </dependency>
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.cloud</groupId>-->
+            <!--<artifactId>spring-cloud-starter-openfeign</artifactId>-->
+        <!--</dependency>-->
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 1 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/TestController.java

@@ -4,7 +4,7 @@ import io.github.nnkwrik.common.dto.JWTUser;
 import io.github.nnkwrik.common.dto.Response;
 import io.github.nnkwrik.common.token.TokenSolver;
 import io.github.nnkwrik.common.token.injection.JWT;
-import io.github.nnkwrik.goodsservice.client.UserClient;
+import io.github.nnkwrik.common.client.UserClient;
 import io.github.nnkwrik.goodsservice.model.po.Goods;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;

+ 2 - 2
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/GoodsServiceImpl.java

@@ -3,14 +3,14 @@ package io.github.nnkwrik.goodsservice.service.impl;
 import com.github.pagehelper.PageHelper;
 import io.github.nnkwrik.common.dto.Response;
 import io.github.nnkwrik.common.dto.SimpleUser;
-import io.github.nnkwrik.goodsservice.client.UserClient;
+import io.github.nnkwrik.common.client.UserClient;
 import io.github.nnkwrik.goodsservice.dao.CategoryMapper;
 import io.github.nnkwrik.goodsservice.dao.GoodsMapper;
 import io.github.nnkwrik.goodsservice.model.po.*;
 import io.github.nnkwrik.goodsservice.model.vo.CategoryPageVo;
 import io.github.nnkwrik.goodsservice.model.vo.CommentVo;
 import io.github.nnkwrik.goodsservice.service.GoodsService;
-import io.github.nnkwrik.goodsservice.util.BeanListUtils;
+import io.github.nnkwrik.common.util.BeanListUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;

+ 1 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/UserServiceImpl.java

@@ -3,7 +3,7 @@ package io.github.nnkwrik.goodsservice.service.impl;
 import com.github.pagehelper.PageHelper;
 import io.github.nnkwrik.common.dto.Response;
 import io.github.nnkwrik.common.dto.SimpleUser;
-import io.github.nnkwrik.goodsservice.client.UserClient;
+import io.github.nnkwrik.common.client.UserClient;
 import io.github.nnkwrik.goodsservice.dao.UserMapper;
 import io.github.nnkwrik.goodsservice.model.po.Goods;
 import io.github.nnkwrik.goodsservice.model.po.GoodsExample;

+ 6 - 0
im-service/pom.xml

@@ -61,6 +61,12 @@
             <version>1.3.1</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.pagehelper</groupId>
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
+            <version>1.2.10</version>
+        </dependency>
+
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>

+ 1 - 1
im-service/src/main/java/io/github/nnkwrik/imservice/ImServiceApplication.java

@@ -3,7 +3,7 @@ package io.github.nnkwrik.imservice;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages = "io.github.nnkwrik")
 public class ImServiceApplication {
 
     public static void main(String[] args) {

+ 14 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/constant/MessageType.java

@@ -0,0 +1,14 @@
+package io.github.nnkwrik.imservice.constant;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 13:42
+ */
+public class MessageType {
+
+    public static final int SYS_MESSAGE = 0;
+
+    public static final int USER_MESSAGE = 1;
+
+    public static final int ESTABLISH_CHAT = 2;
+}

+ 62 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/controller/ChatController.java

@@ -0,0 +1,62 @@
+package io.github.nnkwrik.imservice.controller;
+
+import io.github.nnkwrik.common.dto.JWTUser;
+import io.github.nnkwrik.common.dto.Response;
+import io.github.nnkwrik.common.token.injection.JWT;
+import io.github.nnkwrik.imservice.model.po.LastChat;
+import io.github.nnkwrik.imservice.model.vo.ChatIndex;
+import io.github.nnkwrik.imservice.redis.RedisClient;
+import io.github.nnkwrik.imservice.service.IndexService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 13:31
+ */
+@RestController
+@Slf4j
+public class ChatController {
+
+    @Autowired
+    private RedisClient redisClient;
+
+    @Autowired
+    private IndexService indexService;
+
+    //用于打开小程序时
+    @GetMapping("/chat/unreadCount")
+    public Response<Integer> getUnreadCount(@JWT JWTUser user) {
+
+        if (user == null) {
+            return Response.ok(0);
+        }
+        List<LastChat> unreadMessage = redisClient.hvals(user.getOpenId());
+        int unreadCount = unreadMessage.stream().mapToInt(LastChat::getUnreadCount).sum();
+        log.info("已登录用户查询未读消息个数, {} 条未读信息.用户id = {},用户昵称 = {}", unreadCount, user.getOpenId(), user.getNickName());
+
+        return Response.ok(unreadCount);
+    }
+
+    //打开消息一览时
+    @GetMapping("/chat/index")
+    public Response<List<ChatIndex>> getChatIndex(@JWT JWTUser user,
+                                                  @RequestParam(value = "page", defaultValue = "1") int page,
+                                                  @RequestParam(value = "size", defaultValue = "10") int size) {
+        if (user == null) {
+            return Response.ok(0);
+        }
+        List<ChatIndex> voList = indexService.showIndex(user.getOpenId(), page, size);
+        log.info("展示消息一览,展示{} 条信息.用户id = {},用户昵称 = {}", voList.size(), user.getOpenId(), user.getNickName());
+
+        return Response.ok(voList);
+
+
+    }
+
+}

+ 44 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/dao/HistoryMapper.java

@@ -1,10 +1,13 @@
 package io.github.nnkwrik.imservice.dao;
 
 import io.github.nnkwrik.imservice.model.po.History;
+import io.github.nnkwrik.imservice.model.po.HistoryExample;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 
+import java.util.List;
+
 /**
  * @author nnkwrik
  * @date 18/12/05 22:20
@@ -14,6 +17,47 @@ public interface HistoryMapper {
     @Select("insert into history (chat_id, u1_to_u2, message_type, message_body, send_time)\n" +
             "values (#{chatId}, #{u1ToU2}, #{messageType}, #{messageBody}, #{sendTime});")
     void addHistory(History history);
+
+
+    /**
+     * 获取自己和所有人的最后一条"已读的"聊天记录,按时间倒序
+     * @param unreadChatIds 未读的chatId
+     * @return
+     */
+    @Select("<script>\n" +
+            "select history.chat_id, history.message_type, history.message_body, history.send_time, chat.u1, chat.u2, chat.goods_id\n" +
+            "from history\n" +
+            "       inner join (select chat_id, max(send_time) as max_time\n" +
+            "                   from history\n" +
+            "                   where chat_id in (select id\n" +
+            "                                     from chat\n" +
+            "                                     where id not in\n" +
+            "                       <foreach item = 'item' collection = 'unreadChatIds' open = '(' separator = ',' close = ')'>\n" +
+            "                       #{item}\n" +
+            "                       </foreach>\n" +
+            "                       and ((u1 = 1 and show_to_u1 = true) or (u2 = 1 and show_to_u2 = true)))\n" +
+            "                   group by chat_id) as foo on foo.chat_id = history.chat_id and foo.max_time = history.send_time\n" +
+            "        inner join chat where history.chat_id = chat.id\n"+
+            "order by send_time desc" +
+            "</script>")
+    List<HistoryExample> getLastReadChat(@Param("unreadChatIds") List<Integer> unreadChatIds);
+
+
+    /**
+     * 获取自己和所有人的最后一条聊天记录,按时间倒序
+     * @return
+     */
+    @Select("select history.chat_id, history.message_type, history.message_body, history.send_time, chat.u1, chat.u2, chat.goods_id\n" +
+            "from history\n" +
+            "       inner join (select chat_id, max(send_time) as max_time\n" +
+            "                   from history\n" +
+            "                   where chat_id in\n" +
+            "                         (select id from chat where ((u1 = 1 and show_to_u1 = true) or (u2 = 1 and show_to_u2 = true)))\n" +
+            "                   group by chat_id) as foo on foo.chat_id = history.chat_id and foo.max_time = history.send_time\n" +
+            "        inner join chat where history.chat_id = chat.id\n"+
+            "order by send_time desc")
+    List<HistoryExample> getLastChat();
+
 //    @Select("select message_type,hint,message_body,create_time\n" +
 //            "from chat_history\n" +
 //            "where chat_user_id = 1\n" +

+ 1 - 1
im-service/src/main/java/io/github/nnkwrik/imservice/model/po/History.java

@@ -13,7 +13,7 @@ public class History {
     private Integer id;
     private Integer chatId;
     private Boolean u1ToU2;
-    private Integer messageType;    //0:系统消息,1.用户消息,2.建立连接(u1点开了u2但没发消息, u1ToU2 = false,type = )
+    private Integer messageType;    //   0:系统消息,1.用户消息,2.单方建立会话
     private String messageBody;
     private Date sendTime;
 }

+ 14 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/model/po/HistoryExample.java

@@ -0,0 +1,14 @@
+package io.github.nnkwrik.imservice.model.po;
+
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 16:05
+ */
+@Data
+public class HistoryExample extends History {
+    private String u1;
+    private String u2;
+    private Integer goodsId;
+}

+ 18 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/model/vo/ChatIndex.java

@@ -0,0 +1,18 @@
+package io.github.nnkwrik.imservice.model.vo;
+
+import io.github.nnkwrik.common.dto.SimpleGoods;
+import io.github.nnkwrik.common.dto.SimpleUser;
+import io.github.nnkwrik.imservice.model.po.History;
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 15:56
+ */
+@Data
+public class ChatIndex {
+    private SimpleUser otherSide;
+    private SimpleGoods goods;
+    private Integer unreadCount;
+    private History lastChat;
+}

+ 1 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/model/vo/WsMessage.java

@@ -12,6 +12,7 @@ import java.util.Date;
  */
 @Data
 public class WsMessage {
+    private Integer chatId;
     private String senderId;
     private String receiverId;
     private Integer goodsId;

+ 13 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/service/IndexService.java

@@ -0,0 +1,13 @@
+package io.github.nnkwrik.imservice.service;
+
+import io.github.nnkwrik.imservice.model.vo.ChatIndex;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 16:31
+ */
+public interface IndexService {
+    List<ChatIndex> showIndex(String userId, int page, int size);
+}

+ 3 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/service/WebSocketService.java

@@ -1,9 +1,12 @@
 package io.github.nnkwrik.imservice.service;
 
+import io.github.nnkwrik.imservice.model.vo.WsMessage;
+
 /**
  * @author nnkwrik
  * @date 18/12/05 12:28
  */
 public interface WebSocketService {
     void OnMessage(String senderId, String message);
+
 }

+ 149 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/service/impl/IndexServiceImpl.java

@@ -0,0 +1,149 @@
+package io.github.nnkwrik.imservice.service.impl;
+
+import com.github.pagehelper.PageHelper;
+import io.github.nnkwrik.imservice.dao.HistoryMapper;
+import io.github.nnkwrik.imservice.model.po.History;
+import io.github.nnkwrik.imservice.model.po.HistoryExample;
+import io.github.nnkwrik.imservice.model.po.LastChat;
+import io.github.nnkwrik.imservice.model.vo.ChatIndex;
+import io.github.nnkwrik.imservice.redis.RedisClient;
+import io.github.nnkwrik.imservice.service.IndexService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 16:32
+ */
+@Service
+public class IndexServiceImpl implements IndexService {
+
+    @Autowired
+    private RedisClient redisClient;
+
+    @Autowired
+    private HistoryMapper historyMapper;
+
+//    @Autowired
+//    private UserClient userClient;
+
+
+    @Override
+    public List<ChatIndex> showIndex(String userId, int page, int size) {
+        List<LastChat> unreadMessage = redisClient.hvals(userId);
+
+        List<ChatIndex> resultVoList = new ArrayList<>();    //最终要返回的值
+        List<Integer> goodsIds = new ArrayList<>();         //需要去商品服务查的id
+        List<String> userIds = new ArrayList<>();           //需要去用户服务查的id
+
+
+        int alreadyShow = size * (page - 1);
+        int needShow = page * size;
+        if (unreadMessage.size() >= needShow) {
+            //需要展示的内容全在redis中
+
+            List<LastChat> unread = getDisplayUnread(unreadMessage, alreadyShow, size);
+
+            dealUnread(unread, resultVoList, goodsIds, userIds);
+        } else if (unreadMessage.size() > alreadyShow) {
+            //需要的内容的一部分在redis(未读),一部分在sql(已读)
+
+            //未读消息
+            List<LastChat> unread = getDisplayUnread(unreadMessage, alreadyShow, size);
+            dealUnread(unread, resultVoList, goodsIds, userIds);
+
+            List<Integer> unreadChatIds = unread.stream()
+                    .map(chat -> chat.getLastMsg().getChatId())
+                    .collect(Collectors.toList());
+
+            //已读消息
+            int remain = needShow - unreadMessage.size();
+            PageHelper.offsetPage(0, remain);
+            List<HistoryExample> read = historyMapper.getLastReadChat(unreadChatIds);
+            dealRead(read,userId, resultVoList, goodsIds, userIds);
+
+
+        } else {
+            //需要的内容全在sql
+
+            int offset = alreadyShow - unreadMessage.size();
+            PageHelper.offsetPage(offset, size);
+            List<HistoryExample> read = historyMapper.getLastChat();
+            dealRead(read,userId, resultVoList, goodsIds, userIds);
+        }
+
+        //去用户服务查用户名字头像
+
+        //去商品服务查商品图片
+
+        //遍历赋值
+
+
+        return resultVoList;
+    }
+
+
+    private List<LastChat> getDisplayUnread(List<LastChat> unreadMessage, int offset, int size) {
+        List<LastChat> displayUnread = unreadMessage.stream()
+                .sorted((a, b) -> b.getLastMsg().getSenderId().compareTo(a.getLastMsg().getSenderId()))
+                .skip(offset)
+                .limit(size)
+                .collect(Collectors.toList());
+        return displayUnread;
+    }
+
+    private void dealUnread(List<LastChat> unread,
+                            List<ChatIndex> resultVoList,
+                            List<Integer> goodsIds, List<String> userIds) {
+
+        unread.stream().forEach(po -> {
+            //稍后去其他服务查询
+            goodsIds.add(po.getLastMsg().getGoodsId());
+            userIds.add(po.getLastMsg().getSenderId());
+
+            //设置未读数
+            ChatIndex vo = new ChatIndex();
+            vo.setUnreadCount(po.getUnreadCount());
+
+            //设置最后一条信息
+            History lastChat = new History();
+            BeanUtils.copyProperties(po.getLastMsg(), lastChat);
+            vo.setLastChat(lastChat);
+
+            resultVoList.add(vo);
+        });
+
+    }
+
+    private void dealRead(List<HistoryExample> read, String userId,
+                          List<ChatIndex> resultVoList,
+                          List<Integer> goodsIds, List<String> userIds) {
+
+        read.stream().forEach(po -> {
+
+            goodsIds.add(po.getGoodsId());
+            if (userId.equals(po.getU1())) {
+                userIds.add(po.getU2());
+            } else {
+                userIds.add(po.getU1());
+            }
+
+            //设置未读数
+            ChatIndex vo = new ChatIndex();
+            vo.setUnreadCount(0);
+
+            //设置最后一条信息
+            History lastChat = new History();
+            BeanUtils.copyProperties(po, lastChat);
+            vo.setLastChat(lastChat);
+
+            resultVoList.add(vo);
+        });
+
+    }
+}

+ 26 - 23
im-service/src/main/java/io/github/nnkwrik/imservice/service/impl/WebSocketServiceImpl.java

@@ -5,7 +5,6 @@ import io.github.nnkwrik.common.exception.GlobalException;
 import io.github.nnkwrik.common.util.JsonUtil;
 import io.github.nnkwrik.imservice.dao.ChatMapper;
 import io.github.nnkwrik.imservice.dao.HistoryMapper;
-import io.github.nnkwrik.imservice.model.po.Chat;
 import io.github.nnkwrik.imservice.model.po.History;
 import io.github.nnkwrik.imservice.model.po.LastChat;
 import io.github.nnkwrik.imservice.model.vo.WsMessage;
@@ -16,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
@@ -49,20 +49,34 @@ public class WebSocketServiceImpl implements WebSocketService {
             chatEndpoint.sendMessage(senderId, Response.fail(e.getErrno(), e.getErrmsg()));
             return;
         }
+        if (!senderId.equals(message.getSenderId())){
+            String msg = "发送者与ws连接中的不一致,消息发送失败";
+            log.info(msg);
+            chatEndpoint.sendMessage(senderId, Response.fail(Response.SENDER_AND_WS_IS_NOT_MATCH, msg));
+            return;
+        }
+
+
+        //更新数据库
+        try {
+            updateSQL(message);
+            updateRedis(message);
+        } catch (Exception e) {
+            String msg = "添加聊天记录时发生异常,消息发送失败";
+            log.info(msg);
+            e.printStackTrace();
+            chatEndpoint.sendMessage(senderId, Response.fail(Response.UPDATE_HISTORY_TO_SQL_FAIL, msg));
+            return;
+        }
 
         //如果接收方在线,转发ws消息到接收方
         if (chatEndpoint.hasConnect(message.getReceiverId())) {
             chatEndpoint.sendMessage(message.getReceiverId(), Response.ok(message));
         }
-
-        //更新redis
-        updateRedis(message);
-        //更新数据库
-        updateSQL(message);
     }
 
     private void updateRedis(WsMessage message) {
-        LastChat lastChat = redisClient.hget(message.getReceiverId(), message.getGoodsId()+"");
+        LastChat lastChat = redisClient.hget(message.getReceiverId(), message.getGoodsId() + "");
         if (lastChat != null) {
             lastChat.setUnreadCount(lastChat.getUnreadCount() + 1);
             lastChat.setLastMsg(message);
@@ -71,10 +85,11 @@ public class WebSocketServiceImpl implements WebSocketService {
             lastChat.setUnreadCount(1);
             lastChat.setLastMsg(message);
         }
-        redisClient.hset(message.getReceiverId(), message.getGoodsId()+"", lastChat);
+        redisClient.hset(message.getReceiverId(), message.getGoodsId() + "", lastChat);
     }
 
-    private void updateSQL(WsMessage message) {
+    @Transactional
+    public void updateSQL(WsMessage message) throws Exception {
         String u1 = null;
         String u2 = null;
         if (message.getSenderId().compareTo(message.getReceiverId()) > 0) {
@@ -85,22 +100,9 @@ public class WebSocketServiceImpl implements WebSocketService {
             u2 = message.getReceiverId();
         }
 
-        //取chatid
-        Integer chatId = chatMapper.getChatId(u1, u2, message.getGoodsId());
-        if (chatId == null) {
-            Chat chat = new Chat();
-            chat.setU1(u1);
-            chat.setU2(u2);
-            chat.setGoodsId(message.getGoodsId());
-            chatMapper.addChat(chat);
-            chatId = chat.getId();
-        }
-
-
         //添加聊天记录
         History history = new History();
         BeanUtils.copyProperties(message, history);
-        history.setChatId(chatId);
         boolean u1ToU2 = u1.equals(message.getSendTime()) ? true : false;
         history.setU1ToU2(u1ToU2);
         historyMapper.addHistory(history);
@@ -113,7 +115,8 @@ public class WebSocketServiceImpl implements WebSocketService {
             log.info(msg);
             throw new GlobalException(Response.MESSAGE_FORMAT_IS_WRONG, msg);
         }
-        if (StringUtils.isEmpty(wsMessage.getSenderId()) ||
+        if (ObjectUtils.isEmpty(wsMessage.getChatId()) ||
+                StringUtils.isEmpty(wsMessage.getSenderId()) ||
                 StringUtils.isEmpty(wsMessage.getReceiverId()) ||
                 ObjectUtils.isEmpty(wsMessage.getGoodsId()) ||
                 ObjectUtils.isEmpty(wsMessage.getMessageType()) ||

+ 7 - 7
im-service/src/main/resources/application.yml

@@ -14,11 +14,11 @@ spring:
     port: 6379
     host: localhost
     timeout: 30s
-#  jackson:
-#    default-property-inclusion: non_null  #如果值为null,构造json时不加入
-#    date-format: yyyy-MM-dd HH:mm    #指定Json中Date对象的传输格式.对于DTO还是使用以往的方式
-#    locale: zh_CN
-#    time-zone: Asia/Shanghai
+  jackson:
+    default-property-inclusion: non_null  #如果值为null,构造json时不加入
+    date-format: yyyy-MM-dd HH:mm    #指定Json中Date对象的传输格式.对于DTO还是使用以往的方式
+    locale: zh_CN
+    time-zone: Asia/Shanghai
 
 server:
   port: 8805
@@ -31,5 +31,5 @@ mybatis:
 #  level:
 #    org.springframework.web: info
 #    io.github.nnkwrik.goodsservice: debug
-#jwt:
-#  pub-key-file-name: RSA.pub
+jwt:
+  pub-key-file-name: RSA.pub

+ 2 - 1
im-service/src/test/java/io/github/nnkwrik/imservice/dao/TestChatMapper.java → im-service/src/test/java/io/github/nnkwrik/imservice/TestChatMapper.java

@@ -1,5 +1,6 @@
-package io.github.nnkwrik.imservice.dao;
+package io.github.nnkwrik.imservice;
 
+import io.github.nnkwrik.imservice.dao.ChatMapper;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;

+ 35 - 0
im-service/src/test/java/io/github/nnkwrik/imservice/TestRedisClient.java

@@ -0,0 +1,35 @@
+package io.github.nnkwrik.imservice;
+
+import io.github.nnkwrik.imservice.model.vo.WsMessage;
+import io.github.nnkwrik.imservice.redis.RedisClient;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/07 13:57
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+public class TestRedisClient {
+
+    @Autowired
+    private RedisClient redisClient;
+
+    @Test
+    public void testNoUnread(){
+        //not exist
+        List<WsMessage> hvals = redisClient.hvals("1212");
+        if (hvals == null){
+            System.out.println("null");
+        }else {
+            System.out.println(hvals.size());
+        }
+
+    }
+}

+ 5 - 5
user-service/pom.xml

@@ -48,11 +48,11 @@
         </dependency>
 
 
-        <dependency>
-            <groupId>com.github.binarywang</groupId>
-            <artifactId>weixin-java-miniapp</artifactId>
-            <version>3.2.0</version>
-        </dependency>
+        <!--<dependency>-->
+            <!--<groupId>com.github.binarywang</groupId>-->
+            <!--<artifactId>weixin-java-miniapp</artifactId>-->
+            <!--<version>3.2.0</version>-->
+        <!--</dependency>-->
 
         <dependency>
             <groupId>org.projectlombok</groupId>