¶Ô±ÈÐÂÎļþ |
| | |
| | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | | using Microsoft.Extensions.DependencyModel; |
| | | using Microsoft.IdentityModel.Tokens; |
| | | using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; |
| | | using SqlSugar; |
| | | using System; |
| | | 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 |
| | | { |
| | | /// <summary> |
| | | /// éªè¯å®ä½åæ° |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="data"></param> |
| | | /// <returns></returns> |
| | | public static (bool, string, object?) ValidateModelData<T>(T data) where T : class, new() |
| | | { |
| | | Type modelType = typeof(T); |
| | | if (data == null) return (false, "ä¼ å
¥åæ°ä¸å¯ä¸ºnull", data); |
| | | ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute<ModelValidateAttribute>(); |
| | | if (modelAttribute == null) return (false, $"{modelType.Name}æªå®ä¹ãModelValidateAttributeãç¹æ§", data); |
| | | PropertyInfo[] propertyInfos = modelType.GetProperties(); |
| | | return SimpleValidate(propertyInfos, data); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éªè¯å®ä½åæ° |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="data"></param> |
| | | /// <returns></returns> |
| | | public static (bool, string, object?) ValidateModelData<T>(List<T> 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<ModelValidateAttribute>(); |
| | | 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?) CustomMethodValidate<T>(ModelValidateAttribute modelAttribute, T data) where T : class, new() |
| | | //{ |
| | | // try |
| | | // { |
| | | // if (modelAttribute.CustomValidateMethodTypeName == null) return (false, $"èªå®ä¹éªè¯æ¹æ³éè¦æä¾æ¹æ³æå¨ç±»çç±»å对象", data); |
| | | // if (modelAttribute.CustomValidateMethodName == null) return (false, $"èªå®ä¹éªè¯æ¹æ³éè¦æä¾æ¹æ³å", data); |
| | | |
| | | // string path = Path.Combine(AppContext.BaseDirectory, $"{modelAttribute.CustomValidateAssemblyName}.dll"); |
| | | // Assembly assembly = Assembly.LoadFrom(path); |
| | | |
| | | // Type t = assembly.GetType(modelAttribute.CustomValidateAssemblyName + "." + modelAttribute.CustomValidateMethodTypeName); |
| | | // object bbb = App.GetService(t); |
| | | // MethodInfo? methodInfo = t.GetMethod(modelAttribute.CustomValidateMethodName); |
| | | // var result = methodInfo.Invoke(bbb, new object[] { data }); |
| | | // //MethodInfo? methodInfo = modelAttribute.CustomValidateMethodType.GetMethod(modelAttribute.CustomValidateMethodName); |
| | | // //if (methodInfo == null) return (false, $"æªå¨è¯¥ç±»å对象ã{modelAttribute.CustomValidateMethodType.Name}ã䏿¾å°è¯¥æ¹æ³ã{modelAttribute.CustomValidateMethodName}ã", data); |
| | | // //methodInfo.GetGenericArguments() |
| | | // return (true, "æå", data); |
| | | // } |
| | | // catch(Exception ex) |
| | | // { |
| | | // throw new Exception(); |
| | | // } |
| | | |
| | | //} |
| | | |
| | | private static (bool, string, object?) SimpleValidate<T>(PropertyInfo[] propertyInfos, T data) where T : class, new() |
| | | { |
| | | try |
| | | { |
| | | foreach (PropertyInfo propertyInfo in propertyInfos) |
| | | { |
| | | PropertyValidateAttribute? propertyAttribute = propertyInfo.GetCustomAttribute<PropertyValidateAttribute>(); |
| | | 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 (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); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return (false, $"æ°æ®éªè¯å¼å¸¸,å¼å¸¸ä¿¡æ¯:{ex.Message}", data); |
| | | } |
| | | return (true, "éªè¯æå", data); |
| | | } |
| | | } |
| | | } |