添加了注册,登录接口

--LoginController.java
修改登录请求dto,使得符合Apifox定义接口请求属性
--LoginDTO.java
This commit is contained in:
剑雨 2025-03-10 17:53:38 +08:00
parent 61809aced1
commit 0d0a8fa5ef
2 changed files with 47 additions and 19 deletions

View file

@ -2,41 +2,37 @@ package xiaozhi.modules.security.controller;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.redis.RedisUtils; import xiaozhi.common.exception.RenException;
import xiaozhi.common.utils.PropertiesUtils;
import xiaozhi.common.utils.Result; import xiaozhi.common.utils.Result;
import xiaozhi.common.validator.AssertUtils; import xiaozhi.common.validator.AssertUtils;
import xiaozhi.modules.security.dto.LoginDTO; import xiaozhi.modules.security.dto.LoginDTO;
import xiaozhi.modules.security.password.PasswordUtils;
import xiaozhi.modules.security.service.CaptchaService; import xiaozhi.modules.security.service.CaptchaService;
import xiaozhi.modules.security.service.SysUserTokenService; import xiaozhi.modules.security.service.SysUserTokenService;
import xiaozhi.modules.sys.service.SysParamsService; import xiaozhi.modules.sys.dto.SysUserDTO;
import xiaozhi.modules.sys.service.SysUserService; import xiaozhi.modules.sys.service.SysUserService;
import java.io.IOException; import java.io.IOException;
/** /**
* 登录 * 登录控制层
*/ */
@Tag(name = "登录管理")
@AllArgsConstructor @AllArgsConstructor
@RestController @RestController
@RequestMapping("/user")
@Tag(name = "登录管理") @Tag(name = "登录管理")
public class LoginController { public class LoginController {
private final SysUserService sysUserService; private final SysUserService sysUserService;
private final SysUserTokenService sysUserTokenService; private final SysUserTokenService sysUserTokenService;
private final CaptchaService captchaService; private final CaptchaService captchaService;
private final RedisUtils redisUtils;
private final SysParamsService sysParamsService;
private final PropertiesUtils propertiesUtils;
@GetMapping("captcha")
@GetMapping("/captcha")
@Operation(summary = "验证码") @Operation(summary = "验证码")
public void captcha(HttpServletResponse response, String uuid) throws IOException { public void captcha(HttpServletResponse response, String uuid) throws IOException {
//uuid不能为空 //uuid不能为空
@ -46,10 +42,42 @@ public class LoginController {
captchaService.create(response, uuid); captchaService.create(response, uuid);
} }
@PostMapping("login") @PostMapping("/login")
@Operation(summary = "登录") @Operation(summary = "登录")
public Result login(HttpServletRequest request, @RequestBody LoginDTO login) { public Result login( @RequestBody LoginDTO login) {
return sysUserTokenService.createToken(1L); // 验证是否正确输入验证码
boolean validate = captchaService.validate(login.getCaptchaId(), login.getCaptcha());
if (!validate) {
throw new RenException("验证码错误,请重新获取");
}
// 按照用户名获取用户
SysUserDTO userDTO = sysUserService.getByUsername(login.getMobile());
// 判断用户是否存在
if (userDTO == null) {
throw new RenException("请检测用户和密码是否输入错误");
}
// 判断密码是否正确,不一样则进入if
if (!PasswordUtils.matches(login.getPassword(), userDTO.getPassword())) {
throw new RenException("请检测用户和密码是否输入错误");
}
return sysUserTokenService.createToken(userDTO.getId());
}
@PostMapping("/register")
@Operation(summary = "注册")
public Result<Void> register(@RequestBody LoginDTO login) {
// 验证是否正确输入验证码
captchaService.validate(login.getCaptchaId(), login.getCaptcha());
// 按照用户名获取用户
SysUserDTO userDTO = sysUserService.getByUsername(login.getMobile());
if (userDTO != null){
throw new RenException("此手机号码已经注册过");
}
userDTO = new SysUserDTO();
userDTO.setUsername(login.getMobile());
userDTO.setPassword(login.getPassword());
sysUserService.save(userDTO);
return new Result<Void>();
} }
} }

View file

@ -13,9 +13,9 @@ import java.io.Serializable;
@Schema(description = "登录表单") @Schema(description = "登录表单")
public class LoginDTO implements Serializable { public class LoginDTO implements Serializable {
@Schema(description = "用户名", required = true) @Schema(description = "手机号码")
@NotBlank(message = "{sysuser.username.require}") @NotBlank(message = "{sysuser.username.require}")
private String username; private String mobile;
@Schema(description = "密码") @Schema(description = "密码")
@NotBlank(message = "{sysuser.password.require}") @NotBlank(message = "{sysuser.password.require}")
@ -27,6 +27,6 @@ public class LoginDTO implements Serializable {
@Schema(description = "唯一标识") @Schema(description = "唯一标识")
@NotBlank(message = "{sysuser.uuid.require}") @NotBlank(message = "{sysuser.uuid.require}")
private String uuid; private String captchaId;
} }