|
@@ -1,12 +1,9 @@
|
|
|
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;
|
|
|
+import com.usky.common.core.utils.StringUtils;
|
|
|
import com.usky.common.log.annotation.Log;
|
|
|
import com.usky.common.log.enums.BusinessType;
|
|
|
import com.usky.common.redis.core.RedisService;
|
|
@@ -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,63 +23,101 @@ 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<>();
|
|
|
+
|
|
|
+ for (String key : keys) {
|
|
|
+ // 检查键是否存在并且未过期
|
|
|
+ if (redisService.hasKey(key)) {
|
|
|
+ LoginUser user = redisService.getCacheObject(key);
|
|
|
+ if (user != null) {
|
|
|
+ SysUserOnline online = userOnlineService.loginUserToUserOnline(user);
|
|
|
+ if (online != null) {
|
|
|
+ List<SysUserOnline> list = userOnlineMap.computeIfAbsent(user.getUsername(), k -> new ArrayList<>());
|
|
|
+ list.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());
|
|
|
+ int loginCount = sortedList.size(); // 计算登录次数
|
|
|
+ SysUserOnline lastOnline = sortedList.get(0); // 取最后登录时间的记录
|
|
|
+ lastOnline.setLoginCount(loginCount); // 设置登录记录的数量
|
|
|
+ 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));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 强退用户
|
|
|
*/
|
|
|
- @RequiresPermissions("monitor:online:forceLogout")
|
|
|
+// @RequiresPermissions("monitor:online:forceLogout")
|
|
|
@Log(title = "在线用户", businessType = BusinessType.FORCE)
|
|
|
@DeleteMapping("/{tokenId}")
|
|
|
- public ApiResult forceLogout(@PathVariable String tokenId)
|
|
|
- {
|
|
|
- redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
|
|
|
- return ApiResult.success();
|
|
|
+ public ApiResult forceLogout(@PathVariable String tokenId) {
|
|
|
+ try {
|
|
|
+ if (StringUtils.isBlank(tokenId)) {
|
|
|
+ logger.error("Token ID is blank for force logout");
|
|
|
+ return ApiResult.error("SYS-0004", "Token ID is required for force logout");
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = CacheConstants.LOGIN_TOKEN_KEY + tokenId;
|
|
|
+ boolean deleted = redisService.deleteObject(key);
|
|
|
+ if (deleted) {
|
|
|
+ logger.info("User with tokenId {} has been forcibly logged out.", tokenId);
|
|
|
+ return ApiResult.success();
|
|
|
+ } else {
|
|
|
+ logger.warn("Failed to find and delete user session with tokenId {}", tokenId);
|
|
|
+ return ApiResult.error("SYS-0002", "Session not found or already deleted");
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error("Error processing forceLogout for tokenId {}", tokenId, ex);
|
|
|
+ return ApiResult.error("SYS-0003", "Internal system error during force logout", ex.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|