优化用户登入和注册

This commit is contained in:
石皮幼鸟 2024-04-11 19:45:55 +08:00
parent 347801ba4e
commit 035e257852
1 changed files with 79 additions and 22 deletions

View File

@ -34,22 +34,17 @@ public class AuthServiceImpl implements AuthService {
public R<String> login(LoginDto loginDto) { public R<String> login(LoginDto loginDto) {
// 根据用户名查询用户信息 // 根据用户名查询用户信息
var username = loginDto.getUsername(); var username = loginDto.getUsername();
var matchType = getMatchType(username);
// 判断用户名是否为邮箱 var user = switch (matchType) {
var patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX); case CommonConstant.EMAIL ->
var matcherEmail = patternEmail.matcher(username); mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
case CommonConstant.PHONE ->
mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
default ->
mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
};
// 判断用户名是否为手机号
var patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
var matcherPhone = patternPhone.matcher(username);
User user;
if (matcherEmail.matches()) {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
} else if (matcherPhone.matches()) {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
} else {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
}
if (Objects.isNull(user)) { if (Objects.isNull(user)) {
return R.fail("用户不存在"); return R.fail("用户不存在");
} }
@ -84,14 +79,38 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public R<UserVo> register(RegisterDto registerDto) { public R<UserVo> register(RegisterDto registerDto) {
var regedUser = getUserByName(registerDto.getUsername()); // 根据用户名查询用户信息
var username = registerDto.getUsername();
var matchType = getMatchType(username);
// 用户名重复 User user;
if (regedUser != null) { switch (matchType) {
R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc()); case CommonConstant.EMAIL -> {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.EMAIL).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
if (user != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
user = new User();
user.setEmail(username);
}
case CommonConstant.PHONE -> {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.PHONE).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
if (user != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
user = new User();
user.setPhone(username);
}
default -> {
user = mongoTemplate.findOne(new Query(Criteria.where(CommonConstant.USERNAME).is(username).and(CommonConstant.STATUS).ne(0)), User.class);
if (user != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
user = new User();
user.setUsername(username);
}
} }
var user = new User();
user.setUsername(registerDto.getUsername());
user.setPassword(registerDto.getPassword()); user.setPassword(registerDto.getPassword());
user.setCreateTime(System.currentTimeMillis()); user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis()); user.setUpdateTime(System.currentTimeMillis());
@ -103,6 +122,26 @@ public class AuthServiceImpl implements AuthService {
} }
} }
/**
* 获取用户名匹配类型通常是登录和注册时使用
*
* @param username 用户名
* @return 匹配类型
*/
private String getMatchType(String username) {
var patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX);
var matcherEmail = patternEmail.matcher(username);
if (matcherEmail.matches()) {
return CommonConstant.EMAIL;
}
var patternPhone = Pattern.compile(CommonConstant.PHONE_REGEX);
var matcherPhone = patternPhone.matcher(username);
if (matcherPhone.matches()) {
return CommonConstant.PHONE;
}
return CommonConstant.USERNAME;
}
@Override @Override
public User getUserInfo(String id) { public User getUserInfo(String id) {
return mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)), User.class); return mongoTemplate.findOne(new Query(Criteria.where("id").is(id).and(CommonConstant.STATUS).ne(0)), User.class);
@ -131,14 +170,32 @@ public class AuthServiceImpl implements AuthService {
verify.setUsername(user.getUsername()); verify.setUsername(user.getUsername());
} }
// 如果邮箱修改了检查是否有重复有重复则返回null否则修改
if (!verify.getEmail().equals(user.getEmail())) {
var regedUser = mongoTemplate.findOne(new Query(Criteria.where("email").is(user.getEmail()).and(CommonConstant.STATUS).ne(0)), User.class);
if (regedUser != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
verify.setEmail(user.getEmail());
}
// 如果手机号修改了检查是否有重复有重复则返回null否则修改
if (!verify.getPhone().equals(user.getPhone())) {
var regedUser = mongoTemplate.findOne(new Query(Criteria.where("phone").is(user.getPhone()).and(CommonConstant.STATUS).ne(0)), User.class);
if (regedUser != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
verify.setPhone(user.getPhone());
}
// 如果密码为空意味着没修改密码否则修改密码 // 如果密码为空意味着没修改密码否则修改密码
if (user.getPassword() != null && !user.getPassword().isEmpty()) { if (user.getPassword() != null && !user.getPassword().isEmpty()) {
verify.setPassword(user.getPassword()); verify.setPassword(user.getPassword());
} }
verify.setAvatar(user.getAvatar()); verify.setAvatar(user.getAvatar());
verify.setEmail(user.getEmail());
verify.setPhone(user.getPhone());
verify.setSex(user.getSex()); verify.setSex(user.getSex());
verify.setIntroduction(user.getIntroduction()); verify.setIntroduction(user.getIntroduction());
verify.setUpdateTime(System.currentTimeMillis()); verify.setUpdateTime(System.currentTimeMillis());