完成用户在线状态查询
This commit is contained in:
@@ -203,4 +203,15 @@ public class UserController {
|
|||||||
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,4 +92,11 @@ public interface UserService {
|
|||||||
* @return 用户列表
|
* @return 用户列表
|
||||||
*/
|
*/
|
||||||
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
|
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断用户是否在线
|
||||||
|
* @param userId 用户ID
|
||||||
|
* @return 是否在线
|
||||||
|
*/
|
||||||
|
boolean isUserOnline(Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -491,4 +491,23 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
return result;
|
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