diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java index e003e65d..7e358d51 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java @@ -136,7 +136,7 @@ public class SysUserController extends BaseController { @Log(title = "用户管理", businessType = BusinessType.INSERT) @PostMapping public R add(@Validated @RequestBody SysUser user) { - if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) { + if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) { return R.fail("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { @@ -158,7 +158,7 @@ public class SysUserController extends BaseController { public R edit(@Validated @RequestBody SysUser user) { userService.checkUserAllowed(user); userService.checkUserDataScope(user.getUserId()); - if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) { + if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) { return R.fail("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java index 97e73e05..042efc2c 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java @@ -86,7 +86,7 @@ public interface ISysUserService { * @param user 用户信息 * @return 结果 */ - String checkUserNameUnique(String userName); + String checkUserNameUnique(SysUser user); /** * 校验手机号码是否唯一 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java index 6d861e09..983173d1 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java @@ -48,15 +48,15 @@ public class SysRegisterService { if (captchaEnabled) { validateCaptcha(username, registerBody.getCode(), registerBody.getUuid(), request); } - - if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) { - throw new UserException("user.register.save.error", username); - } SysUser sysUser = new SysUser(); sysUser.setUserName(username); sysUser.setNickName(username); sysUser.setPassword(BCrypt.hashpw(password)); sysUser.setUserType(userType); + + if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysUser))) { + throw new UserException("user.register.save.error", username); + } boolean regFlag = userService.registerUser(sysUser); if (!regFlag) { throw new UserException("user.register.error"); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 39ad7273..4e63f17e 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -193,12 +193,14 @@ public class SysUserServiceImpl implements ISysUserService { /** * 校验用户名称是否唯一 * - * @param userName 用户名称 + * @param user 用户信息 * @return 结果 */ @Override - public String checkUserNameUnique(String userName) { - boolean exist = baseMapper.exists(new LambdaQueryWrapper().eq(SysUser::getUserName, userName)); + public String checkUserNameUnique(SysUser user) { + boolean exist = baseMapper.exists(new LambdaQueryWrapper() + .eq(SysUser::getUserName, user.getUserName()) + .ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId())); if (exist) { return UserConstants.NOT_UNIQUE; }