Skip to content

Commit

Permalink
[代码修复](v2.6):修复手机号可以重复的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jiefei30 authored Nov 8, 2020
1 parent db6d523 commit c7e95d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificat
*/
User findByEmail(String email);

/**
* 根据手机号查询
* @param phone 手机号
* @return /
*/
User findByPhone(String phone);

/**
* 修改密码
* @param username 用户名
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public void create(User resources) {
if (userRepository.findByEmail(resources.getEmail()) != null) {
throw new EntityExistException(User.class, "email", resources.getEmail());
}
if (userRepository.findByPhone(resources.getPhone()) != null) {
throw new EntityExistException(User.class, "phone", resources.getPhone());
}
userRepository.save(resources);
}

Expand All @@ -101,14 +104,16 @@ public void update(User resources) {
ValidationUtil.isNull(user.getId(), "User", "id", resources.getId());
User user1 = userRepository.findByUsername(resources.getUsername());
User user2 = userRepository.findByEmail(resources.getEmail());

User user3 = userRepository.findByPhone(resources.getPhone());
if (user1 != null && !user.getId().equals(user1.getId())) {
throw new EntityExistException(User.class, "username", resources.getUsername());
}

if (user2 != null && !user.getId().equals(user2.getId())) {
throw new EntityExistException(User.class, "email", resources.getEmail());
}
if (user3 != null && !user.getId().equals(user3.getId())) {
throw new EntityExistException(User.class, "phone", resources.getPhone());
}
// 如果用户的角色改变
if (!resources.getRoles().equals(user.getRoles())) {
redisUtils.del(CacheKey.DATE_USER + resources.getId());
Expand Down Expand Up @@ -141,6 +146,10 @@ public void update(User resources) {
@Transactional(rollbackFor = Exception.class)
public void updateCenter(User resources) {
User user = userRepository.findById(resources.getId()).orElseGet(User::new);
User user1 = userRepository.findByPhone(resources.getPhone());
if (user1 != null && !user.getId().equals(user1.getId())) {
throw new EntityExistException(User.class, "phone", resources.getPhone());
}
user.setNickName(resources.getNickName());
user.setPhone(resources.getPhone());
user.setGender(resources.getGender());
Expand Down
2 changes: 1 addition & 1 deletion sql/eladmin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ CREATE TABLE `sys_user` (
-- ----------------------------
BEGIN;
INSERT INTO `sys_user` VALUES (1, 2, 'admin', '管理员', '', '18888888888', '[email protected]', 'avatar-20200806032259161.png', '/Users/jie/Documents/work/me/admin/eladmin/~/avatar/avatar-20200806032259161.png', '$2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa', b'1', 1, NULL, 'admin', '2020-05-03 16:38:31', '2018-08-23 09:11:56', '2020-09-05 10:43:31');
INSERT INTO `sys_user` VALUES (2, 2, 'test', '测试', '', '18888888888', '[email protected]', NULL, NULL, '$2a$10$4XcyudOYTSz6fue6KFNMHeUQnCX5jbBQypLEnGk1PmekXt5c95JcK', b'0', 1, 'admin', 'admin', NULL, '2020-05-05 11:15:49', '2020-09-05 10:43:38');
INSERT INTO `sys_user` VALUES (2, 2, 'test', '测试', '', '19999999999', '[email protected]', NULL, NULL, '$2a$10$4XcyudOYTSz6fue6KFNMHeUQnCX5jbBQypLEnGk1PmekXt5c95JcK', b'0', 1, 'admin', 'admin', NULL, '2020-05-05 11:15:49', '2020-09-05 10:43:38');
COMMIT;

-- ----------------------------
Expand Down

0 comments on commit c7e95d9

Please sign in to comment.