重构:重构获取登录用户信息的代码逻辑
1.用户基本信息调整为实时获取 2.登录用户不再保存基本信息,降低维护成本 3.移除冗余的维护登录用户基本信息的代码
This commit is contained in:
parent
267ad9be13
commit
bc54acd60b
@ -25,7 +25,6 @@ import lombok.Data;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.GenderEnum;
|
||||
|
||||
/**
|
||||
* 登录用户信息
|
||||
@ -48,55 +47,25 @@ public class LoginUser implements Serializable {
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String nickname;
|
||||
|
||||
/**
|
||||
* 性别(0未知 1男 2女)
|
||||
*/
|
||||
private GenderEnum gender;
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 头像地址
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 最后一次修改密码时间
|
||||
*/
|
||||
private LocalDateTime pwdResetTime;
|
||||
|
||||
/**
|
||||
* 部门 ID
|
||||
*/
|
||||
private Long deptId;
|
||||
|
||||
/**
|
||||
* 部门名称
|
||||
* 权限码集合
|
||||
*/
|
||||
private String deptName;
|
||||
private Set<String> permissions;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
* 角色编码集合
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
private Set<String> roles;
|
||||
|
||||
/**
|
||||
* 角色集合
|
||||
*/
|
||||
private Set<RoleDTO> roleSet;
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
@ -123,21 +92,6 @@ public class LoginUser implements Serializable {
|
||||
*/
|
||||
private LocalDateTime loginTime;
|
||||
|
||||
/**
|
||||
* 权限码集合
|
||||
*/
|
||||
private Set<String> permissions;
|
||||
|
||||
/**
|
||||
* 角色编码集合
|
||||
*/
|
||||
private Set<String> roles;
|
||||
|
||||
/**
|
||||
* 角色集合
|
||||
*/
|
||||
private Set<RoleDTO> roleSet;
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
|
@ -26,10 +26,12 @@ import lombok.NoArgsConstructor;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.constant.CacheConsts;
|
||||
import top.charles7c.cnadmin.common.model.dto.LogContext;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.service.CommonUserService;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.IpUtils;
|
||||
import top.charles7c.cnadmin.common.util.ServletUtils;
|
||||
@ -110,7 +112,7 @@ public class LoginHelper {
|
||||
/**
|
||||
* 获取登录用户 ID
|
||||
*
|
||||
* @return /
|
||||
* @return 登录用户 ID
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getId());
|
||||
@ -119,7 +121,7 @@ public class LoginHelper {
|
||||
/**
|
||||
* 获取登录用户名
|
||||
*
|
||||
* @return /
|
||||
* @return 登录用户名
|
||||
*/
|
||||
public static String getUsername() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getUsername());
|
||||
@ -128,9 +130,20 @@ public class LoginHelper {
|
||||
/**
|
||||
* 获取登录用户昵称
|
||||
*
|
||||
* @return /
|
||||
* @return 登录用户昵称
|
||||
*/
|
||||
public static String getNickname() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getNickname());
|
||||
return getNickname(getUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户昵称
|
||||
*
|
||||
* @param userId
|
||||
* 登录用户 ID
|
||||
* @return 登录用户昵称
|
||||
*/
|
||||
public static String getNickname(Long userId) {
|
||||
return ExceptionUtils.exToNull(() -> SpringUtil.getBean(CommonUserService.class).getNicknameById(userId));
|
||||
}
|
||||
}
|
||||
|
@ -69,18 +69,18 @@ public class UserInfoVO implements Serializable {
|
||||
@Schema(description = "性别(0:未知,1:男,2:女)", type = "Integer", allowableValues = {"0", "1", "2"})
|
||||
private GenderEnum gender;
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
@Schema(description = "手机号码")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
@Schema(description = "邮箱")
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
@Schema(description = "手机号码")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 头像地址
|
||||
*/
|
||||
|
@ -39,13 +39,13 @@ import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.enums.MenuTypeEnum;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.SecureUtils;
|
||||
import top.charles7c.cnadmin.common.util.TreeUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
import top.charles7c.cnadmin.system.model.entity.UserDO;
|
||||
import top.charles7c.cnadmin.system.model.query.MenuQuery;
|
||||
import top.charles7c.cnadmin.system.model.vo.DeptDetailVO;
|
||||
import top.charles7c.cnadmin.system.model.vo.MenuVO;
|
||||
import top.charles7c.cnadmin.system.service.DeptService;
|
||||
import top.charles7c.cnadmin.system.service.MenuService;
|
||||
@ -75,10 +75,11 @@ public class LoginServiceImpl implements LoginService {
|
||||
Long userId = userDO.getId();
|
||||
CheckUtils.throwIfNotEqual(SecureUtils.md5Salt(password, userId.toString()), userDO.getPassword(), "用户名或密码错误");
|
||||
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, userDO.getStatus(), "此账号已被禁用,如有疑问,请联系管理员");
|
||||
DeptDetailVO deptDetailVO = deptService.get(userDO.getDeptId());
|
||||
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, deptDetailVO.getStatus(), "此账号部门已被禁用,如有疑问,请联系管理员");
|
||||
|
||||
// 登录
|
||||
LoginUser loginUser = BeanUtil.copyProperties(userDO, LoginUser.class);
|
||||
loginUser.setDeptName(ExceptionUtils.exToNull(() -> deptService.get(loginUser.getDeptId()).getName()));
|
||||
loginUser.setPermissions(permissionService.listPermissionByUserId(userId));
|
||||
loginUser.setRoles(permissionService.listRoleCodeByUserId(userId));
|
||||
loginUser.setRoleSet(roleService.listByUserId(userId));
|
||||
|
@ -21,8 +21,6 @@ import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
@ -50,7 +48,6 @@ import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
* @since 2023/3/25 22:49
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OnlineUserServiceImpl implements OnlineUserService {
|
||||
|
||||
@Override
|
||||
@ -109,7 +106,7 @@ public class OnlineUserServiceImpl implements OnlineUserService {
|
||||
String nickname = query.getNickname();
|
||||
if (StrUtil.isNotBlank(nickname)) {
|
||||
flag1 = StrUtil.contains(loginUser.getUsername(), nickname)
|
||||
|| StrUtil.contains(loginUser.getNickname(), nickname);
|
||||
|| StrUtil.contains(LoginHelper.getNickname(loginUser.getId()), nickname);
|
||||
}
|
||||
|
||||
boolean flag2 = true;
|
||||
|
@ -42,12 +42,10 @@ import top.charles7c.cnadmin.common.constant.StringConsts;
|
||||
import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.DataTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.service.CommonUserService;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.FileUtils;
|
||||
import top.charles7c.cnadmin.common.util.SecureUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
import top.charles7c.cnadmin.system.mapper.UserMapper;
|
||||
import top.charles7c.cnadmin.system.model.entity.UserDO;
|
||||
@ -151,6 +149,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
|
||||
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportImgTypes), "头像仅支持 {} 格式的图片",
|
||||
String.join(StringConsts.CHINESE_COMMA, avatarSupportImgTypes));
|
||||
|
||||
UserDO userDO = super.getById(id);
|
||||
// 上传新头像
|
||||
String avatarPath = localStorageProperties.getPath().getAvatar();
|
||||
File newAvatarFile = FileUtils.upload(avatarFile, avatarPath, false);
|
||||
@ -162,15 +161,10 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
|
||||
baseMapper.lambdaUpdate().set(UserDO::getAvatar, newAvatar).eq(UserDO::getId, id).update();
|
||||
|
||||
// 删除原头像
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
String oldAvatar = loginUser.getAvatar();
|
||||
if (StrUtil.isNotBlank(loginUser.getAvatar())) {
|
||||
String oldAvatar = userDO.getAvatar();
|
||||
if (StrUtil.isNotBlank(oldAvatar)) {
|
||||
FileUtil.del(avatarPath + oldAvatar);
|
||||
}
|
||||
|
||||
// 更新登录用户信息
|
||||
loginUser.setAvatar(newAvatar);
|
||||
LoginHelper.updateLoginUser(loginUser);
|
||||
return newAvatar;
|
||||
}
|
||||
|
||||
@ -185,11 +179,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
baseMapper.lambdaUpdate().set(UserDO::getPassword, SecureUtils.md5Salt(newPassword, id.toString()))
|
||||
.set(UserDO::getPwdResetTime, now).eq(UserDO::getId, id).update();
|
||||
|
||||
// 更新登录用户信息
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
loginUser.setPwdResetTime(now);
|
||||
LoginHelper.updateLoginUser(loginUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -203,11 +192,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
|
||||
|
||||
// 更新邮箱
|
||||
baseMapper.lambdaUpdate().set(UserDO::getEmail, newEmail).eq(UserDO::getId, id).update();
|
||||
|
||||
// 更新登录用户信息
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
loginUser.setEmail(newEmail);
|
||||
LoginHelper.updateLoginUser(loginUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -45,6 +45,8 @@ import top.charles7c.cnadmin.common.util.RedisUtils;
|
||||
import top.charles7c.cnadmin.common.util.SecureUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
|
||||
import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
|
||||
import top.charles7c.cnadmin.system.service.UserService;
|
||||
|
||||
/**
|
||||
* 登录 API
|
||||
@ -59,6 +61,7 @@ import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
|
||||
public class LoginController {
|
||||
|
||||
private final LoginService loginService;
|
||||
private final UserService userService;
|
||||
|
||||
@SaIgnore
|
||||
@Operation(summary = "用户登录", description = "根据用户名和密码进行登录认证")
|
||||
@ -93,7 +96,10 @@ public class LoginController {
|
||||
@GetMapping("/user/info")
|
||||
public R<UserInfoVO> getUserInfo() {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
UserInfoVO userInfoVO = BeanUtil.copyProperties(loginUser, UserInfoVO.class);
|
||||
UserDetailVO userDetailVO = userService.get(loginUser.getId());
|
||||
UserInfoVO userInfoVO = BeanUtil.copyProperties(userDetailVO, UserInfoVO.class);
|
||||
userInfoVO.setPermissions(loginUser.getPermissions());
|
||||
userInfoVO.setRoles(loginUser.getRoles());
|
||||
return R.ok(userInfoVO);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user