😎代码优化

This commit is contained in:
zuohuaijun 2025-05-25 23:04:14 +08:00
parent fd12e5b74b
commit 5b82f75f04
3 changed files with 15 additions and 16 deletions

View File

@ -1,7 +1,7 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
@ -34,9 +34,10 @@ public class DataMaskAttribute : Attribute
public DataMaskAttribute(int startIndex, int length)
{
if (startIndex < 0) throw new ArgumentOutOfRangeException(nameof(startIndex));
if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);
StartIndex = startIndex;
Length = length;
}
@ -52,8 +53,6 @@ public class DataMaskAttribute : Attribute
var maskedLength = Math.Min(Length, input.Length - StartIndex);
var maskStr = new string(MaskChar, KeepLength ? maskedLength : Math.Min(4, maskedLength));
return input.Substring(0, StartIndex) + maskStr +
(StartIndex + maskedLength < input.Length ?
input.Substring(StartIndex + maskedLength) : "");
return input.Substring(0, StartIndex) + maskStr + (StartIndex + maskedLength < input.Length ? input.Substring(StartIndex + maskedLength) : "");
}
}

View File

@ -15,7 +15,7 @@ public static partial class ObjectExtension
/// 类型属性列表映射表
/// </summary>
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new();
/// <summary>
/// 脱敏特性缓存映射表
/// </summary>
@ -291,9 +291,9 @@ public static partial class ObjectExtension
if (obj == null) return null;
var type = typeof(T);
// 获取或缓存属性集合
var properties = PropertyCache.GetOrAdd(type, t =>
var properties = PropertyCache.GetOrAdd(type, t =>
t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType == typeof(string) && p.GetCustomAttribute<DataMaskAttribute>() != null)
.ToArray());
@ -302,10 +302,10 @@ public static partial class ObjectExtension
Parallel.ForEach(properties, prop =>
{
if (!prop.CanWrite) return;
// 获取或缓存特性
var maskAttr = AttributeCache.GetOrAdd(prop, p => p.GetCustomAttribute<DataMaskAttribute>());
if (maskAttr == null) return;
// 处理非空字符串

View File

@ -124,10 +124,10 @@ public static class SqlSugarPagedExtensions
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
items.ForEach(x => x.MaskSensitiveData());
items.ForEach(u => u.MaskSensitiveData());
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 脱敏分页拓展
/// </summary>
@ -139,7 +139,7 @@ public static class SqlSugarPagedExtensions
{
var total = list.Count();
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);
}