😎代码优化
This commit is contained in:
parent
fd12e5b74b
commit
5b82f75f04
@ -1,7 +1,7 @@
|
|||||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
//
|
//
|
||||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
//
|
//
|
||||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
namespace Admin.NET.Core;
|
namespace Admin.NET.Core;
|
||||||
@ -34,9 +34,10 @@ public class DataMaskAttribute : Attribute
|
|||||||
|
|
||||||
public DataMaskAttribute(int startIndex, int length)
|
public DataMaskAttribute(int startIndex, int length)
|
||||||
{
|
{
|
||||||
if (startIndex < 0) throw new ArgumentOutOfRangeException(nameof(startIndex));
|
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
|
||||||
if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
|
|
||||||
|
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);
|
||||||
|
|
||||||
StartIndex = startIndex;
|
StartIndex = startIndex;
|
||||||
Length = length;
|
Length = length;
|
||||||
}
|
}
|
||||||
@ -52,8 +53,6 @@ public class DataMaskAttribute : Attribute
|
|||||||
var maskedLength = Math.Min(Length, input.Length - StartIndex);
|
var maskedLength = Math.Min(Length, input.Length - StartIndex);
|
||||||
var maskStr = new string(MaskChar, KeepLength ? maskedLength : Math.Min(4, maskedLength));
|
var maskStr = new string(MaskChar, KeepLength ? maskedLength : Math.Min(4, maskedLength));
|
||||||
|
|
||||||
return input.Substring(0, StartIndex) + maskStr +
|
return input.Substring(0, StartIndex) + maskStr + (StartIndex + maskedLength < input.Length ? input.Substring(StartIndex + maskedLength) : "");
|
||||||
(StartIndex + maskedLength < input.Length ?
|
|
||||||
input.Substring(StartIndex + maskedLength) : "");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ public static partial class ObjectExtension
|
|||||||
/// 类型属性列表映射表
|
/// 类型属性列表映射表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new();
|
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 脱敏特性缓存映射表
|
/// 脱敏特性缓存映射表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -291,9 +291,9 @@ public static partial class ObjectExtension
|
|||||||
if (obj == null) return null;
|
if (obj == null) return null;
|
||||||
|
|
||||||
var type = typeof(T);
|
var type = typeof(T);
|
||||||
|
|
||||||
// 获取或缓存属性集合
|
// 获取或缓存属性集合
|
||||||
var properties = PropertyCache.GetOrAdd(type, t =>
|
var properties = PropertyCache.GetOrAdd(type, t =>
|
||||||
t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||||
.Where(p => p.PropertyType == typeof(string) && p.GetCustomAttribute<DataMaskAttribute>() != null)
|
.Where(p => p.PropertyType == typeof(string) && p.GetCustomAttribute<DataMaskAttribute>() != null)
|
||||||
.ToArray());
|
.ToArray());
|
||||||
@ -302,10 +302,10 @@ public static partial class ObjectExtension
|
|||||||
Parallel.ForEach(properties, prop =>
|
Parallel.ForEach(properties, prop =>
|
||||||
{
|
{
|
||||||
if (!prop.CanWrite) return;
|
if (!prop.CanWrite) return;
|
||||||
|
|
||||||
// 获取或缓存特性
|
// 获取或缓存特性
|
||||||
var maskAttr = AttributeCache.GetOrAdd(prop, p => p.GetCustomAttribute<DataMaskAttribute>());
|
var maskAttr = AttributeCache.GetOrAdd(prop, p => p.GetCustomAttribute<DataMaskAttribute>());
|
||||||
|
|
||||||
if (maskAttr == null) return;
|
if (maskAttr == null) return;
|
||||||
|
|
||||||
// 处理非空字符串
|
// 处理非空字符串
|
||||||
|
|||||||
@ -124,10 +124,10 @@ public static class SqlSugarPagedExtensions
|
|||||||
{
|
{
|
||||||
RefAsync<int> total = 0;
|
RefAsync<int> total = 0;
|
||||||
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
|
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
|
||||||
items.ForEach(x => x.MaskSensitiveData());
|
items.ForEach(u => u.MaskSensitiveData());
|
||||||
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
|
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 脱敏分页拓展
|
/// 脱敏分页拓展
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -139,7 +139,7 @@ public static class SqlSugarPagedExtensions
|
|||||||
{
|
{
|
||||||
var total = list.Count();
|
var total = list.Count();
|
||||||
var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
||||||
items.ForEach(x => x.MaskSensitiveData());
|
items.ForEach(u => u.MaskSensitiveData());
|
||||||
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
|
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user