diff --git a/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java b/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java index a54d83e..6226805 100644 --- a/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java +++ b/opsli-api/src/main/java/org/opsli/api/base/result/ResultVo.java @@ -156,6 +156,7 @@ public class ResultVo implements Serializable { ResultVo ret = new ResultVo<>(); ret.setMsg(msg); ret.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value()); + ret.setSuccess(false); return ret; } @@ -170,6 +171,7 @@ public class ResultVo implements Serializable { ResultVo ret = new ResultVo<>(); ret.setMsg(msg); ret.setCode(code); + ret.setSuccess(false); return ret; } @@ -186,6 +188,7 @@ public class ResultVo implements Serializable { ret.setMsg(msg); ret.setCode(code); ret.put(data); + ret.setSuccess(false); return ret; } diff --git a/opsli-api/src/main/java/org/opsli/api/msg/ValidationMsg.java b/opsli-api/src/main/java/org/opsli/api/msg/ValidationMsg.java new file mode 100644 index 0000000..dbab91c --- /dev/null +++ b/opsli-api/src/main/java/org/opsli/api/msg/ValidationMsg.java @@ -0,0 +1,80 @@ +package org.opsli.api.msg; + +import org.opsli.common.base.msg.BaseMsg; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.core.msg + * @Author: Parker + * @CreateTime: 2020-09-13 19:36 + * @Description: 参数验证 - 消息 + */ +public enum ValidationMsg implements BaseMsg { + + /** 不能为空 */ + EXCEPTION_IS_NOT_NULL(9800,"不能为空! "), + /** 字母,数字和下划线 */ + EXCEPTION_IS_GENERAL(9801,"不是字母,数字或下划线! "), + /** 数字 */ + EXCEPTION_IS_NUMBER(9802,"不是数字! "), + /** 纯字母 */ + EXCEPTION_IS_LETTER(9803,"不是纯字母! "), + /** 大写 */ + EXCEPTION_IS_UPPER_CASE(9804,"不是大写字母! "), + /** 小写 */ + EXCEPTION_IS_LOWER_CASE(9805,"不是小写字母! "), + /** ip4 */ + EXCEPTION_IS_IPV4(9806,"不是IPV4地址! "), + /** 金额 */ + EXCEPTION_IS_MONEY(9807,"不是金额! "), + /** 邮箱 */ + EXCEPTION_IS_EMAIL(9808,"不是邮箱! "), + /** 手机号 */ + EXCEPTION_IS_MOBILE(9809,"不是手机号! "), + /** 18位身份证 */ + EXCEPTION_IS_CITIZENID(9810,"不是18位身份证! "), + /** 邮编 */ + EXCEPTION_IS_ZIPCODE(9811,"不是邮编! "), + /** URL */ + EXCEPTION_IS_URL(9812,"不是URL! "), + /** 汉字 */ + EXCEPTION_IS_CHINESE(9813,"不是汉字! "), + /** 汉字,字母,数字和下划线 */ + EXCEPTION_IS_GENERAL_WITH_CHINESE(9814,"不是汉字,字母,数字和下划线! "), + /** MAC地址 */ + EXCEPTION_IS_MAC(9815,"不是MAC地址! "), + /** 中国车牌 */ + EXCEPTION_IS_PLATE_NUMBER(9816,"不是中国车牌! "), + + /** 超出最大长度 */ + EXCEPTION_IS_MAX(9816,"超出最大长度! "), + + /** 小于最小长度 */ + EXCEPTION_IS_MIN(9816,"小于最小长度! "), + + ; + + private static final String PREFIX = "参数验证错误: "; + private int code; + private String fieldName; + private String message; + + ValidationMsg(int code, String message){ + this.code = code; + this.message = message; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + @Override + public Integer getCode() { + return this.code; + } + + @Override + public String getMessage() { + return PREFIX + this.fieldName + "," + this.message; + } +} diff --git a/opsli-api/src/main/java/org/opsli/api/utils/ValidationUtil.java b/opsli-api/src/main/java/org/opsli/api/utils/ValidationUtil.java new file mode 100644 index 0000000..3e077ba --- /dev/null +++ b/opsli-api/src/main/java/org/opsli/api/utils/ValidationUtil.java @@ -0,0 +1,330 @@ +package org.opsli.api.utils; + +import cn.hutool.core.lang.Validator; +import cn.hutool.core.util.ReflectUtil; +import io.swagger.annotations.ApiModelProperty; +import lombok.extern.slf4j.Slf4j; +import org.opsli.api.msg.ValidationMsg; +import org.opsli.api.wrapper.system.dict.SysDictModel; +import org.opsli.common.annotation.validation.ValidationArgs; +import org.opsli.common.annotation.validation.ValidationArgsMax; +import org.opsli.common.annotation.validation.ValidationArgsMin; +import org.opsli.common.enums.ValiArgsType; +import org.opsli.common.exception.ServiceException; + +import java.lang.reflect.Field; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.utils + * @Author: Parker + * @CreateTime: 2020-09-22 17:29 + * @Description: 验证器工具类 + */ +@Slf4j +public final class ValidationUtil { + + /** 编码格式 */ + private static final String CHARSET_NAME = "utf-8"; + + /** + * 验证对象 + * @param obj + */ + public static void verify(Object obj){ + if(obj == null){ + return; + } + + Field[] fields = ReflectUtil.getFields(obj.getClass()); + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + // 获得 统一验证 注解 + ValidationArgs validationArgs = field.getAnnotation(ValidationArgs.class); + if(validationArgs != null){ + ValiArgsType[] types = validationArgs.value(); + Object fieldValue = ReflectUtil.getFieldValue(obj, field); + ValidationUtil.check(field, types, fieldValue); + } + + // 获得 最大长度 注解 + ValidationArgsMax validationArgsMax = field.getAnnotation(ValidationArgsMax.class); + if(validationArgsMax != null){ + int maxLength = validationArgsMax.value(); + Object fieldValue = ReflectUtil.getFieldValue(obj, field); + ValidationUtil.checkMax(field, maxLength, fieldValue); + } + + // 获得 最小长度 注解 + ValidationArgsMin validationArgsMin = field.getAnnotation(ValidationArgsMin.class); + if(validationArgsMin != null){ + int minLength = validationArgsMin.value(); + Object fieldValue = ReflectUtil.getFieldValue(obj, field); + ValidationUtil.checkMin(field, minLength, fieldValue); + } + } + } + + /** + * 通用校验 + * @param field + * @param types + * @param fieldValue + */ + private static void check(Field field, ValiArgsType[] types, Object fieldValue){ + // 获得字段名 + String fieldName = field.getName(); + ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); + if(annotation != null){ + fieldName = annotation.value(); + } + + // 循环验证 + for (ValiArgsType type : types) { + try { + switch (type) { + // 不能为空 + case IS_NOT_NULL: + boolean notNull = Validator.isNotEmpty(fieldValue); + if(!notNull){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_NOT_NULL; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 字母,数字和下划线 + case IS_GENERAL: + boolean general = Validator.isGeneral(String.valueOf(fieldValue)); + if(!general){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_GENERAL; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 数字 + case IS_NUMBER: + boolean number = Validator.isNumber(String.valueOf(fieldValue)); + if(!number){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_NUMBER; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 纯字母 + case IS_LETTER: + boolean letter = Validator.isLetter(String.valueOf(fieldValue)); + if(!letter){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_LETTER; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 大写 + case IS_UPPER_CASE: + boolean upperCase = Validator.isUpperCase(String.valueOf(fieldValue)); + if(!upperCase){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_UPPER_CASE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 小写 + case IS_LOWER_CASE: + boolean lowerCase = Validator.isLowerCase(String.valueOf(fieldValue)); + if(!lowerCase){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_LOWER_CASE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // IPV4 + case IS_IPV4: + boolean ipv4 = Validator.isIpv4(String.valueOf(fieldValue)); + if(!ipv4){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_IPV4; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 金额 + case IS_MONEY: + boolean money = Validator.isMoney(String.valueOf(fieldValue)); + if(!money){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_MONEY; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 邮箱 + case IS_EMAIL: + boolean email = Validator.isEmail(String.valueOf(fieldValue)); + if(!email){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_EMAIL; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 手机号 + case IS_MOBILE: + boolean mobile = Validator.isMobile(String.valueOf(fieldValue)); + if(!mobile){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_MOBILE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 18位身份证 + case IS_CITIZENID: + boolean citizenId = Validator.isCitizenId(String.valueOf(fieldValue)); + if(!citizenId){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_CITIZENID; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 邮编 + case IS_ZIPCODE: + boolean zipCode = Validator.isZipCode(String.valueOf(fieldValue)); + if(!zipCode){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_ZIPCODE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // URL + case IS_URL: + boolean url = Validator.isUrl(String.valueOf(fieldValue)); + if(!url){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_URL; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 汉字 + case IS_CHINESE: + boolean chinese = Validator.isChinese(String.valueOf(fieldValue)); + if(!chinese){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_CHINESE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 汉字,字母,数字和下划线 + case IS_GENERAL_WITH_CHINESE: + boolean generalWithChinese = Validator.isGeneralWithChinese(String.valueOf(fieldValue)); + if(!generalWithChinese){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_GENERAL_WITH_CHINESE; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // MAC地址 + case IS_MAC: + boolean mac = Validator.isMac(String.valueOf(fieldValue)); + if(!mac){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_MAC; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + // 中国车牌 + case IS_PLATE_NUMBER: + boolean plateNumber = Validator.isPlateNumber(String.valueOf(fieldValue)); + if(!plateNumber){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_PLATE_NUMBER; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + break; + default: + break; + } + }catch (ServiceException e){ + throw e; + }catch (Exception e){ + log.error(e.getMessage(),e); + } + } + } + + + /** + * 最大长度校验 + * @param field + * @param maxLength + * @param fieldValue + */ + private static void checkMax(Field field, int maxLength, Object fieldValue){ + // 获得字段名 + String fieldName = field.getName(); + ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); + if(annotation != null){ + fieldName = annotation.value(); + } + + // 循环验证 + try { + String value = String.valueOf(fieldValue); + if(value != null){ + // 转换为 数据库真实 长度 + int strLength = value.getBytes(CHARSET_NAME).length; + if(strLength > maxLength){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_MAX; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + } + }catch (ServiceException e){ + throw e; + }catch (Exception e){ + log.error(e.getMessage(),e); + } + } + + /** + * 最小长度校验 + * @param field + * @param minLength + * @param fieldValue + */ + private static void checkMin(Field field, int minLength, Object fieldValue){ + // 获得字段名 + String fieldName = field.getName(); + ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); + if(annotation != null){ + fieldName = annotation.value(); + } + + // 循环验证 + try { + String value = String.valueOf(fieldValue); + if(value != null){ + // 转换为 数据库真实 长度 + int strLength = value.getBytes(CHARSET_NAME).length; + if(strLength < minLength){ + ValidationMsg msg = ValidationMsg.EXCEPTION_IS_MIN; + msg.setFieldName(fieldName); + throw new ServiceException(msg); + } + } + }catch (ServiceException e){ + throw e; + }catch (Exception e){ + log.error(e.getMessage(),e); + } + } + + public static void main(String[] args) { + SysDictModel sysDictModel = new SysDictModel(); + sysDictModel.setTypeCode("asdsa"); + sysDictModel.setTypeName("阿哈哈哈哈"); + sysDictModel.setRemark("测试11232131231231223123"); + sysDictModel.setIzLock('1'); + + ValidationUtil.verify(sysDictModel); + } + + + // ================ + private ValidationUtil(){} +} diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgs.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgs.java new file mode 100644 index 0000000..43b5483 --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgs.java @@ -0,0 +1,21 @@ +package org.opsli.common.annotation.validation; + +import org.opsli.common.enums.ValiArgsType; + +import java.lang.annotation.*; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.annotation + * @Author: Parker + * @CreateTime: 2020-09-22 17:07 + * @Description: 字段验证器 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +public @interface ValidationArgs { + + ValiArgsType[] value(); + +} diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMax.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMax.java new file mode 100644 index 0000000..742228c --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMax.java @@ -0,0 +1,23 @@ +package org.opsli.common.annotation.validation; + + +import java.lang.annotation.*; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.annotation + * @Author: Parker + * @CreateTime: 2020-09-22 17:07 + * @Description: 字段验证器 - 字段最大长度 + * + * 对应 数据库 真实长度数 + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +public @interface ValidationArgsMax { + + int value(); + +} diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMin.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMin.java new file mode 100644 index 0000000..8191752 --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/annotation/validation/ValidationArgsMin.java @@ -0,0 +1,23 @@ +package org.opsli.common.annotation.validation; + + +import java.lang.annotation.*; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.annotation + * @Author: Parker + * @CreateTime: 2020-09-22 17:07 + * @Description: 字段验证器 - 字段最大长度 + * + * 对应 数据库 真实长度数 + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +public @interface ValidationArgsMin { + + int value(); + +} diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/enums/ValiArgsType.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/enums/ValiArgsType.java new file mode 100644 index 0000000..623a01f --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/enums/ValiArgsType.java @@ -0,0 +1,93 @@ +package org.opsli.common.enums; + + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.enums + * @Author: Parker + * @CreateTime: 2020-09-17 23:40 + * @Description: 通过单例 模式 生成系统唯一标示 + */ +public enum ValiArgsType { + + /** 不能为空 */ + IS_NOT_NULL, + /** 字母,数字和下划线 */ + IS_GENERAL, + /** 数字 */ + IS_NUMBER, + /** 纯字母 */ + IS_LETTER, + /** 大写 */ + IS_UPPER_CASE, + /** 小写 */ + IS_LOWER_CASE, + /** ip4 */ + IS_IPV4, + /** 金额 */ + IS_MONEY, + /** 邮箱 */ + IS_EMAIL, + /** 手机号 */ + IS_MOBILE, + /** 18位身份证 */ + IS_CITIZENID, + /** 邮编 */ + IS_ZIPCODE, + /** URL */ + IS_URL, + /** 汉字 */ + IS_CHINESE, + /** 汉字,字母,数字和下划线 */ + IS_GENERAL_WITH_CHINESE, + /** MAC地址 */ + IS_MAC, + /** 中国车牌 */ + IS_PLATE_NUMBER, + + ; + public static void main(String[] args) { + /* + 为空判断 + isNull + 字母,数字和下划线 + isGeneral + 数字 + isNumber + 纯字母 + isLetter + 大小写 + isUpperCase + isLowerCase + ip4 + isIpv4 + 金额 + isMoney + 邮件 + isEmail + 手机号码 + isMobile + 18位身份证 + isCitizenId + 邮编 + isZipCode + URL + isUrl + 汉字 + isChinese + 汉字,字母,数字和下划线 + isGeneralWithChinese + mac地址 + isMac + 中国车牌 + isPlateNumber + uuid + isUUID + * + * + * + * + * + * */ + } +}