优化用户登入和注册
This commit is contained in:
parent
347801ba4e
commit
035e257852
|
@ -34,22 +34,17 @@ public class AuthServiceImpl implements AuthService {
|
|||
public R<String> login(LoginDto loginDto) {
|
||||
// 根据用户名查询用户信息
|
||||
var username = loginDto.getUsername();
|
||||
var matchType = getMatchType(username);
|
||||
|
||||
// 判断用户名是否为邮箱
|
||||
var patternEmail = Pattern.compile(CommonConstant.EMAIL_REGEX);
|
||||
var matcherEmail = patternEmail.matcher(username);
|
||||
var user = switch (matchType) {
|
||||
case CommonConstant.EMAIL ->
|
||||
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)) {
|
||||
return R.fail("用户不存在");
|
||||
}
|
||||
|
@ -84,14 +79,38 @@ public class AuthServiceImpl implements AuthService {
|
|||
|
||||
@Override
|
||||
public R<UserVo> register(RegisterDto registerDto) {
|
||||
var regedUser = getUserByName(registerDto.getUsername());
|
||||
// 根据用户名查询用户信息
|
||||
var username = registerDto.getUsername();
|
||||
var matchType = getMatchType(username);
|
||||
|
||||
// 用户名重复
|
||||
if (regedUser != null) {
|
||||
R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
|
||||
User user;
|
||||
switch (matchType) {
|
||||
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());
|
||||
}
|
||||
var user = new User();
|
||||
user.setUsername(registerDto.getUsername());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
user.setPassword(registerDto.getPassword());
|
||||
user.setCreateTime(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
|
||||
public User getUserInfo(String id) {
|
||||
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());
|
||||
}
|
||||
|
||||
// 如果邮箱修改了,检查是否有重复,有重复则返回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()) {
|
||||
verify.setPassword(user.getPassword());
|
||||
}
|
||||
|
||||
verify.setAvatar(user.getAvatar());
|
||||
verify.setEmail(user.getEmail());
|
||||
verify.setPhone(user.getPhone());
|
||||
verify.setSex(user.getSex());
|
||||
verify.setIntroduction(user.getIntroduction());
|
||||
verify.setUpdateTime(System.currentTimeMillis());
|
||||
|
|
Loading…
Reference in New Issue