xsx 1 год назад
Родитель
Сommit
edd01b70ce

+ 11 - 2
im-platform/src/main/java/com/bx/implatform/service/INotifyPrivateService.java

@@ -7,7 +7,9 @@ import com.bx.implatform.session.NotifySession;
 import com.bx.implatform.service.thirdparty.UniPushService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
@@ -27,8 +29,10 @@ import java.util.concurrent.TimeUnit;
 @Service
 @RequiredArgsConstructor
 public class INotifyPrivateService {
+    @Lazy
+    @Autowired
+    private IUserService userService;
     private final UniPushService uniPushService;
-    private final IUserService userService;
     private final RedisTemplate<String, Object> redisTemplate;
     @Value("${notify.enable}")
     private Boolean enable = false;
@@ -64,12 +68,17 @@ public class INotifyPrivateService {
         saveNotifySession(session,sendId,recvId);
     }
 
-    public void removeNotifySession(Long sendId, Long recvId){
+    public void removeNotifySession( Long recvId){
         String key = StrUtil.join(":", RedisKey.IM_OFFLINE_NOTIFY_PRIVATE, "*", recvId);
         Set<String> keys =  redisTemplate.keys(key);
         redisTemplate.delete(keys);
     }
 
+    public void removeNotifySession(Long sendId, Long recvId){
+        String key = StrUtil.join(":", RedisKey.IM_OFFLINE_NOTIFY_PRIVATE, sendId, recvId);
+        redisTemplate.delete(key);
+    }
+
     private NotifySession createNotifySession(Long sendId, Long recvId) {
         String key = StrUtil.join(":", RedisKey.IM_OFFLINE_NOTIFY_PRIVATE, sendId, recvId);
         User sendUser = userService.getById(sendId);

+ 1 - 1
im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java

@@ -220,7 +220,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper,
                 .set(PrivateMessage::getStatus, MessageStatus.READED.code());
         this.update(updateWrapper);
         // 清除通知会话信息
-        notifyPrivateService.removeNotifySession(msgInfo.getSendId(),msgInfo.getRecvId());
+        notifyPrivateService.removeNotifySession(friendId,session.getUserId());
 
         log.info("消息已读,接收方id:{},发送方id:{}", session.getUserId(), friendId);
     }

+ 6 - 2
im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java

@@ -23,6 +23,7 @@ import com.bx.implatform.exception.GlobalException;
 import com.bx.implatform.mapper.UserMapper;
 import com.bx.implatform.service.IFriendService;
 import com.bx.implatform.service.IGroupMemberService;
+import com.bx.implatform.service.INotifyPrivateService;
 import com.bx.implatform.service.IUserService;
 import com.bx.implatform.session.SessionContext;
 import com.bx.implatform.session.UserSession;
@@ -50,7 +51,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
     private final IFriendService friendService;
     private final JwtProperties jwtProperties;
     private final IMClient imClient;
-
+    private final INotifyPrivateService notifyPrivateService;
 
     @Override
     public LoginVO login(LoginDTO dto) {
@@ -63,8 +64,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         }
         // 更新用户登陆时间和cid
         user.setLastLoginTime(new Date());
-        if(StrUtil.isNotEmpty(dto.getCid())){
+        // 用户更换了设备,记录新的cid
+        if(StrUtil.isNotEmpty(dto.getCid()) && dto.getCid().equals(user.getCid())){
             user.setCid(dto.getCid());
+            notifyPrivateService.removeNotifySession(user.getId());
         }
         this.updateById(user);
         // 生成token
@@ -87,6 +90,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         UserSession session = SessionContext.getSession();
         if(StrUtil.isNotEmpty(dto.getCid())){
             // 清除cid,不再推送离线通知
+            notifyPrivateService.removeNotifySession(session.getUserId());
             LambdaUpdateWrapper<User> wrapper =  Wrappers.lambdaUpdate();
             wrapper.eq(User::getId,session.getUserId());
             wrapper.eq(User::getCid,dto.getCid());