zhaowenyu
2026-02-04 9c3c085032bddf6bb5e8b730067292e7f1520328
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.ard.web.controller.monitor;
 
import java.util.ArrayList;
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ard.common.annotation.Log;
import com.ard.common.constant.CacheConstants;
import com.ard.common.core.controller.BaseController;
import com.ard.common.core.domain.AjaxResult;
import com.ard.common.core.domain.model.LoginUser;
import com.ard.common.core.page.TableDataInfo;
import com.ard.common.core.redis.RedisCache;
import com.ard.common.enums.BusinessType;
import com.ard.common.utils.StringUtils;
import com.ard.system.domain.SysUserOnline;
import com.ard.system.service.ISysUserOnlineService;
 
/**
 * 在线用户监控
 *
 * @author ard
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;
 
    @Autowired
    private RedisCache redisCache;
 
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @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) {
            LoginUser user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        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) {
        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();
    }
}