From 2c213d4f6543a66845ac5d2bc13eaf06d1033eb6 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期二, 26 十一月 2024 14:43:17 +0800 Subject: [PATCH] 更新 --- 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs | 80 +++++++++++++++++++++++++-------------- 1 files changed, 51 insertions(+), 29 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" index 3d50266..76e5aaf 100644 --- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" @@ -2,8 +2,10 @@ 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; @@ -32,6 +34,15 @@ return SimpleValidate(propertyInfos, data); } + public static (bool, string, object?) ValidateModelData(object data, Type type) + { + if (data == null) return (false, "浼犲叆鍙傛暟涓嶅彲涓簄ull", data); + ModelValidateAttribute? modelAttribute = type.GetCustomAttribute<ModelValidateAttribute>(); + if (modelAttribute == null) return (false, $"{type.Name}鏈畾涔夈�怣odelValidateAttribute銆戠壒鎬�", data); + PropertyInfo[] propertyInfos = type.GetProperties(); + return SimpleValidate(propertyInfos, data); + } + /// <summary> /// 楠岃瘉瀹炰綋鍙傛暟 /// </summary> @@ -55,32 +66,6 @@ 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() { @@ -171,12 +156,16 @@ if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString()) { string str = value.Serialize(); - if (str == "[]") + + 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) { @@ -185,7 +174,6 @@ return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊间负銆恵propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}銆戞椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间笉鍙负绌�", data); } } - } } else @@ -193,6 +181,40 @@ 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)}涓嶅彲涓簄ull", data); + if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}浣跨敤浜咰heck绾︽潫锛屼紶鍏ョ殑鍊间笉鍦▄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)}涓嶅彲涓簄ull", 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)}涓嶅彲涓簄ull", 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)}涓嶅彲涓簄ull", 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) -- Gitblit v1.9.3