From 2ce8f06cfb4a06f6206a33d7a99daa154717f968 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Tue, 29 Apr 2025 21:37:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/UserInfoController.java | 40 ++++++++----------- .../exception/GlobalExceptionHandler.java | 4 +- .../model/dto/user/UserInfoAddRequest.java | 6 ++- .../model/dto/user/UserInfoLoginRequest.java | 4 +- .../model/dto/user/UserInfoUpdateRequest.java | 3 ++ .../user/impl/UserInfoServiceImpl.java | 1 - 6 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java b/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java index 6db8952..0c349a8 100644 --- a/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java +++ b/src/main/java/com/greenorange/promotion/controller/user/UserInfoController.java @@ -26,10 +26,9 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -70,10 +69,9 @@ public class UserInfoController { */ @PostMapping("login") @Operation(summary = "web端管理员登录", description = "参数:用户登录请求体,权限:管理员(boss, admin),方法名:userInfoLogin") - public BaseResponse userInfoLogin(@RequestBody UserInfoLoginRequest userInfoLoginRequest, HttpServletRequest request) { + public BaseResponse userInfoLogin(@Valid @RequestBody UserInfoLoginRequest userInfoLoginRequest, HttpServletRequest request) { String userAccount = userInfoLoginRequest.getUserAccount(); String userPassword = userInfoLoginRequest.getUserPassword(); - ThrowUtils.throwIf(StringUtils.isAnyBlank(userAccount, userPassword), ErrorCode.PARAMS_ERROR); String token = userInfoService.userInfoLogin(userAccount, userPassword, request); return ResultUtils.success(token); } @@ -87,12 +85,11 @@ public class UserInfoController { @PostMapping("logout") @Operation(summary = "web端管理员退出登录", description = "参数:JWT,权限:管理员(boss, admin),方法名:userInfoLogout") @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse userInfoLogout(@RequestHeader("Authorization") String token) { - // 获取 token 的过期时间 + public BaseResponse userInfoLogout(@NotBlank @RequestHeader("Authorization") String token) { + // 获取token的过期时间 DecodedJWT decodedJWT = jwtUtils.verify(token); long expirationTime = decodedJWT.getExpiresAt().getTime() - System.currentTimeMillis(); - - // 将 token 存入 Redis 黑名单,并设置过期时间与 token 一致 + // 将token存入Redis黑名单,并设置过期时间与token一致 redisTemplate.opsForValue().set(token, token, expirationTime, TimeUnit.MILLISECONDS); return ResultUtils.success(true); } @@ -106,7 +103,7 @@ public class UserInfoController { * @return 是否添加成功 */ @PostMapping("add") - @Operation(summary = "web端管理员添加用户表", description = "参数:用户表添加请求体,权限:管理员(boss, admin),方法名:addUserInfo") + @Operation(summary = "web端管理员添加用户", description = "参数:用户表添加请求体,权限:管理员(boss, admin),方法名:addUserInfo") public BaseResponse addUserInfo(@Valid @RequestBody UserInfoAddRequest userInfoAddRequest) { UserInfo userInfo = commonService.copyProperties(userInfoAddRequest, UserInfo.class); userInfoService.save(userInfo); @@ -121,9 +118,8 @@ public class UserInfoController { * @return 是否更新成功 */ @PostMapping("update") - @Operation(summary = "web端管理员更新用户表", description = "参数:用户表更新请求体,权限:管理员(boss, admin),方法名:updateUserInfo") - public BaseResponse updateUserInfo(@RequestBody UserInfoUpdateRequest userInfoUpdateRequest) { - ThrowUtils.throwIf(userInfoUpdateRequest == null || userInfoUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR); + @Operation(summary = "web端管理员更新用户", description = "参数:用户表更新请求体,权限:管理员(boss, admin),方法名:updateUserInfo") + public BaseResponse updateUserInfo(@Valid @RequestBody UserInfoUpdateRequest userInfoUpdateRequest) { UserInfo userInfo = commonService.copyProperties(userInfoUpdateRequest, UserInfo.class); userInfoService.updateById(userInfo); return ResultUtils.success(true); @@ -135,9 +131,8 @@ public class UserInfoController { * @return 是否删除成功 */ @PostMapping("delete") - @Operation(summary = "web端管理员删除用户表", description = "参数:用户表删除请求体,权限:管理员(boss, admin),方法名:delUserInfo") - public BaseResponse delUserInfo(@RequestBody CommonRequest commonRequest) { - ThrowUtils.throwIf(commonRequest == null || commonRequest.getId() <= 0, ErrorCode.PARAMS_ERROR); + @Operation(summary = "web端管理员删除用户", description = "参数:用户表删除请求体,权限:管理员(boss, admin),方法名:delUserInfo") + public BaseResponse delUserInfo(@Valid @RequestBody CommonRequest commonRequest) { Long id = commonRequest.getId(); userInfoService.removeById(id); return ResultUtils.success(true); @@ -149,9 +144,8 @@ public class UserInfoController { * @return 用户表列表 */ @PostMapping("page") - @Operation(summary = "Web端管理员分页查看用户表", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:listUserInfoByPage") - public BaseResponse> listUserInfoByPage(@RequestBody UserInfoQueryRequest userInfoQueryRequest) { - if (userInfoQueryRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR); + @Operation(summary = "Web端管理员分页查看用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:listUserInfoByPage") + public BaseResponse> listUserInfoByPage(@Valid @RequestBody UserInfoQueryRequest userInfoQueryRequest) { long current = userInfoQueryRequest.getCurrent(); long pageSize = userInfoQueryRequest.getPageSize(); QueryWrapper queryWrapper = userInfoService.getQueryWrapper(userInfoQueryRequest); @@ -173,10 +167,9 @@ public class UserInfoController { * @return 用户表信息 */ @PostMapping("queryById") - @Operation(summary = "web端管理员根据id查询用户表", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById") + @Operation(summary = "web端管理员根据id查询用户", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserInfoById") @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse queryUserInfoById(@RequestBody CommonRequest commonRequest) { - ThrowUtils.throwIf(commonRequest == null || commonRequest.getId() <= 0, ErrorCode.PARAMS_ERROR); + public BaseResponse queryUserInfoById(@Valid @RequestBody CommonRequest commonRequest) { Long id = commonRequest.getId(); UserInfo userInfo = userInfoService.getById(id); ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "当前用户不存在"); @@ -191,9 +184,8 @@ public class UserInfoController { * @return 是否删除成功 */ @PostMapping("delBatch") - @Operation(summary = "web端管理员批量删除用户表", description = "参数:用户表批量删除请求体,权限:管理员(boss, admin),方法名:delBatchUserInfo") - public BaseResponse delBatchUserInfo(@RequestBody CommonBatchRequest commonBatchRequest) { - ThrowUtils.throwIf(commonBatchRequest == null || commonBatchRequest.getIds() == null || commonBatchRequest.getIds().isEmpty(), ErrorCode.PARAMS_ERROR); + @Operation(summary = "web端管理员批量删除用户", description = "参数:用户表批量删除请求体,权限:管理员(boss, admin),方法名:delBatchUserInfo") + public BaseResponse delBatchUserInfo(@Valid @RequestBody CommonBatchRequest commonBatchRequest) { List ids = commonBatchRequest.getIds(); userInfoService.removeByIds(ids); return ResultUtils.success(true); diff --git a/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java b/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java index 6d3f14b..200657b 100644 --- a/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/greenorange/promotion/exception/GlobalExceptionHandler.java @@ -47,10 +47,11 @@ public class GlobalExceptionHandler { @ExceptionHandler(HttpMessageNotReadableException.class) public BaseResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { log.error("HttpMessageNotReadableException", e); - return ResultUtils.error(ErrorCode.PARAMS_ERROR, e.getMessage()); + return ResultUtils.error(ErrorCode.PARAMS_ERROR, "请求体不能为空或格式无效"); } + // 处理业务异常 @ExceptionHandler(BusinessException.class) public BaseResponse businessExceptionHandler(BusinessException e) { log.error("BusinessException", e); @@ -58,6 +59,7 @@ public class GlobalExceptionHandler { } + // 处理运行时异常 @ExceptionHandler(RuntimeException.class) public BaseResponse runtimeExceptionHandler(RuntimeException e) { log.error("RuntimeException", e); diff --git a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoAddRequest.java b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoAddRequest.java index 7e20fb3..79f9b39 100644 --- a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoAddRequest.java +++ b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoAddRequest.java @@ -42,14 +42,16 @@ public class UserInfoAddRequest implements Serializable { * 账号 */ @NotBlank(message = "账号不能为空") - @Schema(description = "账号", example = "qingcheng_account") + @Size(min = 6, max = 10, message = "账号长度在 6 到 10 个字符") + @Schema(description = "账号", example = "qingcheng") private String userAccount; /** * 密码 */ @NotBlank(message = "密码不能为空") - @Schema(description = "密码", example = "qingcheng_password") + @Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符") + @Schema(description = "密码", example = "qingcheng") private String userPassword; /** diff --git a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoLoginRequest.java b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoLoginRequest.java index d1608c2..2da2c67 100644 --- a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoLoginRequest.java +++ b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoLoginRequest.java @@ -18,7 +18,7 @@ public class UserInfoLoginRequest implements Serializable { * 账号 */ @NotBlank(message = "账号不能为空") - @Size(min = 6, max = 8, message = "账号长度在 6 到 8 个字符") + @Size(min = 6, max = 10, message = "账号长度在 6 到 10 个字符") @Schema(description = "账号", example = "qingcheng_account") private String userAccount; @@ -26,7 +26,7 @@ public class UserInfoLoginRequest implements Serializable { * 密码 */ @NotBlank(message = "密码不能为空") - @Size(min = 6, max = 8, message = "密码长度在 6 到 8 个字符") + @Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符") @Schema(description = "密码", example = "qingcheng_password") private String userPassword; diff --git a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoUpdateRequest.java b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoUpdateRequest.java index adf2bd5..f571095 100644 --- a/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoUpdateRequest.java +++ b/src/main/java/com/greenorange/promotion/model/dto/user/UserInfoUpdateRequest.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; import lombok.Data; import java.io.Serial; @@ -50,6 +51,7 @@ public class UserInfoUpdateRequest implements Serializable { * 账号 */ @NotBlank(message = "账号不能为空") + @Size(min = 6, max = 10, message = "账号长度在 6 到 10 个字符") @Schema(description = "账号", example = "qingcheng_account") private String userAccount; @@ -57,6 +59,7 @@ public class UserInfoUpdateRequest implements Serializable { * 密码 */ @NotBlank(message = "密码不能为空") + @Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符") @Schema(description = "密码", example = "qingcheng_password") private String userPassword; diff --git a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java index f591ef8..c8490c5 100644 --- a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java @@ -57,7 +57,6 @@ public class UserInfoServiceImpl extends ServiceImpl */ @Override public String userInfoLogin(String userAccount, String userPassword, HttpServletRequest request) { - ThrowUtils.throwIf(userAccount.length() < 6 || userPassword.length() < 6, ErrorCode.PARAMS_ERROR); LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UserInfo::getUserAccount, userAccount).eq(UserInfo::getUserPassword, userPassword); UserInfo userInfo = this.getOne(lambdaQueryWrapper);