mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-11-04 10:57:10 +08:00 
			
		
		
		
	refactor: 使用 Crane4j 优化在线用户数据填充
This commit is contained in:
		@@ -41,6 +41,11 @@ public class ContainerConstants extends ContainerPool {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public static final String ROLE_DEPT_ID_LIST = "RoleDeptIdList";
 | 
					    public static final String ROLE_DEPT_ID_LIST = "RoleDeptIdList";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 在线用户最后活跃时间
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static final String ONLINE_USER_LAST_ACTIVE_TIME = "OnlineUserLastActiveTime";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private ContainerConstants() {
 | 
					    private ContainerConstants() {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -20,7 +20,7 @@ import cn.crane4j.annotation.Assemble;
 | 
				
			|||||||
import cn.crane4j.annotation.Mapping;
 | 
					import cn.crane4j.annotation.Mapping;
 | 
				
			||||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
					import io.swagger.v3.oas.annotations.media.Schema;
 | 
				
			||||||
import lombok.Data;
 | 
					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.Serial;
 | 
				
			||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
@@ -43,13 +43,14 @@ public class OnlineUserResp implements Serializable {
 | 
				
			|||||||
     * ID
 | 
					     * ID
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Schema(description = "ID", example = "1")
 | 
					    @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;
 | 
					    private Long id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 令牌
 | 
					     * 令牌
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Schema(description = "令牌", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiTUd6djdyOVFoeHEwdVFqdFAzV3M5YjVJRzh4YjZPSEUifQ.7q7U3ouoN7WPhH2kUEM7vPe5KF3G_qavSG-vRgIxKvE")
 | 
					    @Schema(description = "令牌", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiTUd6djdyOVFoeHEwdVFqdFAzV3M5YjVJRzh4YjZPSEUifQ.7q7U3ouoN7WPhH2kUEM7vPe5KF3G_qavSG-vRgIxKvE")
 | 
				
			||||||
 | 
					    @Assemble(container = ContainerConstants.ONLINE_USER_LAST_ACTIVE_TIME, props = @Mapping(ref = "lastActiveTime"))
 | 
				
			||||||
    private String token;
 | 
					    private String token;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,7 @@ import top.continew.admin.common.model.dto.LoginUser;
 | 
				
			|||||||
import top.continew.starter.extension.crud.model.query.PageQuery;
 | 
					import top.continew.starter.extension.crud.model.query.PageQuery;
 | 
				
			||||||
import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
					import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.time.LocalDateTime;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -49,6 +50,14 @@ public interface OnlineUserService {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    List<LoginUser> list(OnlineUserQuery query);
 | 
					    List<LoginUser> list(OnlineUserQuery query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 查询 Token 最后活跃时间
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param token Token
 | 
				
			||||||
 | 
					     * @return 最后活跃时间
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    LocalDateTime getLastActiveTime(String token);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 根据角色 ID 清除
 | 
					     * 根据角色 ID 清除
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,8 @@
 | 
				
			|||||||
package top.continew.admin.auth.service.impl;
 | 
					package top.continew.admin.auth.service.impl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cn.crane4j.annotation.AutoOperate;
 | 
					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.dao.SaTokenDao;
 | 
				
			||||||
import cn.dev33.satoken.exception.NotLoginException;
 | 
					import cn.dev33.satoken.exception.NotLoginException;
 | 
				
			||||||
import cn.dev33.satoken.stp.StpUtil;
 | 
					import cn.dev33.satoken.stp.StpUtil;
 | 
				
			||||||
@@ -28,12 +30,14 @@ import org.springframework.stereotype.Service;
 | 
				
			|||||||
import top.continew.admin.auth.model.query.OnlineUserQuery;
 | 
					import top.continew.admin.auth.model.query.OnlineUserQuery;
 | 
				
			||||||
import top.continew.admin.auth.model.resp.OnlineUserResp;
 | 
					import top.continew.admin.auth.model.resp.OnlineUserResp;
 | 
				
			||||||
import top.continew.admin.auth.service.OnlineUserService;
 | 
					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.model.dto.LoginUser;
 | 
				
			||||||
import top.continew.admin.common.util.helper.LoginHelper;
 | 
					import top.continew.admin.common.util.helper.LoginHelper;
 | 
				
			||||||
import top.continew.starter.core.constant.StringConstants;
 | 
					import top.continew.starter.core.constant.StringConstants;
 | 
				
			||||||
import top.continew.starter.extension.crud.model.query.PageQuery;
 | 
					import top.continew.starter.extension.crud.model.query.PageQuery;
 | 
				
			||||||
import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
					import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.time.LocalDateTime;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.Comparator;
 | 
					import java.util.Comparator;
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
@@ -54,14 +58,7 @@ public class OnlineUserServiceImpl implements OnlineUserService {
 | 
				
			|||||||
    public PageResp<OnlineUserResp> page(OnlineUserQuery query, PageQuery pageQuery) {
 | 
					    public PageResp<OnlineUserResp> page(OnlineUserQuery query, PageQuery pageQuery) {
 | 
				
			||||||
        List<LoginUser> loginUserList = this.list(query);
 | 
					        List<LoginUser> loginUserList = this.list(query);
 | 
				
			||||||
        List<OnlineUserResp> list = BeanUtil.copyToList(loginUserList, OnlineUserResp.class);
 | 
					        List<OnlineUserResp> list = BeanUtil.copyToList(loginUserList, OnlineUserResp.class);
 | 
				
			||||||
        PageResp<OnlineUserResp> pageResp = PageResp.build(pageQuery.getPage(), pageQuery.getSize(), list);
 | 
					        return 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;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@@ -86,6 +83,13 @@ public class OnlineUserServiceImpl implements OnlineUserService {
 | 
				
			|||||||
        return loginUserList;
 | 
					        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
 | 
					    @Override
 | 
				
			||||||
    public void cleanByRoleId(Long roleId) {
 | 
					    public void cleanByRoleId(Long roleId) {
 | 
				
			||||||
        List<LoginUser> loginUserList = this.list(new OnlineUserQuery());
 | 
					        List<LoginUser> loginUserList = this.list(new OnlineUserQuery());
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user