feat: 规范auth/register接口

This commit is contained in:
yulinling 2025-06-30 23:07:13 +08:00
parent ee21f94ba7
commit 8eae865aab
6 changed files with 52 additions and 53 deletions

View File

@ -13,6 +13,7 @@ public interface Const {
* 启用 * 启用
*/ */
Integer ENABLE = 1; Integer ENABLE = 1;
/** /**
* 禁用 * 禁用
*/ */

View File

@ -12,83 +12,56 @@ import lombok.Getter;
*/ */
@Getter @Getter
public enum Status { public enum Status {
/** /** 操作成功! */
* 操作成功
*/
SUCCESS(200, "操作成功!"), SUCCESS(200, "操作成功!"),
/** 未知异常 */ /** 未知异常 */
UNKNOWN_ERROR(500, "未知异常"), UNKNOWN_ERROR(500, "未知异常"),
/** /** 退出成功! */
* 退出成功
*/
LOGOUT(200, "退出成功!"), LOGOUT(200, "退出成功!"),
/** /** 请先登录! */
* 请先登录
*/
UNAUTHORIZED(401, "请先登录!"), UNAUTHORIZED(401, "请先登录!"),
/** /** 暂无权限访问! */
* 暂无权限访问
*/
ACCESS_DENIED(403, "权限不足!"), ACCESS_DENIED(403, "权限不足!"),
/** /** 请求不存在! */
* 请求不存在
*/
REQUEST_NOT_FOUND(404, "请求不存在!"), REQUEST_NOT_FOUND(404, "请求不存在!"),
/** /** 请求方式不支持! */
* 请求方式不支持
*/
HTTP_BAD_METHOD(405, "请求方式不支持!"), HTTP_BAD_METHOD(405, "请求方式不支持!"),
/** /** 请求异常! */
* 请求异常
*/
BAD_REQUEST(400, "请求异常!"), BAD_REQUEST(400, "请求异常!"),
/** /** 参数不匹配! */
* 参数不匹配
*/
PARAM_NOT_MATCH(400, "参数不匹配!"), PARAM_NOT_MATCH(400, "参数不匹配!"),
/** /** 参数不能为空! */
* 参数不能为空
*/
PARAM_NOT_NULL(400, "参数不能为空!"), PARAM_NOT_NULL(400, "参数不能为空!"),
/** /** 当前用户已被锁定,请联系管理员解锁! */
* 当前用户已被锁定请联系管理员解锁
*/
USER_DISABLED(403, "当前用户已被锁定,请联系管理员解锁!"), USER_DISABLED(403, "当前用户已被锁定,请联系管理员解锁!"),
/** /** 用户名或密码错误! */
* 用户名或密码错误
*/
USERNAME_PASSWORD_ERROR(5001, "用户名或密码错误!"), USERNAME_PASSWORD_ERROR(5001, "用户名或密码错误!"),
/** /** token 已过期,请重新登录! */
* token 已过期请重新登录
*/
TOKEN_EXPIRED(5002, "token 已过期,请重新登录!"), TOKEN_EXPIRED(5002, "token 已过期,请重新登录!"),
/** /** token 解析失败,请尝试重新登录! */
* token 解析失败请尝试重新登录
*/
TOKEN_PARSE_ERROR(5002, "token 解析失败,请尝试重新登录!"), TOKEN_PARSE_ERROR(5002, "token 解析失败,请尝试重新登录!"),
/** /** 当前用户已在别处登录,请尝试更改密码或重新登录! */
* 当前用户已在别处登录请尝试更改密码或重新登录
*/
TOKEN_OUT_OF_CTRL(5003, "当前用户已在别处登录,请尝试更改密码或重新登录!"), TOKEN_OUT_OF_CTRL(5003, "当前用户已在别处登录,请尝试更改密码或重新登录!"),
/** /** 无法手动踢出自己,请尝试退出登录操作! */
* 无法手动踢出自己请尝试退出登录操作 KICKOUT_SELF(5004, "无法手动踢出自己,请尝试退出登录操作!"),
*/
KICKOUT_SELF(5004, "无法手动踢出自己,请尝试退出登录操作!"); /** 注册成功! */
REGISTER_SUCCESS(200, "注册成功");
/** 状态码 */ /** 状态码 */
private final Integer code; private final Integer code;

View File

@ -4,6 +4,7 @@ import asia.yulinling.workflow.dto.request.LoginRequest;
import asia.yulinling.workflow.dto.request.RegisterRequest; import asia.yulinling.workflow.dto.request.RegisterRequest;
import asia.yulinling.workflow.dto.response.JWTAuthResponse; import asia.yulinling.workflow.dto.response.JWTAuthResponse;
import asia.yulinling.workflow.model.ApiResponse; import asia.yulinling.workflow.model.ApiResponse;
import asia.yulinling.workflow.model.vo.RegisterVO;
import asia.yulinling.workflow.service.AuthService; import asia.yulinling.workflow.service.AuthService;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -40,7 +41,7 @@ public class AuthController {
} }
@PostMapping("/register") @PostMapping("/register")
public ApiResponse<?> register(@RequestBody RegisterRequest registerRequest) throws Exception { public ApiResponse<RegisterVO> register(@RequestBody RegisterRequest registerRequest) throws Exception {
return authService.register(registerRequest); return authService.register(registerRequest);
} }
} }

View File

@ -0,0 +1,18 @@
package asia.yulinling.workflow.model.vo;
import lombok.Data;
import lombok.RequiredArgsConstructor;
/**
* <p>
* 用户注册信息VO
* </p>
*
* @author YLL
* @since 2025/6/30
*/
@Data
@RequiredArgsConstructor
public class RegisterVO {
private Long userId;
}

View File

@ -3,6 +3,7 @@ package asia.yulinling.workflow.service;
import asia.yulinling.workflow.dto.request.LoginRequest; import asia.yulinling.workflow.dto.request.LoginRequest;
import asia.yulinling.workflow.dto.request.RegisterRequest; import asia.yulinling.workflow.dto.request.RegisterRequest;
import asia.yulinling.workflow.model.ApiResponse; import asia.yulinling.workflow.model.ApiResponse;
import asia.yulinling.workflow.model.vo.RegisterVO;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
/** /**
@ -36,5 +37,5 @@ public interface AuthService {
* @param request 注册请求 * @param request 注册请求
* @return 请求结果 * @return 请求结果
*/ */
ApiResponse<?> register(RegisterRequest request) throws Exception; ApiResponse<RegisterVO> register(RegisterRequest request) throws Exception;
} }

View File

@ -6,6 +6,7 @@ import asia.yulinling.workflow.dto.request.RegisterRequest;
import asia.yulinling.workflow.mapper.UserMapper; import asia.yulinling.workflow.mapper.UserMapper;
import asia.yulinling.workflow.model.ApiResponse; import asia.yulinling.workflow.model.ApiResponse;
import asia.yulinling.workflow.model.entity.User; import asia.yulinling.workflow.model.entity.User;
import asia.yulinling.workflow.model.vo.RegisterVO;
import asia.yulinling.workflow.service.AuthService; import asia.yulinling.workflow.service.AuthService;
import asia.yulinling.workflow.utils.JwtUtil; import asia.yulinling.workflow.utils.JwtUtil;
import cn.hutool.core.util.StrUtil; 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.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
@ -80,7 +82,8 @@ public class AuthServiceImpl implements AuthService {
* @return 请求结果 * @return 请求结果
*/ */
@Override @Override
public ApiResponse<?> register(RegisterRequest request) throws Exception { @Transactional(rollbackFor = Exception.class)
public ApiResponse<RegisterVO> register(RegisterRequest request) throws Exception {
if (!StrUtil.equals(request.getConfirmPassword(), request.getPassword())) { if (!StrUtil.equals(request.getConfirmPassword(), request.getPassword())) {
log.info("password {}, confirmPassword {}", 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.setBirthday(request.getBirthday());
user.setStatus(1); user.setStatus(1);
user.setUpdateTime(new Date()); user.setUpdateTime(new Date());
log.info("User: {}", user); log.info("User: {}", user);
try { try {
userMapper.insert(user); 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) { } catch (Exception e) {
log.error("insertUser Error: {}", e.getMessage()); log.error("数据插入用户数据失败: {}", e.getMessage());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return ApiResponse.of(200, "注册成功", null);
} }
} }