2024-11-05 17:10:26 +08:00
|
|
|
package com.yunan.service.impl;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.yunan.dto.RegisterDTO;
|
2024-11-09 16:49:13 +08:00
|
|
|
import com.yunan.dto.Users;
|
2024-11-05 17:10:26 +08:00
|
|
|
import com.yunan.mapper.AuthMapper;
|
2024-11-06 18:47:00 +08:00
|
|
|
import com.yunan.response.ErrorResponse;
|
2024-11-05 17:10:26 +08:00
|
|
|
import com.yunan.service.AuthService;
|
2024-11-09 16:49:13 +08:00
|
|
|
import com.yunan.util.RedisUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-11-06 18:47:00 +08:00
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2024-11-05 17:10:26 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
2024-11-06 18:47:00 +08:00
|
|
|
|
2024-11-05 17:10:26 +08:00
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class AuthServiceImpl implements AuthService {
|
|
|
|
@Resource
|
|
|
|
private AuthMapper authMapper;
|
2024-11-09 16:49:13 +08:00
|
|
|
@Autowired
|
|
|
|
RedisUtils redisUtils;
|
2024-11-05 17:10:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2024-11-09 16:49:13 +08:00
|
|
|
public ResponseEntity<Object> register(RegisterDTO registerDTO) {
|
|
|
|
//获取正确的验证码
|
|
|
|
Object rightCode = redisUtils.hGet("rightCode", registerDTO.getEmail());
|
2024-11-06 18:47:00 +08:00
|
|
|
//处理registerDTO对数据为空的逻辑
|
|
|
|
// 验证验证码的正确性
|
2024-11-09 16:49:13 +08:00
|
|
|
if(registerDTO.getCode().equals(rightCode)){
|
|
|
|
LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
queryWrapper.eq(Users::getEmail, registerDTO.getEmail());
|
2024-11-06 18:47:00 +08:00
|
|
|
if(authMapper.selectOne(queryWrapper) != null) {
|
|
|
|
return ResponseEntity
|
|
|
|
.status(HttpStatus.CONFLICT)
|
|
|
|
.body(new ErrorResponse("用户名已存在!!!"));
|
2024-11-05 17:10:26 +08:00
|
|
|
}
|
2024-11-09 16:49:13 +08:00
|
|
|
Users user = new Users();
|
2024-11-06 18:47:00 +08:00
|
|
|
user.setUsername(registerDTO.getUsername());
|
|
|
|
user.setPassword(registerDTO.getPassword());
|
2024-11-09 16:49:13 +08:00
|
|
|
user.setEmail(registerDTO.getEmail());
|
|
|
|
// boolean isSaved = authMapper.insert(user) > 0;
|
|
|
|
boolean isSaved = true;
|
2024-11-06 18:47:00 +08:00
|
|
|
if (isSaved) {
|
|
|
|
return ResponseEntity
|
|
|
|
.status(HttpStatus.OK)
|
|
|
|
.body("注册成功!");
|
|
|
|
} else {
|
|
|
|
return ResponseEntity
|
|
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
.body("数据库存储失败,请稍后重试");
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
// 如果验证码为空或者与正确的验证码不匹配,返回错误信息
|
|
|
|
return ResponseEntity
|
|
|
|
.status(HttpStatus.BAD_REQUEST) // 设置 HTTP 状态码为 400
|
|
|
|
.body(new ErrorResponse("验证码不正确或已过期")); // 返回自定义的错误消息
|
2024-11-05 17:10:26 +08:00
|
|
|
}
|
2024-11-06 18:47:00 +08:00
|
|
|
|
2024-11-05 17:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|