package me.zhengjie.base.util; import com.alibaba.fastjson.JSON; import lombok.RequiredArgsConstructor; import me.zhengjie.application.admin.service.dto.JwtUserDto; import me.zhengjie.security.service.dto.OnlineUserDto; import me.zhengjie.utils.RedisUtils; import me.zhengjie.utils.SecurityUtils; import me.zhengjie.utils.enums.DataScopeEnum; import org.springframework.stereotype.Service; import java.util.List; @Service @RequiredArgsConstructor public class ApplicationContextUtil { private final RedisUtils redisUtils; /** * 获取指定缓存Key的用户信息 * @param key * @return */ public JwtUserDto getUserByKey(String key) { return JSON.parseObject((String) redisUtils.get(key), JwtUserDto.class); } /** * 获取当前登录的用户 * @return UserDetails */ public JwtUserDto getCurrentUser() { OnlineUserDto onlineUser = (OnlineUserDto) SecurityUtils.getCurrentUser(); return JSON.parseObject((String) redisUtils.get(onlineUser.getOnlineToken()), JwtUserDto.class); } /** * 获取系统用户名称 * * @return 系统用户名称 */ public String getCurrentUsername() { JwtUserDto jwtUserDto = getCurrentUser(); return jwtUserDto.getUsername(); } /** * 获取系统用户ID * @return 系统用户ID */ public Long getCurrentUserId() { JwtUserDto jwtUserDto = getCurrentUser(); return jwtUserDto.getUserId(); } /** * 获取当前用户的数据权限 * @return / */ public List getCurrentUserDataScope(){ JwtUserDto jwtUserDto = getCurrentUser(); return jwtUserDto.getDataScopes(); } /** * 获取数据权限级别 * @return 级别 */ public String getDataScopeType() { List dataScopes = getCurrentUserDataScope(); if(dataScopes.size() != 0){ return ""; } return DataScopeEnum.ALL.getValue(); } }