提交
This commit is contained in:
116
api_v1/Appointment.Entity/ValidateAttribute/AttributeExtend.cs
Normal file
116
api_v1/Appointment.Entity/ValidateAttribute/AttributeExtend.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using Appointment.Entity.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证的执行
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-5
|
||||
/// </remarks>
|
||||
public static class AttributeExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 全部实体属性根据特性校验
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t">验证的实体</param>
|
||||
/// <returns></returns>
|
||||
public static ApiResult Validate<T>(this T t) where T : class
|
||||
{
|
||||
List<string> validates = new List<string>();
|
||||
|
||||
Type type = t.GetType();
|
||||
foreach (PropertyInfo prop in type.GetProperties())
|
||||
{
|
||||
object oValue = prop.GetValue(t);
|
||||
|
||||
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
|
||||
{
|
||||
string msg = attr.Validate(oValue);
|
||||
if (!string.IsNullOrWhiteSpace(msg)) validates.Add(msg);
|
||||
}
|
||||
|
||||
// 2020-10-12 添加屏蔽HTML注入
|
||||
if (prop.PropertyType.Name == "String")
|
||||
{
|
||||
string sValue = (string)prop.GetValue(t);
|
||||
if (sValue != null && !string.IsNullOrEmpty(sValue))
|
||||
{
|
||||
var isIgnoreHtml = prop.GetCustomAttribute(typeof(IsIgnoreHtml), true);
|
||||
if (isIgnoreHtml == null)
|
||||
{
|
||||
sValue = sValue.Replace("<", "<").Replace(">", ">");
|
||||
prop.SetValue(t, sValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validates.Count == 0 ? ApiResult.Success() : ApiResult.Failed(string.Join(",", validates));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定实体的属性名称,根据特性进行校验
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t">验证的实体</param>
|
||||
/// <param name="list">实体的属性名称</param>
|
||||
/// <returns></returns>
|
||||
public static ApiResult Validate<T>(this T t, List<string> list) where T : class
|
||||
{
|
||||
List<string> validates = new List<string>();
|
||||
|
||||
Type type = t.GetType();
|
||||
foreach (PropertyInfo prop in type.GetProperties())
|
||||
{
|
||||
object oValue = prop.GetValue(t);
|
||||
|
||||
if (list.Contains(prop.Name))
|
||||
{
|
||||
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
|
||||
{
|
||||
|
||||
string msg = attr.Validate(oValue);
|
||||
if (!string.IsNullOrWhiteSpace(msg)) validates.Add(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validates.Count == 0 ? ApiResult.Success() : ApiResult.Failed(string.Join(",", validates));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用委托,全部实体属性根据特性校验
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t">验证的实体</param>
|
||||
/// <param name="func">一个ApiResult入参,一个ApiResult返回值的委托</param>
|
||||
/// <returns></returns>
|
||||
public static ApiResult Validate<T>(this T t, Func<ApiResult, ApiResult> func)
|
||||
{
|
||||
List<string> validates = new List<string>();
|
||||
|
||||
Type type = t.GetType();
|
||||
foreach (PropertyInfo prop in type.GetProperties())
|
||||
{
|
||||
object oValue = prop.GetValue(t);
|
||||
|
||||
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
|
||||
{
|
||||
string msg = attr.Validate(oValue);
|
||||
if (!string.IsNullOrWhiteSpace(msg))
|
||||
{
|
||||
validates.Add(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validates.Count == 0 ? func.Invoke(ApiResult.Success()) : ApiResult.Failed(string.Join(",", validates));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
api_v1/Appointment.Entity/ValidateAttribute/IsIgnoreHtml.cs
Normal file
20
api_v1/Appointment.Entity/ValidateAttribute/IsIgnoreHtml.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 忽略HTML标签
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-10-15
|
||||
/// </remarks>
|
||||
public class IsIgnoreHtml : ValidateAttribute
|
||||
{
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 整型数值值范围效验
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-11
|
||||
/// 使用格式:实体属性前加[IntValue(minValue: 1000, maxValue: 999999, ErrorMessage = "验证码长度不正确")]
|
||||
/// </remarks>
|
||||
public class LongValueAttribute : ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 报错提示
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最小值(包含)
|
||||
/// </summary>
|
||||
public long MinValue { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最大值(包含)
|
||||
/// </summary>
|
||||
public long MaxValue { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="minValue">最小值(包含)</param>
|
||||
/// <param name="maxValue">最大值(包含)</param>
|
||||
public LongValueAttribute(long minValue, long maxValue)
|
||||
{
|
||||
this.MinValue = minValue;
|
||||
this.MaxValue = maxValue;
|
||||
}
|
||||
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
if (oValue != null)
|
||||
{
|
||||
long lValue = Convert.ToInt64(oValue);
|
||||
if (lValue < MinValue || lValue > MaxValue) return ErrorMessage;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Appointment.Entity.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 手机格式校验
|
||||
/// 注意:当存在内容时才校验,如果为必填信息,请同时使用必填特性
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-11
|
||||
/// 使用格式:实体属性前加 [Phone(ErrorMessage = "手机号码不正确")]
|
||||
/// </remarks>
|
||||
public class PhoneAttribute : ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 报错提示信息
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
if (oValue != null && !string.IsNullOrWhiteSpace(oValue.ToString()))
|
||||
{
|
||||
string sPhone = oValue.ToString().Replace("+86", "");
|
||||
if (!sPhone.IsPhone()) return ErrorMessage;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串长度校验特性
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-5
|
||||
/// 使用格式:实体属性前加[StringLength(50, MinLength = 2, ErrorMessage = "获取微信用户信息失败")]
|
||||
/// </remarks>
|
||||
public class StringLengthAttribute : ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 最小长度
|
||||
/// </summary>
|
||||
public int MinLength { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 报错提示
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
private int _maxLength;
|
||||
|
||||
public StringLengthAttribute(int maxLength)
|
||||
{
|
||||
this._maxLength = maxLength;
|
||||
}
|
||||
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
if (oValue == null || string.IsNullOrWhiteSpace(oValue.ToString()) || oValue.ToString().Length > this._maxLength || oValue.ToString().Length < this.MinLength)
|
||||
{
|
||||
return ErrorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串最大长度
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2020-12-9
|
||||
/// 使用格式:实体属性前加 [StringMaxLength(200, ErrorMessage = "备注说明最大长度200个字符")]
|
||||
/// </remarks>
|
||||
public class StringMaxLengthAttribute : ValidateAttribute
|
||||
{
|
||||
private int _MaxLength = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 报错提示
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public StringMaxLengthAttribute(int maxLength)
|
||||
{
|
||||
_MaxLength = maxLength;
|
||||
}
|
||||
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
if (oValue != null && oValue.ToString().Length > _MaxLength)
|
||||
{
|
||||
return ErrorMessage;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 必填项
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-11
|
||||
/// [StringRequired(ErrorMessage ="状态不能为空")]
|
||||
/// </remarks>
|
||||
public class StringRequiredAttribute : ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 报错提示信息
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public override string Validate(object oValue)
|
||||
{
|
||||
if (oValue == null || string.IsNullOrWhiteSpace(oValue.ToString()))
|
||||
{
|
||||
return ErrorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Appointment.Entity.ValidateAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 特性基类
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Add:yuangb 2019-3-5
|
||||
/// </remarks>
|
||||
public abstract class ValidateAttribute : Attribute
|
||||
{
|
||||
public abstract string Validate(object oValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user