-
zhangwei
2 天以前 6002efe19de5fbf0ebf4f5192f3d9088f7588439
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
 
using Newtonsoft.Json;
 
namespace Admin.NET.Core;
 
/// <summary>
/// 对象拓展
/// </summary>
[SuppressSniffer]
public static partial class ObjectExtension
{
    /// <summary>
    /// 类型属性列表映射表
    /// </summary>
    private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new();
 
    /// <summary>
    /// 脱敏特性缓存映射表
    /// </summary>
    private static readonly ConcurrentDictionary<PropertyInfo, DataMaskAttribute> AttributeCache = new();
 
    /// <summary>
    /// 判断类型是否实现某个泛型
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="generic">泛型类型</param>
    /// <returns>bool</returns>
    public static bool HasImplementedRawGeneric(this Type type, Type generic)
    {
        // 检查接口类型
        var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
        if (isTheRawGenericType) return true;
 
        // 检查类型
        while (type != null && type != typeof(object))
        {
            isTheRawGenericType = IsTheRawGenericType(type);
            if (isTheRawGenericType) return true;
            type = type.BaseType;
        }
 
        return false;
 
        // 判断逻辑
        bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
    }
 
    /// <summary>
    /// 将字典转化为QueryString格式
    /// </summary>
    /// <param name="dict"></param>
    /// <param name="urlEncode"></param>
    /// <returns></returns>
    public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
    {
        return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
    }
 
    /// <summary>
    /// 将字符串URL编码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string UrlEncode(this string str)
    {
        return string.IsNullOrEmpty(str) ? "" : System.Uri.EscapeDataString(str);
    }
 
    /// <summary>
    /// 对象序列化成Json字符串
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToJson(this object obj)
    {
        var jsonSettings = SetNewtonsoftJsonSetting();
        return JSON.GetJsonSerializer().Serialize(obj, jsonSettings);
    }
 
    private static JsonSerializerSettings SetNewtonsoftJsonSetting()
    {
        JsonSerializerSettings setting = new JsonSerializerSettings();
        setting.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        setting.DateTimeZoneHandling = DateTimeZoneHandling.Local;
        setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
        setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
                                                                      //setting.ContractResolver = new HelErpContractResolver("StartTime", customName);
        return setting;
    }
 
    /// <summary>
    /// Json字符串反序列化成对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="json"></param>
    /// <returns></returns>
    public static T ToObject<T>(this string json)
    {
        return JSON.GetJsonSerializer().Deserialize<T>(json);
    }
 
    /// <summary>
    /// 将object转换为long,若失败则返回0
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static long ParseToLong(this object obj)
    {
        try
        {
            return long.Parse(obj.ToString());
        }
        catch
        {
            return 0L;
        }
    }
 
    /// <summary>
    /// 将object转换为long,若失败则返回指定值
    /// </summary>
    /// <param name="str"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static long ParseToLong(this string str, long defaultValue)
    {
        try
        {
            return long.Parse(str);
        }
        catch
        {
            return defaultValue;
        }
    }
 
    /// <summary>
    /// 将object转换为double,若失败则返回0
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static double ParseToDouble(this object obj)
    {
        try
        {
            return double.Parse(obj.ToString());
        }
        catch
        {
            return 0;
        }
    }
 
    /// <summary>
    /// 将object转换为double,若失败则返回指定值
    /// </summary>
    /// <param name="str"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static double ParseToDouble(this object str, double defaultValue)
    {
        try
        {
            return double.Parse(str.ToString());
        }
        catch
        {
            return defaultValue;
        }
    }
 
    /// <summary>
    /// 将string转换为DateTime,若失败则返回日期最小值
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static DateTime ParseToDateTime(this string str)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return DateTime.MinValue;
            }
            if (str.Contains('-') || str.Contains('/'))
            {
                return DateTime.Parse(str);
            }
            else
            {
                int length = str.Length;
                switch (length)
                {
                    case 4:
                        return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 6:
                        return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 8:
                        return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 10:
                        return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 12:
                        return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 14:
                        return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
 
                    default:
                        return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                }
            }
        }
        catch
        {
            return DateTime.MinValue;
        }
    }
 
    /// <summary>
    /// 将string转换为DateTime,若失败则返回默认值
    /// </summary>
    /// <param name="str"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return defaultValue.GetValueOrDefault();
            }
            if (str.Contains('-') || str.Contains('/'))
            {
                return DateTime.Parse(str);
            }
            else
            {
                int length = str.Length;
                switch (length)
                {
                    case 4:
                        return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 6:
                        return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 8:
                        return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 10:
                        return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 12:
                        return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
 
                    case 14:
                        return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
 
                    default:
                        return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                }
            }
        }
        catch
        {
            return defaultValue.GetValueOrDefault();
        }
    }
 
    /// <summary>
    /// 将 string 时间日期格式转换成字符串 如 {yyyy} => 2024
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ParseToDateTimeForRep(this string str)
    {
        if (string.IsNullOrWhiteSpace(str))
            str = $"{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}";
 
        var date = DateTime.Now;
        var reg = new Regex(@"(\{.+?})");
        var match = reg.Matches(str);
        match.ToList().ForEach(u =>
        {
            var temp = date.ToString(u.ToString().Substring(1, u.Length - 2));
            str = str.Replace(u.ToString(), temp);
        });
        return str;
    }
 
    /// <summary>
    /// 是否有值
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static bool IsNullOrEmpty(this object obj)
    {
        return obj == null || string.IsNullOrEmpty(obj.ToString());
    }
 
    /// <summary>
    /// 字符串掩码
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="mask">掩码符</param>
    /// <returns></returns>
    public static string Mask(this string str, char mask = '*')
    {
        if (string.IsNullOrWhiteSpace(str?.Trim()))
            return str;
 
        str = str.Trim();
        var masks = mask.ToString().PadLeft(4, mask);
        return str.Length switch
        {
            >= 11 => Regex.Replace(str, "(.{3}).*(.{4})", $"$1{masks}$2"),
            10 => Regex.Replace(str, "(.{3}).*(.{3})", $"$1{masks}$2"),
            9 => Regex.Replace(str, "(.{2}).*(.{3})", $"$1{masks}$2"),
            8 => Regex.Replace(str, "(.{2}).*(.{2})", $"$1{masks}$2"),
            7 => Regex.Replace(str, "(.{1}).*(.{2})", $"$1{masks}$2"),
            6 => Regex.Replace(str, "(.{1}).*(.{1})", $"$1{masks}$2"),
            _ => Regex.Replace(str, "(.{1}).*", $"$1{masks}")
        };
    }
 
    /// <summary>
    /// 身份证号掩码
    /// </summary>
    /// <param name="idCard">身份证号</param>
    /// <param name="mask">掩码符</param>
    /// <returns></returns>
    public static string MaskIdCard(this string idCard, char mask = '*')
    {
        if (!idCard.TryValidate(ValidationTypes.IDCard).IsValid) return idCard;
 
        var masks = mask.ToString().PadLeft(8, mask);
        return Regex.Replace(idCard, @"^(.{6})(.*)(.{4})$", $"$1{masks}$3");
    }
 
    /// <summary>
    /// 邮箱掩码
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <param name="mask">掩码符</param>
    /// <returns></returns>
    public static string MaskEmail(this string email, char mask = '*')
    {
        if (!email.TryValidate(ValidationTypes.EmailAddress).IsValid) return email;
 
        var pos = email.IndexOf("@");
        return Mask(email[..pos], mask) + email[pos..];
    }
 
    /// <summary>
    /// 将字符串转为值类型,若没有得到或者错误返回为空
    /// </summary>
    /// <typeparam name="T">指定值类型</typeparam>
    /// <param name="str">传入字符串</param>
    /// <returns>可空值</returns>
    public static T? ParseTo<T>(this string str) where T : struct
    {
        try
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                MethodInfo method = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
                if (method != null)
                {
                    T result = (T)method.Invoke(null, new string[] { str });
                    return result;
                }
            }
        }
        catch
        {
        }
        return null;
    }
 
    /// <summary>
    /// 将字符串转为值类型,若没有得到或者错误返回为空
    /// </summary>
    /// <param name="str">传入字符串</param>
    /// <param name="type">目标类型</param>
    /// <returns>可空值</returns>
    public static object ParseTo(this string str, Type type)
    {
        try
        {
            if (type.Name == "String")
                return str;
 
            if (!string.IsNullOrWhiteSpace(str))
            {
                var _type = type;
                if (type.Name.StartsWith("Nullable"))
                    _type = type.GetGenericArguments()[0];
 
                MethodInfo method = _type.GetMethod("Parse", new Type[] { typeof(string) });
                if (method != null)
                    return method.Invoke(null, new string[] { str });
            }
        }
        catch
        {
        }
        return null;
    }
 
    /// <summary>
    /// 将一个对象属性值赋给另一个指定对象属性, 只复制相同属性的
    /// </summary>
    /// <param name="src">原数据对象</param>
    /// <param name="target">目标数据对象</param>
    /// <param name="changeProperties">属性集,键为原属性,值为目标属性</param>
    /// <param name="unChangeProperties">属性集,目标不修改的属性</param>
    public static void CopyTo(object src, object target, Dictionary<string, string> changeProperties = null, string[] unChangeProperties = null)
    {
        if (src == null || target == null)
            throw new ArgumentException("src == null || target == null ");
 
        var SourceType = src.GetType();
        var TargetType = target.GetType();
 
        if (changeProperties == null || changeProperties.Count == 0)
        {
            var fields = TargetType.GetProperties();
            changeProperties = fields.Select(m => m.Name).ToDictionary(m => m);
        }
 
        if (unChangeProperties == null || unChangeProperties.Length == 0)
        {
            foreach (var item in changeProperties)
            {
                var srcProperty = SourceType.GetProperty(item.Key);
                if (srcProperty != null)
                {
                    var sourceVal = srcProperty.GetValue(src, null);
 
                    var tarProperty = TargetType.GetProperty(item.Value);
                    tarProperty?.SetValue(target, sourceVal, null);
                }
            }
        }
        else
        {
            foreach (var item in changeProperties)
            {
                if (!unChangeProperties.Any(m => m == item.Value))
                {
                    var srcProperty = SourceType.GetProperty(item.Key);
                    if (srcProperty != null)
                    {
                        var sourceVal = srcProperty.GetValue(src, null);
 
                        var tarProperty = TargetType.GetProperty(item.Value);
                        tarProperty?.SetValue(target, sourceVal, null);
                    }
                }
            }
        }
    }
 
    /// <summary>
    /// 深复制
    /// </summary>
    /// <typeparam name="T">深复制源对象</typeparam>
    /// <param name="obj">对象</param>
    /// <returns></returns>
    public static T DeepCopy<T>(this T obj)
    {
        var jsonSettings = SetNewtonsoftJsonSetting();
        var json = JSON.Serialize(obj, jsonSettings);
        return JSON.Deserialize<T>(json);
    }
 
    /// <summary>
    /// 对带有<see cref="DataMaskAttribute"/>特性字段进行脱敏处理
    /// </summary>
    public static T MaskSensitiveData<T>(this T obj) where T : class
    {
        if (obj == null) return null;
 
        var type = typeof(T);
 
        // 获取或缓存属性集合
        var properties = PropertyCache.GetOrAdd(type, t =>
            t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => p.PropertyType == typeof(string) && p.GetCustomAttribute<DataMaskAttribute>() != null)
                .ToArray());
 
        // 并行处理可写属性
        Parallel.ForEach(properties, prop =>
        {
            if (!prop.CanWrite) return;
 
            // 获取或缓存特性
            var maskAttr = AttributeCache.GetOrAdd(prop, p => p.GetCustomAttribute<DataMaskAttribute>());
 
            if (maskAttr == null) return;
 
            // 处理非空字符串
            if (prop.GetValue(obj) is string { Length: > 0 } value)
            {
                prop.SetValue(obj, maskAttr.Mask(value));
            }
        });
 
        return obj;
    }
}