From 8eae865aab58fb38fbe88c9510c26cfac718d37a Mon Sep 17 00:00:00 2001 From: yulinling <2712495353@qq.com> Date: Mon, 30 Jun 2025 23:07:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=84=E8=8C=83auth/register?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yulinling/workflow/constant/Const.java | 1 + .../yulinling/workflow/constant/Status.java | 65 ++++++------------- .../workflow/controller/AuthController.java | 3 +- .../workflow/model/vo/RegisterVO.java | 18 +++++ .../workflow/service/AuthService.java | 3 +- .../service/impl/AuthServiceImpl.java | 15 +++-- 6 files changed, 52 insertions(+), 53 deletions(-) create mode 100644 src/main/java/asia/yulinling/workflow/model/vo/RegisterVO.java diff --git a/src/main/java/asia/yulinling/workflow/constant/Const.java b/src/main/java/asia/yulinling/workflow/constant/Const.java index e0ddd86..7aaaf5b 100644 --- a/src/main/java/asia/yulinling/workflow/constant/Const.java +++ b/src/main/java/asia/yulinling/workflow/constant/Const.java @@ -13,6 +13,7 @@ public interface Const { * 启用 */ Integer ENABLE = 1; + /** * 禁用 */ diff --git a/src/main/java/asia/yulinling/workflow/constant/Status.java b/src/main/java/asia/yulinling/workflow/constant/Status.java index d0578e1..e77da16 100644 --- a/src/main/java/asia/yulinling/workflow/constant/Status.java +++ b/src/main/java/asia/yulinling/workflow/constant/Status.java @@ -12,83 +12,56 @@ import lombok.Getter; */ @Getter public enum Status { - /** - * 操作成功! - */ + /** 操作成功! */ SUCCESS(200, "操作成功!"), /** 未知异常 */ UNKNOWN_ERROR(500, "未知异常"), - /** - * 退出成功! - */ + /** 退出成功! */ LOGOUT(200, "退出成功!"), - /** - * 请先登录! - */ + /** 请先登录! */ UNAUTHORIZED(401, "请先登录!"), - /** - * 暂无权限访问! - */ + /** 暂无权限访问! */ ACCESS_DENIED(403, "权限不足!"), - /** - * 请求不存在! - */ + /** 请求不存在! */ REQUEST_NOT_FOUND(404, "请求不存在!"), - /** - * 请求方式不支持! - */ + /** 请求方式不支持! */ HTTP_BAD_METHOD(405, "请求方式不支持!"), - /** - * 请求异常! - */ + /** 请求异常! */ BAD_REQUEST(400, "请求异常!"), - /** - * 参数不匹配! - */ + /** 参数不匹配! */ PARAM_NOT_MATCH(400, "参数不匹配!"), - /** - * 参数不能为空! - */ + /** 参数不能为空! */ PARAM_NOT_NULL(400, "参数不能为空!"), - /** - * 当前用户已被锁定,请联系管理员解锁! - */ + /** 当前用户已被锁定,请联系管理员解锁! */ USER_DISABLED(403, "当前用户已被锁定,请联系管理员解锁!"), - /** - * 用户名或密码错误! - */ + /** 用户名或密码错误! */ USERNAME_PASSWORD_ERROR(5001, "用户名或密码错误!"), - /** - * token 已过期,请重新登录! - */ + /** token 已过期,请重新登录! */ TOKEN_EXPIRED(5002, "token 已过期,请重新登录!"), - /** - * token 解析失败,请尝试重新登录! - */ + /** token 解析失败,请尝试重新登录! */ TOKEN_PARSE_ERROR(5002, "token 解析失败,请尝试重新登录!"), - /** - * 当前用户已在别处登录,请尝试更改密码或重新登录! - */ + /** 当前用户已在别处登录,请尝试更改密码或重新登录! */ TOKEN_OUT_OF_CTRL(5003, "当前用户已在别处登录,请尝试更改密码或重新登录!"), - /** - * 无法手动踢出自己,请尝试退出登录操作! - */ - KICKOUT_SELF(5004, "无法手动踢出自己,请尝试退出登录操作!"); + /** 无法手动踢出自己,请尝试退出登录操作! */ + KICKOUT_SELF(5004, "无法手动踢出自己,请尝试退出登录操作!"), + + /** 注册成功! */ + REGISTER_SUCCESS(200, "注册成功"); /** 状态码 */ private final Integer code; diff --git a/src/main/java/asia/yulinling/workflow/controller/AuthController.java b/src/main/java/asia/yulinling/workflow/controller/AuthController.java index 8ef2c43..158d12b 100644 --- a/src/main/java/asia/yulinling/workflow/controller/AuthController.java +++ b/src/main/java/asia/yulinling/workflow/controller/AuthController.java @@ -4,6 +4,7 @@ import asia.yulinling.workflow.dto.request.LoginRequest; import asia.yulinling.workflow.dto.request.RegisterRequest; import asia.yulinling.workflow.dto.response.JWTAuthResponse; import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.vo.RegisterVO; import asia.yulinling.workflow.service.AuthService; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; @@ -40,7 +41,7 @@ public class AuthController { } @PostMapping("/register") - public ApiResponse register(@RequestBody RegisterRequest registerRequest) throws Exception { + public ApiResponse register(@RequestBody RegisterRequest registerRequest) throws Exception { return authService.register(registerRequest); } } diff --git a/src/main/java/asia/yulinling/workflow/model/vo/RegisterVO.java b/src/main/java/asia/yulinling/workflow/model/vo/RegisterVO.java new file mode 100644 index 0000000..b224c3a --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/model/vo/RegisterVO.java @@ -0,0 +1,18 @@ +package asia.yulinling.workflow.model.vo; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +/** + *

+ * 用户注册信息VO + *

+ * + * @author YLL + * @since 2025/6/30 + */ +@Data +@RequiredArgsConstructor +public class RegisterVO { + private Long userId; +} diff --git a/src/main/java/asia/yulinling/workflow/service/AuthService.java b/src/main/java/asia/yulinling/workflow/service/AuthService.java index 8066b6b..8901d19 100644 --- a/src/main/java/asia/yulinling/workflow/service/AuthService.java +++ b/src/main/java/asia/yulinling/workflow/service/AuthService.java @@ -3,6 +3,7 @@ package asia.yulinling.workflow.service; import asia.yulinling.workflow.dto.request.LoginRequest; import asia.yulinling.workflow.dto.request.RegisterRequest; import asia.yulinling.workflow.model.ApiResponse; +import asia.yulinling.workflow.model.vo.RegisterVO; import jakarta.servlet.http.HttpServletRequest; /** @@ -36,5 +37,5 @@ public interface AuthService { * @param request 注册请求 * @return 请求结果 */ - ApiResponse register(RegisterRequest request) throws Exception; + ApiResponse register(RegisterRequest request) throws Exception; } diff --git a/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java b/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java index d57b37d..a780512 100644 --- a/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java +++ b/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java @@ -6,6 +6,7 @@ import asia.yulinling.workflow.dto.request.RegisterRequest; import asia.yulinling.workflow.mapper.UserMapper; import asia.yulinling.workflow.model.ApiResponse; import asia.yulinling.workflow.model.entity.User; +import asia.yulinling.workflow.model.vo.RegisterVO; import asia.yulinling.workflow.service.AuthService; import asia.yulinling.workflow.utils.JwtUtil; import cn.hutool.core.util.StrUtil; @@ -19,6 +20,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; @@ -80,7 +82,8 @@ public class AuthServiceImpl implements AuthService { * @return 请求结果 */ @Override - public ApiResponse register(RegisterRequest request) throws Exception { + @Transactional(rollbackFor = Exception.class) + public ApiResponse register(RegisterRequest request) throws Exception { if (!StrUtil.equals(request.getConfirmPassword(), request.getPassword())) { log.info("password {}, confirmPassword {}", request.getConfirmPassword(), request.getPassword()); @@ -104,15 +107,17 @@ public class AuthServiceImpl implements AuthService { user.setBirthday(request.getBirthday()); user.setStatus(1); user.setUpdateTime(new Date()); - log.info("User: {}", user); + try { userMapper.insert(user); + log.info("insert user after: {}", user); + RegisterVO registerVO = new RegisterVO(); + registerVO.setUserId(user.getId()); + return ApiResponse.ofStatus(Status.REGISTER_SUCCESS, registerVO); } catch (Exception e) { - log.error("insertUser Error: {}", e.getMessage()); + log.error("数据插入用户数据失败: {}", e.getMessage()); throw new RuntimeException(e); } - - return ApiResponse.of(200, "注册成功", null); } }