From 854b405d893be3bcee20f3b9f44c83102eacb9f8 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 18 Sep 2022 11:26:03 +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 --- .../web/controller/system/SysUserController.java | 8 ++++++-- .../framework/web/service/SysRegisterService.java | 13 ++++++------- .../java/com/ruoyi/system/mapper/SysUserMapper.java | 2 +- .../com/ruoyi/system/service/ISysUserService.java | 4 ++-- .../system/service/impl/SysUserServiceImpl.java | 9 +++++---- .../main/resources/mapper/system/SysUserMapper.xml | 4 ++-- 6 files changed, 22 insertions(+), 18 deletions(-) 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 e9030e0..e4bcf78 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 @@ -125,7 +125,7 @@ public class SysUserController extends BaseController @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 class SysUserController extends BaseController { 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/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java index 061af23..5e290b4 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/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 @@ public class SysRegisterService { 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 @@ public class SysRegisterService } 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/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java index 28713f6..c86acea 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java @@ -107,7 +107,7 @@ public interface SysUserMapper * @param userName 用户名称 * @return 结果 */ - public int checkUserNameUnique(String userName); + public SysUser checkUserNameUnique(String userName); /** * 校验手机号码是否唯一 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 29193ce..54ed035 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 @@ -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/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 3e1680f..017a358 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 @@ -161,14 +161,15 @@ public class SysUserServiceImpl implements ISysUserService /** * 校验用户名称是否唯一 * - * @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/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml index 9f1839c..3c1c6c5 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/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