qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.Filters;
using WIDESEA_Core.Utilities;
 
 
namespace WIDESEA_Core.ObjectActionValidator
{
    public static class MethodsValidator
    {
        /// <summary>
        /// 方法上的model校验配置
        /// </summary>
        public static Dictionary<string, string[]> ValidatorCollection { get; } = new Dictionary<string, string[]>();
 
        public static void Add<T>(this ValidatorModel validatorGroup, Expression<Func<T, object>> loginExpress = null)
        {
            if (!ValidatorCollection.TryAdd(validatorGroup.ToString().ToLower(),
                loginExpress == null
                ? typeof(T).GetGenericProperties().Select(x => x.Name).ToArray()
                : loginExpress.GetExpressionToArray()))
            {
                throw new Exception($"键{validatorGroup.ToString()}的表达式已经注册过了");
            }
        }
        /// <summary>
        /// 方法上的普通参数校验配置
        /// </summary>
        public static Dictionary<string, GeneralOptions> ValidatorGeneralCollection { get; } = new Dictionary<string, GeneralOptions>();
 
        /// <summary>
        /// 默认校验的是string类型
        /// </summary>
        /// <param name="general">普通参数名</param>
        /// <param name="CNName">校验错误时显示的提示名字</param>
        public static void Add(this ValidatorGeneral general, string CNName)
        {
            general.Add(CNName, ParamType.String, null, null);
        }
        public static void Add(this ValidatorGeneral general, string CNName, int? max)
        {
            general.Add(CNName, ParamType.String, null, max);
        }
        public static void Add(this ValidatorGeneral general, string CNName, ParamType type)
        {
            general.Add(CNName, type, null, null);
        }
        public static void Add(this ValidatorGeneral general, string CNName, ParamType type, int? max)
        {
            general.Add(CNName, type, null, max);
        }
        public static void Add(this ValidatorGeneral general, string CNName, int? min, int? max)
        {
            general.Add(CNName, ParamType.String, min, max);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="general">普通参数名</param>
        /// <param name="CNName">校验错误时显示的提示名字</param>
        /// <param name="type">参数类型</param>
        /// <param name="min">最大小度(最小值)</param>
        /// <param name="max">最大长度(最大值)</param>
        public static void Add(this ValidatorGeneral general, string CNName, ParamType type, int? min = null, int? max = null)
        {
            GeneralOptions options = new GeneralOptions(general, CNName, type, min, max);
            if (!ValidatorGeneralCollection.TryAdd(general.ToString().ToLower(), options))
            {
                throw new Exception($"键{general.ToString()}参数配置已经注入过了");
            }
        }
        /// <summary>
        /// 自定义验证
        /// </summary>
        /// <param name="general"></param>
        /// <param name="customValidator"></param>
        public static void Add(this ValidatorGeneral general, string CNName, Func<object, ObjectValidatorResult> customValidator)
        {
            GeneralOptions options = new GeneralOptions(general, CNName, customValidator);
            if (!ValidatorGeneralCollection.TryAdd(general.ToString().ToLower(), options))
            {
                throw new Exception($"键{general.ToString()}参数配置已经注入过了");
            }
        }
 
 
 
        /// <summary>
        /// 获取方法上绑定的model校验字段
        /// </summary>
        /// <param name="validatorGroup"></param>
        /// <returns></returns>
        public static string[] GetModelParameters(this ValidatorModel validatorGroup)
        {
            return validatorGroup.ToString().GetModelParameters();
        }
        /// <summary>
        /// 获取方法上绑定的model校验字段
        /// </summary>
        /// <param name="validatorGroup"></param>
        /// <returns></returns>
        public static string[] GetModelParameters(this string validatorGroup)
        {
            if (!ValidatorCollection.TryGetValue(validatorGroup.ToLower(), out string[] values))
            {
                Console.WriteLine($"未注册{validatorGroup}参数的表达式");
                throw new Exception($"未注册{validatorGroup}参数的表达式");
            }
            return values;
        }
        /// <summary>
        /// 获取方法上绑定校验字段的配置信息
        /// </summary>
        /// <param name="general"></param>
        /// <returns></returns>
        public static IEnumerable<GeneralOptions> GetGeneralOption(this ValidatorGeneral[] general)
        {
            return general.Select(x => x.ToString()).ToArray().GetGeneralOption();
        }
        /// <summary>
        /// 获取方法上绑定校验字段的配置信息
        /// </summary>
        /// <param name="general"></param>
        /// <returns></returns>
        public static IEnumerable<GeneralOptions> GetGeneralOption(this string[] generalName)
        {
            foreach (string item in generalName)
            {
                if (!ValidatorGeneralCollection.TryGetValue(item.ToLower(), out GeneralOptions options))
                {
                    throw new Exception($"未注册{generalName.ToString()}参数的配置");
                }
                yield return options;
            }
        }
 
        public static void ActionParamsValidator(this ActionExecutingContext context)
        {
            //普通参数校验
            context.GeneralValidator();
 
            //是否使用了model参数校验
            if (!context.ExistsActionModelValidator()) return;
            //判断当前model校验是否通垸
            ObjectModelValidatorState objectModel = context.HttpContext.GetService<ObjectModelValidatorState>();
            if (!objectModel.Status)
            {
                context.Result = new JsonResult(objectModel);
                return;
            }
 
            //判断是否提交了model参数
            if (!objectModel.HasModelContent)
            {
                context.Result = new JsonResult(new WebResponseContent().Error($"请提交参数"));
                return;
            }
        }
 
        /// <summary>
        /// 是否添加了ModelValidator实体校验
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool ExistsActionModelValidator(this ActionExecutingContext context)
        {
            return context.ActionDescriptor.EndpointMetadata.Any(item => item is ObjectModelValidatorFilter);
        }
        /// <summary>
        /// 是否添加了ModelValidator实体校验
        /// </summary>
        /// <param name="actionContext"></param>
        /// <returns></returns>
        public static string[] GetModelValidatorParams(this ActionContext actionContext)
        {
            return (actionContext.ActionDescriptor
                     .EndpointMetadata
                     .Where(item => item is ObjectModelValidatorFilter)
                     .FirstOrDefault() as ObjectModelValidatorFilter)?.MethodsParameters;
        }
        /// <summary>
        /// model校验
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="prefix"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static void ModelValidator(this ActionContext actionContext, string prefix, object model)
        {
            string[] parameters = actionContext.GetModelValidatorParams();
            //没有设置模型校验的直接返回
            if (parameters == null) return;
            if (model == null)
            {
                actionContext.ErrorResult("没有获取到参数");
                return;
            }
            //model==list未判断
            PropertyInfo[] properties = model.GetType().GetProperties().Where(x => parameters.Contains(x.Name.ToLower())).ToArray();
            foreach (var item in properties)
            {
                if (!item.ValidationRquiredValueForDbType(item.GetValue(model), out string message))
                {
                    actionContext.ErrorResult(message);
                    return;
                }
            }
            actionContext.OkModelResult();
        }
 
        /// <summary>
        /// 普通参数校验
        /// </summary>
        /// <param name="actionContext"></param>
        public static void GeneralValidator(this ActionExecutingContext actionContext)
        {
            if (actionContext.ActionDescriptor
             .EndpointMetadata
             .Where(item => item is ObjectGeneralValidatorFilter)
             .FirstOrDefault() is ObjectGeneralValidatorFilter objectGeneral)
            {
                foreach (GeneralOptions general in objectGeneral.MethodsParameters)
                {
                    if (actionContext.Result != null) return;
                    if (!actionContext.HttpContext.Request.Query.TryGetValue(general.Name, out StringValues value))
                    {
                        actionContext.ActionErrorResult($"请提交参数[{general.CNName}]");
                        return;
                    }
                    if (string.IsNullOrEmpty(value))
                    {
                        actionContext.ActionErrorResult($"参数[{general.CNName}]不能为空");
                        return;
                    }
                    //自定义验证
                    if (general.CustomValidator != null)
                    {
                        ObjectValidatorResult validatorResult = general.CustomValidator(value);
                        if (!validatorResult.Status)
                        {
                            actionContext.ActionErrorResult(validatorResult.Message);
                            return;
                        }
                        continue;
                    }
                    actionContext.ValidatorValue(general, value);
                }
            }
        }
 
        public static void ValidatorNumber(Type type, object value)
        {
            value.ChangeType(type);
        }
        public static void ValidatorValue(this ActionExecutingContext actionContext, GeneralOptions options, object model)
        {
            if (model == null)
            {
                actionContext.ActionErrorResult($"请提交参数{options.CNName}");
                return;
            }
            if (options.Min == null && options.Max == null) return;
            switch (options.ParamType)
            {
                //待完ParamType.Long,Byte类型
                case ParamType.Int:
                    //case ParamType.Long:
                    //case ParamType.Byte:
                    if (!int.TryParse(model.ToString(), out int _number))
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]必须是整数");
                        break;
                    }
                    if (options.Min != null && _number < options.Min)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不能小于[{options.Min}]");
                        break;
                    }
                    if (options.Max != null && _number > options.Max)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不能大于[{options.Max}]");
                    }
                    break;
                case ParamType.String:
                    string value = model.ToString();
                    if (options.Min != null && value.Length < options.Min)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]至少[{options.Min}]个字符");
                    }
                    if (options.Max != null && value.Length > options.Max)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]最多[{options.Max}]个字符");
                    }
                    break;
                //待完日期大小
                case ParamType.DateTime:
                    if (!DateTime.TryParse(model.ToString(),out _))
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]应该是日期格式");
                    }
                    break;
                //min,max应该是decimal类型,待完
                case ParamType.Decimal:
                    if (!decimal.TryParse(model.ToString(), out decimal _decimal))
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不是数字");
                        break;
                    }
                    if (options.Min != null && _decimal < options.Min)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不能小于[{options.Min}]");
                        break;
                    }
                    if (options.Max != null && _decimal > options.Max)
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不能大于[{options.Max}]");
                    }
                    break;
                case ParamType.Guid:
                    if (!Guid.TryParse(model.ToString(), out _))
                    {
                        actionContext.ActionErrorResult($"[{options.CNName}]不是Guid类型");
                    }
                    break;
                default:
                    break;
            }
        }
 
        public static void ActionErrorResult(this ActionExecutingContext actionContext, string message)
        {
            actionContext.Result = new JsonResult(new { Status = false, Message = message });
        }
 
        /// <summary>
        ///参数验证没有通过的直接返回校验结果
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="message"></param>
        public static void ErrorResult(this ActionContext actionContext, string message)
        {
            ObjectModelValidatorState objectModel = actionContext.HttpContext.GetService<ObjectModelValidatorState>();
            if (!objectModel.Status)
            {
                return;
            }
            objectModel.Status = false;
            objectModel.Message = message;
        }
        public static void OkModelResult(this ActionContext actionContext)
        {
            ObjectModelValidatorState objectModel = actionContext.HttpContext.GetService<ObjectModelValidatorState>();
            objectModel.HasModelContent = true;
        }
 
    }
}