| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 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<Long> getCurrentUserDataScope(){
- JwtUserDto jwtUserDto = getCurrentUser();
- return jwtUserDto.getDataScopes();
- }
- /**
- * 获取数据权限级别
- * @return 级别
- */
- public String getDataScopeType() {
- List<Long> dataScopes = getCurrentUserDataScope();
- if(dataScopes.size() != 0){
- return "";
- }
- return DataScopeEnum.ALL.getValue();
- }
- }
|