package com.project.admin.controller.monitor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import com.project.system.domain.bo.queryBo.SysUserOnlineQueryBo; 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.project.common.annotation.Log; import com.project.common.constant.CacheConstants; import com.project.common.core.controller.BaseController; import com.project.common.core.domain.AjaxResult; import com.project.common.core.domain.model.LoginUser; import com.project.common.core.page.TableDataInfo; import com.project.common.core.redis.RedisCache; import com.project.common.enums.BusinessType; import com.project.common.utils.StringUtils; import com.project.system.domain.SysUserOnline; import com.project.system.service.ISysUserOnlineService; /** * 在线用户监控 * * @author project */ @RestController @RequestMapping("/monitor/online") public class SysUserOnlineController extends BaseController { @Autowired private ISysUserOnlineService userOnlineService; @Autowired private RedisCache redisCache; @PreAuthorize("@ss.hasPermi('monitor:online:list')") @GetMapping("/list") public TableDataInfo list(SysUserOnlineQueryBo bo) { startPage(); Collection keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*"); List userOnlineList = new ArrayList<>(); for (String key : keys) { LoginUser user = redisCache.getCacheObject(key); if (StringUtils.isNotEmpty(bo.getIpaddr()) && StringUtils.isNotEmpty(bo.getUserName())) { userOnlineList.add(userOnlineService.selectOnlineByInfo(bo.getIpaddr(), bo.getUserName(), user)); } else if (StringUtils.isNotEmpty(bo.getIpaddr())) { userOnlineList.add(userOnlineService.selectOnlineByIpaddr(bo.getIpaddr(), user)); } else if (StringUtils.isNotEmpty(bo.getUserName()) && StringUtils.isNotNull(user.getUser())) { userOnlineList.add(userOnlineService.selectOnlineByUserName(bo.getUserName(), user)); } else { userOnlineList.add(userOnlineService.loginUserToUserOnline(user)); } } Collections.reverse(userOnlineList); userOnlineList.removeAll(Collections.singleton(null)); return getListDataTable(userOnlineList); } /** * 强退用户 */ @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); return success(); } }