参数校验
This commit is contained in:
parent
2ce8f06cfb
commit
dc090de5ab
|
@ -0,0 +1,28 @@
|
|||
package com.greenorange.promotion.annotation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.Arrays;
|
||||
|
||||
// 枚举校验器
|
||||
public class EnumValidator implements ConstraintValidator<EnumValue, String> {
|
||||
|
||||
private EnumValue enumValue;
|
||||
|
||||
@Override
|
||||
public void initialize(EnumValue constraintAnnotation) {
|
||||
this.enumValue = constraintAnnotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
if (value == null) {
|
||||
return true; // 如果值为 null,跳过校验,可以用 @NotNull 另行校验
|
||||
}
|
||||
|
||||
// 获取枚举类
|
||||
Class<? extends Enum<?>> enumClass = enumValue.enumClass();
|
||||
return Arrays.stream(enumClass.getEnumConstants())
|
||||
.anyMatch(enumConstant -> enumConstant.name().equals(value));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.greenorange.promotion.annotation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
// 自定义校验注解
|
||||
@Constraint(validatedBy = EnumValidator.class)
|
||||
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface EnumValue {
|
||||
String message() default "无效的用户角色"; // 错误信息
|
||||
Class<?>[] groups() default {}; // 组别
|
||||
Class<? extends Payload>[] payload() default {}; // 负载
|
||||
Class<? extends Enum<?>> enumClass(); // 枚举类类型
|
||||
}
|
|
@ -40,7 +40,7 @@ import java.util.concurrent.TimeUnit;
|
|||
@RestController
|
||||
@RequestMapping("userInfo")
|
||||
@Slf4j
|
||||
@Tag(name = "用户表管理")
|
||||
@Tag(name = "用户管理")
|
||||
public class UserInfoController {
|
||||
|
||||
|
||||
|
@ -85,7 +85,7 @@ public class UserInfoController {
|
|||
@PostMapping("logout")
|
||||
@Operation(summary = "web端管理员退出登录", description = "参数:JWT,权限:管理员(boss, admin),方法名:userInfoLogout")
|
||||
@RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> userInfoLogout(@NotBlank @RequestHeader("Authorization") String token) {
|
||||
public BaseResponse<Boolean> userInfoLogout(@RequestHeader("Authorization") String token) {
|
||||
// 获取token的过期时间
|
||||
DecodedJWT decodedJWT = jwtUtils.verify(token);
|
||||
long expirationTime = decodedJWT.getExpiresAt().getTime() - System.currentTimeMillis();
|
||||
|
@ -152,12 +152,10 @@ public class UserInfoController {
|
|||
Page<UserInfo> page = userInfoService.page(new Page<>(current, pageSize), queryWrapper);
|
||||
List<UserInfo> userInfoList = page.getRecords();
|
||||
List<UserInfoVO> userInfoVOList = commonService.convertList(userInfoList, UserInfoVO.class);
|
||||
Page<UserInfoVO> voPage = new Page<>();
|
||||
Page<UserInfoVO> voPage = new Page<>(current, pageSize);
|
||||
voPage.setRecords(userInfoVOList);
|
||||
voPage.setPages(page.getPages());
|
||||
voPage.setCurrent(page.getCurrent());
|
||||
voPage.setTotal(page.getTotal());
|
||||
voPage.setSize(page.getSize());
|
||||
return ResultUtils.success(voPage);
|
||||
}
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ public class Generator {
|
|||
.strategyConfig(builder -> {
|
||||
builder.addInclude(TABLE_NAME) // 设置需要生成的数据表名
|
||||
.controllerBuilder()
|
||||
.enableFileOverride() // 覆盖controller
|
||||
// .enableFileOverride() // 覆盖controller
|
||||
.enableRestStyle() // 开启生成 @RestController 控制器
|
||||
.formatFileName("%sController"); // 格式化 Controller 类文件名称,%s进行匹配表名,如 UserController
|
||||
builder.entityBuilder().disable(); // 禁止生成 Entity
|
||||
|
@ -101,13 +101,13 @@ public class Generator {
|
|||
// DTO
|
||||
List<CustomFile> customFiles = new ArrayList<>();
|
||||
customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_ADD_REQUEST)
|
||||
.templatePath(DTO_ADD_REQUEST_TEMPLATE).enableFileOverride().build());
|
||||
.templatePath(DTO_ADD_REQUEST_TEMPLATE).build());
|
||||
customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_UPDATE_REQUEST)
|
||||
.templatePath(DTO_UPDATE_REQUEST_TEMPLATE).enableFileOverride().build());
|
||||
.templatePath(DTO_UPDATE_REQUEST_TEMPLATE).build());
|
||||
customFiles.add(new CustomFile.Builder().packageName(DTO_PACKAGE).fileName(DTO_QUERY_REQUEST)
|
||||
.templatePath(DTO_QUERY_REQUEST_TEMPLATE).enableFileOverride().build());
|
||||
.templatePath(DTO_QUERY_REQUEST_TEMPLATE).build());
|
||||
customFiles.add(new CustomFile.Builder().packageName(VO_PACKAGE).fileName(VO)
|
||||
.templatePath(VO_TEMPLATE).enableFileOverride().build());
|
||||
.templatePath(VO_TEMPLATE).build());
|
||||
consumer.customFile(customFiles);
|
||||
})
|
||||
//7、模板
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.greenorange.promotion.model.dto.user;
|
||||
|
||||
import com.greenorange.promotion.annotation.EnumValue;
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
@ -13,7 +15,7 @@ import java.io.Serializable;
|
|||
* 用户添加请求体
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表添加请求体", requiredProperties = {"nickName", "userAvatar", "phoneNumber",
|
||||
@Schema(description = "用户添加请求体", requiredProperties = {"nickName", "userAvatar", "phoneNumber",
|
||||
"userAccount", "userPassword", "userRole"})
|
||||
public class UserInfoAddRequest implements Serializable {
|
||||
|
||||
|
@ -63,9 +65,9 @@ public class UserInfoAddRequest implements Serializable {
|
|||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@NotBlank(message = "用户角色不能为空")
|
||||
@Schema(description = "用户角色", example = "user")
|
||||
private String userRole;
|
||||
@EnumValue(enumClass = UserRoleEnum.class)
|
||||
@Schema(description = "用户角色", example = "USER")
|
||||
private UserRoleEnum userRole;
|
||||
|
||||
/**
|
||||
* 上级用户id
|
||||
|
|
|
@ -19,7 +19,7 @@ public class UserInfoLoginRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "账号不能为空")
|
||||
@Size(min = 6, max = 10, message = "账号长度在 6 到 10 个字符")
|
||||
@Schema(description = "账号", example = "qingcheng_account")
|
||||
@Schema(description = "账号", example = "qingcheng")
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@ public class UserInfoLoginRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符")
|
||||
@Schema(description = "密码", example = "qingcheng_password")
|
||||
@Schema(description = "密码", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import com.greenorange.promotion.common.PageRequest;
|
|||
* 用户查询请求体,继承自分页请求 PageRequest
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表查询请求体", requiredProperties = {"current", "pageSize"})
|
||||
@Schema(description = "用户查询请求体", requiredProperties = {"current", "pageSize"})
|
||||
public class UserInfoQueryRequest extends PageRequest implements Serializable {
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.io.Serializable;
|
|||
* 用户更新请求体
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表更新请求体", requiredProperties = {"id", "nickName", "userAvatar", "phoneNumber",
|
||||
@Schema(description = "用户更新请求体", requiredProperties = {"id", "nickName", "userAvatar", "phoneNumber",
|
||||
"userAccount", "userPassword", "userRole"})
|
||||
public class UserInfoUpdateRequest implements Serializable {
|
||||
|
||||
|
@ -52,7 +52,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "账号不能为空")
|
||||
@Size(min = 6, max = 10, message = "账号长度在 6 到 10 个字符")
|
||||
@Schema(description = "账号", example = "qingcheng_account")
|
||||
@Schema(description = "账号", example = "qingcheng")
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
|
@ -60,7 +60,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
|||
*/
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 10, message = "密码长度在 6 到 10 个字符")
|
||||
@Schema(description = "密码", example = "qingcheng_password")
|
||||
@Schema(description = "密码", example = "qingcheng")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user