From bc8849c1d80f5a8c5b8d20425aee0538350623ae Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 18 Sep 2022 11:26:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=B4=A6=E5=8F=B7=E9=87=8D=E5=A4=8D=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/service/SysRegisterService.java | 13 ++++++------- .../system/controller/SysUserController.java | 8 ++++++-- .../ruoyi/project/system/mapper/SysUserMapper.java | 2 +- .../project/system/service/ISysUserService.java | 4 ++-- .../system/service/impl/SysUserServiceImpl.java | 9 +++++---- src/main/resources/mybatis/system/SysUserMapper.xml | 4 ++-- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/ruoyi/framework/security/service/SysRegisterService.java b/src/main/java/com/ruoyi/framework/security/service/SysRegisterService.java index 7c02bf1a..b6fba698 100644 --- a/src/main/java/com/ruoyi/framework/security/service/SysRegisterService.java +++ b/src/main/java/com/ruoyi/framework/security/service/SysRegisterService.java @@ -41,9 +41,11 @@ public class SysRegisterService public String register(RegisterBody registerBody) { String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword(); + SysUser sysUser = new SysUser(); + sysUser.setUserName(username); - boolean captchaEnabled = configService.selectCaptchaEnabled(); // 验证码开关 + boolean captchaEnabled = configService.selectCaptchaEnabled(); if (captchaEnabled) { validateCaptcha(username, registerBody.getCode(), registerBody.getUuid()); @@ -67,16 +69,14 @@ else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH { msg = "密码长度必须在5到20个字符之间"; } - else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) + else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysUser))) { msg = "保存用户'" + username + "'失败,注册账号已存在"; } else { - SysUser sysUser = new SysUser(); - sysUser.setUserName(username); sysUser.setNickName(username); - sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword())); + sysUser.setPassword(SecurityUtils.encryptPassword(password)); boolean regFlag = userService.registerUser(sysUser); if (!regFlag) { @@ -84,8 +84,7 @@ else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(usernam } else { - AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, - MessageUtils.message("user.register.success"))); + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"))); } } return msg; diff --git a/src/main/java/com/ruoyi/project/system/controller/SysUserController.java b/src/main/java/com/ruoyi/project/system/controller/SysUserController.java index e21fadf7..ad16fee0 100644 --- a/src/main/java/com/ruoyi/project/system/controller/SysUserController.java +++ b/src/main/java/com/ruoyi/project/system/controller/SysUserController.java @@ -125,7 +125,7 @@ public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long @PostMapping public AjaxResult add(@Validated @RequestBody SysUser user) { - if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) + if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) { return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); } @@ -154,7 +154,11 @@ public AjaxResult edit(@Validated @RequestBody SysUser user) { userService.checkUserAllowed(user); userService.checkUserDataScope(user.getUserId()); - if (StringUtils.isNotEmpty(user.getPhonenumber()) + if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) + { + return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); + } + else if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); diff --git a/src/main/java/com/ruoyi/project/system/mapper/SysUserMapper.java b/src/main/java/com/ruoyi/project/system/mapper/SysUserMapper.java index a0efb902..f08882a5 100644 --- a/src/main/java/com/ruoyi/project/system/mapper/SysUserMapper.java +++ b/src/main/java/com/ruoyi/project/system/mapper/SysUserMapper.java @@ -109,7 +109,7 @@ public interface SysUserMapper * @param userName 用户名称 * @return 结果 */ - public int checkUserNameUnique(String userName); + public SysUser checkUserNameUnique(String userName); /** * 校验手机号码是否唯一 diff --git a/src/main/java/com/ruoyi/project/system/service/ISysUserService.java b/src/main/java/com/ruoyi/project/system/service/ISysUserService.java index 43be9a60..3985e31d 100644 --- a/src/main/java/com/ruoyi/project/system/service/ISysUserService.java +++ b/src/main/java/com/ruoyi/project/system/service/ISysUserService.java @@ -69,10 +69,10 @@ public interface ISysUserService /** * 校验用户名称是否唯一 * - * @param userName 用户名称 + * @param user 用户信息 * @return 结果 */ - public String checkUserNameUnique(String userName); + public String checkUserNameUnique(SysUser user); /** * 校验手机号码是否唯一 diff --git a/src/main/java/com/ruoyi/project/system/service/impl/SysUserServiceImpl.java b/src/main/java/com/ruoyi/project/system/service/impl/SysUserServiceImpl.java index 6d5d51a7..1c0ce9c6 100644 --- a/src/main/java/com/ruoyi/project/system/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/ruoyi/project/system/service/impl/SysUserServiceImpl.java @@ -161,14 +161,15 @@ public String selectUserPostGroup(String userName) /** * 校验用户名称是否唯一 * - * @param userName 用户名称 + * @param user 用户信息 * @return 结果 */ @Override - public String checkUserNameUnique(String userName) + public String checkUserNameUnique(SysUser user) { - int count = userMapper.checkUserNameUnique(userName); - if (count > 0) + Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId(); + SysUser info = userMapper.checkUserNameUnique(user.getUserName()); + if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) { return UserConstants.NOT_UNIQUE; } diff --git a/src/main/resources/mybatis/system/SysUserMapper.xml b/src/main/resources/mybatis/system/SysUserMapper.xml index 7609982e..c0c9cfc5 100644 --- a/src/main/resources/mybatis/system/SysUserMapper.xml +++ b/src/main/resources/mybatis/system/SysUserMapper.xml @@ -130,8 +130,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where u.user_id = #{userId} - + select user_id, user_name from sys_user where user_name = #{userName} and del_flag = '0' limit 1