|
@@ -1,9 +1,6 @@
|
|
|
package com.usky.system.controller.web;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import com.usky.common.core.bean.ApiResult;
|
|
|
import com.usky.common.core.constants.CacheConstants;
|
|
@@ -15,13 +12,10 @@ import com.usky.system.controller.web.page.TableDataInfo;
|
|
|
import com.usky.system.domain.SysUserOnline;
|
|
|
import com.usky.system.model.LoginUser;
|
|
|
import com.usky.system.service.ISysUserOnlineService;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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 org.springframework.web.bind.annotation.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
|
/**
|
|
|
* 在线用户监控
|
|
@@ -29,52 +23,72 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping("/online")
|
|
|
-public class SysUserOnlineController extends BaseController
|
|
|
-{
|
|
|
+public class SysUserOnlineController {
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(SysUserOnlineController.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
private ISysUserOnlineService userOnlineService;
|
|
|
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
|
|
|
- @RequiresPermissions("monitor:online:list")
|
|
|
+ private TableDataInfo getDataTable(List<SysUserOnline> userOnlineList, int pageNum, int pageSize, long total) {
|
|
|
+ TableDataInfo dataInfo = new TableDataInfo();
|
|
|
+ dataInfo.setRows(userOnlineList);
|
|
|
+ dataInfo.setTotal(total);
|
|
|
+ dataInfo.setPageNum(pageNum);
|
|
|
+ dataInfo.setPageSize(pageSize);
|
|
|
+ dataInfo.setPages((int) Math.ceil((double) total / pageSize));
|
|
|
+ return dataInfo;
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/list")
|
|
|
- public ApiResult<TableDataInfo> list(String ipaddr, String userName)
|
|
|
- {
|
|
|
- Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
|
|
|
- List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
|
|
|
- for (String key : keys)
|
|
|
- {
|
|
|
- LoginUser user = redisService.getCacheObject(key);
|
|
|
- if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
|
|
|
- {
|
|
|
- if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()))
|
|
|
- {
|
|
|
- userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
|
|
|
- }
|
|
|
- }
|
|
|
- else if (StringUtils.isNotEmpty(ipaddr))
|
|
|
- {
|
|
|
- if (StringUtils.equals(ipaddr, user.getIpaddr()))
|
|
|
- {
|
|
|
- userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
|
|
|
- }
|
|
|
+ public ApiResult<TableDataInfo> list(@RequestParam(required = false, defaultValue = "1") Integer page,
|
|
|
+ @RequestParam(required = false, defaultValue = "10") Integer size) {
|
|
|
+ try {
|
|
|
+ Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
|
|
|
+ if (keys == null || keys.isEmpty()) {
|
|
|
+ logger.error("Redis keys is null or empty");
|
|
|
+ return ApiResult.error("SYS-0000", "Redis keys is null or empty");
|
|
|
}
|
|
|
- else if (StringUtils.isNotEmpty(userName))
|
|
|
- {
|
|
|
- if (StringUtils.equals(userName, user.getUsername()))
|
|
|
- {
|
|
|
- userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
|
|
|
+ Map<String, List<SysUserOnline>> userOnlineMap = new HashMap<>();
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ for (String key : keys) {
|
|
|
+ // 检查键是否存在并且未过期
|
|
|
+ if (redisService.hasKey(key)) {
|
|
|
+ LoginUser user = redisService.getCacheObject(key);
|
|
|
+ if (user != null) {
|
|
|
+ SysUserOnline online = userOnlineService.loginUserToUserOnline(user);
|
|
|
+ if (online != null) {
|
|
|
+ userOnlineMap.computeIfAbsent(user.getUsername(), k -> new ArrayList<>()).add(online);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
|
|
|
+
|
|
|
+ List<SysUserOnline> userOnlineList = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, List<SysUserOnline>> entry : userOnlineMap.entrySet()) {
|
|
|
+ List<SysUserOnline> sortedList = entry.getValue();
|
|
|
+ sortedList.sort(Comparator.comparing(SysUserOnline::getLoginTime).reversed());
|
|
|
+ SysUserOnline lastOnline = sortedList.get(0); // 取最后登录时间的记录
|
|
|
+ lastOnline.setPreviousOnlines(sortedList.subList(1, sortedList.size())); // 设置其他登录记录
|
|
|
+ userOnlineList.add(lastOnline);
|
|
|
}
|
|
|
+
|
|
|
+ // 按照登录时间降序排序
|
|
|
+ userOnlineList.sort(Comparator.comparing(SysUserOnline::getLoginTime).reversed());
|
|
|
+
|
|
|
+ long total = userOnlineList.size();
|
|
|
+ int start = (page - 1) * size;
|
|
|
+ int end = Math.min(start + size, (int) total);
|
|
|
+ List<SysUserOnline> pageList = userOnlineList.subList(start, end);
|
|
|
+
|
|
|
+ return ApiResult.success(getDataTable(pageList, page, size, total));
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error("Error processing /online/list", ex);
|
|
|
+ return ApiResult.error("SYS-0001", "Internal system error", ex.getMessage());
|
|
|
}
|
|
|
- Collections.reverse(userOnlineList);
|
|
|
- userOnlineList.removeAll(Collections.singleton(null));
|
|
|
- return ApiResult.success(getDataTable(userOnlineList));
|
|
|
}
|
|
|
|
|
|
/**
|