zhaowenyu
2026-03-07 d7fef91a4cbb702988dc820bcde82c166253ceec
ard-admin/src/main/java/com/ard/web/controller/monitor/SysUserOnlineController.java
@@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -20,18 +21,17 @@
import com.ard.common.core.redis.RedisCache;
import com.ard.common.enums.BusinessType;
import com.ard.common.utils.StringUtils;
import com.ard.system.system.domain.SysUserOnline;
import com.ard.system.system.service.ISysUserOnlineService;
import com.ard.system.domain.SysUserOnline;
import com.ard.system.service.ISysUserOnlineService;
/**
 * 在线用户监控
 *
 * @author ruoyi
 *
 * @author ard
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController
{
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;
@@ -39,28 +39,19 @@
    private RedisCache redisCache;
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName)
    {
    @GetMapping("/list1")
    public TableDataInfo list1(String ipaddr, String userName) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys)
        {
        for (String key : keys) {
            LoginUser user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser()))
            {
            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            }
            else
            {
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
@@ -69,15 +60,66 @@
        return getDataTable(userOnlineList);
    }
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName) {
        startPage();  // ❗ Ruoyi 分页
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> resultList = new ArrayList<>();
        boolean searchIp = StringUtils.isNotEmpty(ipaddr);
        boolean searchName = StringUtils.isNotEmpty(userName);
        for (String key : keys) {
            LoginUser loginUser = redisCache.getCacheObject(key);
            if (loginUser == null || loginUser.getUser() == null)
                continue;
            // 转换成 SysUserOnline
            SysUserOnline onlineUser = userOnlineService.loginUserToUserOnline(loginUser);
            if (onlineUser == null) continue;
            boolean match = true;
            // ⭐ IP 模糊匹配
            if (searchIp) {
                String userIp = loginUser.getIpaddr();
                match = userIp != null && userIp.contains(ipaddr);
            }
            // ⭐ 用户名模糊匹配
            if (match && searchName) {
                String nick = loginUser.getUsername();
                match = nick != null && nick.contains(userName);
            }
            if (match)
                resultList.add(onlineUser);
        }
        Collections.reverse(resultList);
        return getDataTable(resultList);
    }
    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
    public AjaxResult forceLogout(@PathVariable String tokenId) {
        String tokenKey = CacheConstants.LOGIN_TOKEN_KEY + tokenId;
        LoginUser loginUser = redisCache.getCacheObject(tokenKey);
        if (loginUser != null && loginUser.getUser() != null) {
            Long userId = loginUser.getUser().getUserId();
            redisCache.deleteObject(CacheConstants.LOGIN_USERID_KEY + userId);
        }
        redisCache.deleteObject(tokenKey);
        return success();
    }
}