Compare commits
3 Commits
6d95b8d391
...
feature-ad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39af4e6596 | ||
|
|
ea5c95584d | ||
|
|
e170016019 |
@@ -203,4 +203,15 @@ public class UserController {
|
||||
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户是否在线
|
||||
* @param userId 用户ID
|
||||
* @return 用户是否在线
|
||||
*/
|
||||
@GetMapping("/{userId}/online")
|
||||
public Result<Boolean> isUserOnline(@PathVariable Long userId) {
|
||||
|
||||
boolean online = userService.isUserOnline(userId);
|
||||
return Result.success(ResultCode.SUCCESS, "查询成功", online);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +29,6 @@ public class ChatSessionsVO {
|
||||
private Integer topStatus;
|
||||
/** 免打扰状态 */
|
||||
private Integer muteStatus;
|
||||
/** 会话状态 */
|
||||
private Boolean online;
|
||||
}
|
||||
|
||||
@@ -26,4 +26,5 @@ public class UserInfoVO implements Serializable {
|
||||
private LocalDateTime createdAt;
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
private Boolean online;
|
||||
}
|
||||
|
||||
@@ -92,4 +92,11 @@ public interface UserService {
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
|
||||
|
||||
/**
|
||||
* 判断用户是否在线
|
||||
* @param userId 用户ID
|
||||
* @return 是否在线
|
||||
*/
|
||||
boolean isUserOnline(Long userId);
|
||||
}
|
||||
|
||||
@@ -236,6 +236,7 @@ public class ChatServiceImpl implements ChatService {
|
||||
vo.setSessionName("用户" + session.getTargetUserId());
|
||||
vo.setAvatarUrl(null);
|
||||
}
|
||||
vo.setOnline(userService.isUserOnline(vo.getTargetUserId()));
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -124,6 +124,9 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public void logout(String token) {
|
||||
Claims claims = JwtUtil.getClaimsFromToken(token);
|
||||
// 获取token信息
|
||||
String subject = claims.getSubject();
|
||||
// 获取token的过期时间
|
||||
Date expiration = claims.getExpiration();
|
||||
// 判断 token 是否已过期
|
||||
long ttl = expiration.getTime() - System.currentTimeMillis();
|
||||
@@ -132,6 +135,10 @@ public class UserServiceImpl implements UserService {
|
||||
return;
|
||||
}
|
||||
|
||||
// 从Redis中删除登录token记录
|
||||
String loginTokenKey = "login:token:" + subject;
|
||||
redisTemplate.delete(loginTokenKey);
|
||||
|
||||
String logoutKey = "jwt:blacklist:" + token;
|
||||
redisTemplate.opsForValue().set(
|
||||
logoutKey,
|
||||
@@ -491,4 +498,23 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUserOnline(Long userId) {
|
||||
if (userId == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 是否被封禁
|
||||
String banKey = "user:ban:" + userId;
|
||||
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 是否存在登录 token
|
||||
String tokenKey = "login:token:" + userId;
|
||||
Boolean online = redisTemplate.hasKey(tokenKey);
|
||||
|
||||
return Boolean.TRUE.equals(online);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user