1
wangxinhui
2025-01-18 a7ceaaa38a4394b82501ca60230e97d25a6871e3
´úÂë¹ÜÀí/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>
@@ -56,32 +67,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()
        {
            try
@@ -92,35 +77,49 @@
                    if (propertyAttribute == null) continue;
                    object? value = propertyInfo.GetValue(data, null);
                    if (value is IList)
                    {
                        IList list = (IList)value;
                        Type? t = list.GetType().GetGenericArguments().FirstOrDefault();
                        if (t != null && t.IsClass)
                        {
                            foreach (var item in list)
                            {
                                (bool, string, object?) result = ValidateModelData(item, t);
                                if (!result.Item1) return result;
                            }
                        }
                    }
                    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 (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为空字符串", data);
                    }
                    if (propertyAttribute.MinValue > int.MinValue)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为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);
                            if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{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 (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{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 (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为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);
                            if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{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 (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{value}】要小于【{propertyAttribute.MaxValue}】", data);
                        }
                    }
@@ -171,12 +170,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 +188,6 @@
                                        return (false, $"【{property.Name}】属性的值为【{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}】时【{propertyInfo.Name}】属性的值不可为空", data);
                                    }
                                }
                            }
                        }
                        else
@@ -193,6 +195,38 @@
                            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)}({propertyInfo.Name})不可为null", data);
                        if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})使用了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)}({propertyInfo.Name})不可为null", data);
                        else
                        {
                            if (value.ToString().StartsWith(propertyAttribute.StartWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})必须以{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)}({propertyInfo.Name})不可为null", data);
                        else
                        {
                            if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})必须以{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)