Skip to content

Commit

Permalink
refactor: 使用 Crane4j 优化在线用户数据填充
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed May 14, 2024
1 parent 6396e9a commit cb81135
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class ContainerConstants extends ContainerPool {
*/
public static final String ROLE_DEPT_ID_LIST = "RoleDeptIdList";

/**
* 在线用户最后活跃时间
*/
public static final String ONLINE_USER_LAST_ACTIVE_TIME = "OnlineUserLastActiveTime";

private ContainerConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import cn.crane4j.annotation.Mapping;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.starter.extension.crud.constant.ContainerPool;
import top.continew.admin.common.constant.ContainerConstants;

import java.io.Serial;
import java.io.Serializable;
Expand All @@ -43,13 +43,14 @@ public class OnlineUserResp implements Serializable {
* ID
*/
@Schema(description = "ID", example = "1")
@Assemble(container = ContainerPool.USER_NICKNAME, props = @Mapping(ref = "nickname"))
@Assemble(container = ContainerConstants.USER_NICKNAME, props = @Mapping(ref = "nickname"))
private Long id;

/**
* 令牌
*/
@Schema(description = "令牌", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiTUd6djdyOVFoeHEwdVFqdFAzV3M5YjVJRzh4YjZPSEUifQ.7q7U3ouoN7WPhH2kUEM7vPe5KF3G_qavSG-vRgIxKvE")
@Assemble(container = ContainerConstants.ONLINE_USER_LAST_ACTIVE_TIME, props = @Mapping(ref = "lastActiveTime"))
private String token;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.time.LocalDateTime;
import java.util.List;

/**
Expand Down Expand Up @@ -49,6 +50,14 @@ public interface OnlineUserService {
*/
List<LoginUser> list(OnlineUserQuery query);

/**
* 查询 Token 最后活跃时间
*
* @param token Token
* @return 最后活跃时间
*/
LocalDateTime getLastActiveTime(String token);

/**
* 根据角色 ID 清除
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package top.continew.admin.auth.service.impl;

import cn.crane4j.annotation.AutoOperate;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
Expand All @@ -28,12 +30,14 @@
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.model.resp.OnlineUserResp;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
Expand All @@ -54,14 +58,7 @@ public class OnlineUserServiceImpl implements OnlineUserService {
public PageResp<OnlineUserResp> page(OnlineUserQuery query, PageQuery pageQuery) {
List<LoginUser> loginUserList = this.list(query);
List<OnlineUserResp> list = BeanUtil.copyToList(loginUserList, OnlineUserResp.class);
PageResp<OnlineUserResp> pageResp = PageResp.build(pageQuery.getPage(), pageQuery.getSize(), list);
pageResp.getList().forEach(u -> {
long lastActiveTime = StpUtil.getStpLogic().getTokenLastActiveTime(u.getToken());
if (SaTokenDao.NOT_VALUE_EXPIRE != lastActiveTime) {
u.setLastActiveTime(DateUtil.toLocalDateTime(new Date(lastActiveTime)));
}
});
return pageResp;
return PageResp.build(pageQuery.getPage(), pageQuery.getSize(), list);
}

@Override
Expand All @@ -86,6 +83,13 @@ public List<LoginUser> list(OnlineUserQuery query) {
return loginUserList;
}

@Override
@ContainerMethod(namespace = ContainerConstants.ONLINE_USER_LAST_ACTIVE_TIME, type = MappingType.ORDER_OF_KEYS)
public LocalDateTime getLastActiveTime(String token) {
long lastActiveTime = StpUtil.getStpLogic().getTokenLastActiveTime(token);
return lastActiveTime == SaTokenDao.NOT_VALUE_EXPIRE ? null : DateUtil.date(lastActiveTime).toLocalDateTime();
}

@Override
public void cleanByRoleId(Long roleId) {
List<LoginUser> loginUserList = this.list(new OnlineUserQuery());
Expand Down

0 comments on commit cb81135

Please sign in to comment.