using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.DependencyModel; using Microsoft.IdentityModel.Tokens; using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using SqlSugar; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEA_Core.Attributes; using WIDESEA_Core.Helper; namespace WIDESEA_Core.Utilities { public class ModelValidate { /// /// 验证实体参数 /// /// /// /// public static (bool, string, object?) ValidateModelData(T data) where T : class, new() { Type modelType = typeof(T); if (data == null) return (false, "传入参数不可为null", data); ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute(); if (modelAttribute == null) return (false, $"{modelType.Name}未定义【ModelValidateAttribute】特性", data); PropertyInfo[] propertyInfos = modelType.GetProperties(); return SimpleValidate(propertyInfos, data); } public static (bool, string, object?) ValidateModelData(object data, Type type) { if (data == null) return (false, "传入参数不可为null", data); ModelValidateAttribute? modelAttribute = type.GetCustomAttribute(); if (modelAttribute == null) return (false, $"{type.Name}未定义【ModelValidateAttribute】特性", data); PropertyInfo[] propertyInfos = type.GetProperties(); return SimpleValidate(propertyInfos, data); } /// /// 验证实体参数 /// /// /// /// public static (bool, string, object?) ValidateModelData(List datas) where T : class, new() { Type modelType = typeof(T); if (datas == null) return (false, "传入参数不可为null", datas); if (datas.Count == 0) return (false, "集合个数不可等于0", datas); foreach (T data in datas) { if (data == null) return (false, "传入参数不可为null", data); ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute(); if (modelAttribute == null) return (false, $"{modelType.Name}未定义【ModelValidateAttribute】特性", data); PropertyInfo[] propertyInfos = modelType.GetProperties(); (bool, string, object?) result = SimpleValidate(propertyInfos, data); if (!result.Item1) return result; } return (true, $"成功", datas); } private static (bool, string, object?) SimpleValidate(PropertyInfo[] propertyInfos, T data) where T : class, new() { try { foreach (PropertyInfo propertyInfo in propertyInfos) { PropertyValidateAttribute? propertyAttribute = propertyInfo.GetCustomAttribute(); if (propertyAttribute == null) continue; object? value = propertyInfo.GetValue(data, null); if (propertyAttribute.NotNullAndEmpty) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为空字符串", data); } if (propertyAttribute.MinValue > int.MinValue) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); if (propertyAttribute.IsContainMinValue) { if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可小于【{propertyAttribute.MinValue}】", data); } else { if (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】要大于【{propertyAttribute.MinValue}】", data); } } if (propertyAttribute.MaxValue < int.MaxValue) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); if (propertyAttribute.IsContainMaxValue) { if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可大于【{propertyAttribute.MaxValue}】", data); } else { if (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】要小于【{propertyAttribute.MaxValue}】", data); } } if (!string.IsNullOrEmpty(propertyAttribute.NotNullAndEmptyWithProperty)) { PropertyInfo? property = propertyInfos.FirstOrDefault(x => x.Name == propertyAttribute.NotNullAndEmptyWithProperty); if (property != null) { object? tempValue = property.GetValue(data); if (tempValue != null && !string.IsNullOrEmpty(tempValue.ToString())) { Type[] types = propertyInfo.PropertyType.GenericTypeArguments; if (types.Length == 1) { string str = value.Serialize(); if (str == "[]") { return (false, $"【{property.Name}】属性的值非空时【{propertyInfo.Name}】属性的值也不可为空", data); } } else if (types.Length == 0) { if (value == null) { return (false, $"【{property.Name}】属性的值非空时【{propertyInfo.Name}】属性的值也不可为空", data); } } } } else { return (false, $"未找到【{propertyInfo.Name}】特性属性【{propertyAttribute.NotNullAndEmptyWithProperty}】", data); } } if (propertyAttribute.NotNullAndEmptyWithPropertyAndValue != null && propertyAttribute.NotNullAndEmptyWithPropertyAndValue.Length == 2) { PropertyInfo? property = propertyInfos.FirstOrDefault(x => x.Name == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[0]); if (property != null) { object? tempValue = property.GetValue(data); if (tempValue != null && !string.IsNullOrEmpty(tempValue.ToString())) { Type[] types = propertyInfo.PropertyType.GenericTypeArguments; if (types.Length == 1) { if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString()) { string str = value.Serialize(); if (typeof(IList).IsAssignableFrom(property.PropertyType) && str == "[]") { return (false, $"【{property.Name}】属性的值为【{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}】时【{propertyInfo.Name}】属性的值不可为空", data); } else if (property.PropertyType.IsArray && str == "[]") { return (false, $"【{property.Name}】属性的值为【{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}】时【{propertyInfo.Name}】属性的值不可为空", data); } } } else if (types.Length == 0) { if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString() && value == null && string.IsNullOrEmpty(value.ToString())) { return (false, $"【{property.Name}】属性的值为【{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}】时【{propertyInfo.Name}】属性的值不可为空", data); } } } } else { return (false, $"未找到属性【{propertyAttribute.NotNullAndEmptyWithProperty}】", data); } } if (propertyAttribute.Check != null && propertyAttribute.Check.Length > 0) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}使用了Check约束,传入的值不在{propertyAttribute.Check.Serialize()}中", data); } if (!string.IsNullOrEmpty(propertyAttribute.StartWith) && !string.IsNullOrWhiteSpace(propertyAttribute.EndWith)) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); else { if (value.ToString().StartsWith(propertyAttribute.StartWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}必须以{propertyAttribute.StartWith}开头", data); } } if (!string.IsNullOrEmpty(propertyAttribute.EndWith) && !string.IsNullOrWhiteSpace(propertyAttribute.EndWith)) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); else { if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}必须以{propertyAttribute.EndWith}结尾", data); } } if (propertyAttribute.MaxLength > propertyAttribute.MinLength && propertyAttribute.MinLength > 0) { if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data); if (value.ToString().Length > propertyAttribute.MaxLength) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值超出最大长度【{propertyAttribute.MaxLength}】,数据:{value}", data); if (value.ToString().Length < propertyAttribute.MinLength) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值小于最小长度【{propertyAttribute.MinLength}】,数据:{value}", data); } } } catch (Exception ex) { return (false, $"数据验证异常,异常信息:{ex.Message}", data); } return (true, "验证成功", data); } } }