diff --git a/im-platform/src/main/java/com/bx/implatform/service/INotifyPrivateService.java b/im-platform/src/main/java/com/bx/implatform/service/INotifyPrivateService.java index 6fa2f5a..323d889 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/INotifyPrivateService.java +++ b/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 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 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); diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java index 3c0dc4d..0904735 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java @@ -220,7 +220,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl 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 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 implements IU UserSession session = SessionContext.getSession(); if(StrUtil.isNotEmpty(dto.getCid())){ // 清除cid,不再推送离线通知 + notifyPrivateService.removeNotifySession(session.getUserId()); LambdaUpdateWrapper wrapper = Wrappers.lambdaUpdate(); wrapper.eq(User::getId,session.getUserId()); wrapper.eq(User::getCid,dto.getCid());