hutongqing
2024-11-26 2c213d4f6543a66845ac5d2bc13eaf06d1033eb6
´úÂë¹ÜÀí/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, "传入参数不可为null", data);
            ModelValidateAttribute? modelAttribute = type.GetCustomAttribute<ModelValidateAttribute>();
            if (modelAttribute == null) return (false, $"{type.Name}未定义【ModelValidateAttribute】特性", 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)}不可为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)