Browse Source

新增属性加密方式
酒店资料 加密字段编辑

LEIYI 3 months ago
parent
commit
eb1e5752b4

+ 25 - 1
OASystem/OASystem.Api/Controllers/ResourceController.cs

@@ -5,6 +5,7 @@ using EyeSoft.Reflection;
 using NPOI.SS.Formula.Functions;
 using OASystem.API.OAMethodLib;
 using OASystem.API.OAMethodLib.QiYeWeChatAPI.AppNotice;
+using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Dtos.CRM;
 using OASystem.Domain.Dtos.Groups;
 using OASystem.Domain.Dtos.PersonnelModule;
@@ -14,6 +15,7 @@ using OASystem.Domain.ViewModels.QiYeWeChat;
 using OASystem.Infrastructure.Repositories.Groups;
 using Org.BouncyCastle.Utilities.Encoders;
 using System.Data;
+using XAct.Users;
 using static OASystem.API.OAMethodLib.JWTHelper;
 using static OpenAI.GPT3.ObjectModels.SharedModels.IOpenAiModels;
 
@@ -811,11 +813,33 @@ namespace OASystem.API.Controllers
 
             if (dto.PortType == 1)
             {
-                List<HotelDataItemView> HotelDataData = await _sqlSugar.SqlQueryable<HotelDataItemView>(sql).ToListAsync();
+                var HotelDataData = await _sqlSugar.SqlQueryable<HotelDataItemView>(sql)
+                    //.Select(x => new {
+                    //    x.Row_Number,
+                    //    x.Id,
+                    //    City = AesEncryptionHelper.Decrypt(x.City),
+                    //    Name = AesEncryptionHelper.Decrypt(x.Name),
+                    //    Tel = AesEncryptionHelper.Decrypt(x.Tel),
+                    //    Fax = AesEncryptionHelper.Decrypt(x.Fax),
+                    //    Contact = AesEncryptionHelper.Decrypt(x.Contact),
+                    //    x.CreateUserName,
+                    //    ContactPhone = AesEncryptionHelper.Decrypt(x.ContactPhone),
+                    //    x.CreateTime
+                    //})
+                    .ToListAsync();
+
+
+
+
+
                 if (HotelDataData.Count == 0)
                 {
                     return Ok(JsonView(false, "暂无数据"));
                 }
+                foreach (var item in HotelDataData)
+                {
+                    EncryptionProcessor.DecryptProperties(item);
+                }
                 return Ok(JsonView(true, "操作成功", HotelDataData));
             }
             else if (dto.PortType == 2 || dto.PortType == 3)

+ 3 - 3
OASystem/OASystem.Infrastructure/Tools/AesEncryptionHelper.cs

@@ -1,11 +1,11 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
-using System.Security.Cryptography;
 
-namespace OASystem.Infrastructure.Tools
+namespace OASystem.Domain.AesEncryption
 {
     /// <summary>
     /// Aes加密帮助类
@@ -13,7 +13,7 @@ namespace OASystem.Infrastructure.Tools
     public static class AesEncryptionHelper
     {
         private readonly static string Key = "FANMEIGUOJIKEY01"; // 16 字节密钥
-        private readonly static string IV = "FANMEIGUOJIIVV01";    // 16 字节向量
+        private readonly static string IV = "FANMEIGUOJIIVV01";  // 16 字节向量
 
         /// <summary>
         /// 加密

+ 81 - 0
OASystem/OASystem.Domain/AesEncryption/EncryptionProcessor.cs

@@ -0,0 +1,81 @@
+using OASystem.Domain.Attributes;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OASystem.Domain.AesEncryption
+{
+    public static class EncryptionProcessor
+    {
+       
+        /// <summary>
+        /// aes 加密
+        /// </summary>
+        /// <param name="obj"></param>
+        public static void EncryptProperties(object obj)
+        {
+            if (obj == null) return;
+
+            var properties = obj.GetType()
+                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
+                .Where(p => p.IsDefined(typeof(EncryptedAttribute), false) && p.CanWrite && p.CanRead);
+
+            foreach (var property in properties)
+            {
+                var value = property.GetValue(obj) as string;
+                if (!string.IsNullOrEmpty(value))
+                {
+                    var encryptedValue = AesEncryptionHelper.Encrypt(value);
+                    property.SetValue(obj, encryptedValue);
+                }
+            }
+        }
+
+        /// <summary>
+        /// aes 解密
+        /// </summary>
+        /// <param name="obj"></param>
+        public static void DecryptProperties(object obj)
+        {
+            if (obj == null) return;
+
+            var properties = obj.GetType()
+                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
+                .Where(p => p.IsDefined(typeof(EncryptedAttribute), false) && p.CanWrite && p.CanRead);
+
+            foreach (var property in properties)
+            {
+                var value = property.GetValue(obj) as string;
+                if (!string.IsNullOrEmpty(value) && IsEncrypted(value))
+                {
+                    var decryptedValue = AesEncryptionHelper.Decrypt(value);
+                    property.SetValue(obj, decryptedValue);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 验证字符串是否是有效的加密格式
+        /// </summary>
+        /// <param name="value"></param>
+        /// <returns></returns>
+        public static bool IsEncrypted(string value)
+        {
+            try
+            {
+                // 尝试从 Base64 解码
+                var bytes = Convert.FromBase64String(value);
+
+                // AES 加密后的数据通常会是 16 字节的倍数
+                return bytes.Length % 16 == 0;
+            }
+            catch
+            {
+                return false; // 如果 Base64 解码失败,说明字段未加密
+            }
+        }
+    }
+}

+ 16 - 0
OASystem/OASystem.Domain/Attributes/EncryptedAttribute.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OASystem.Domain.Attributes
+{
+    /// <summary>
+    /// Aes 加密
+    /// </summary>
+    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
+    public class EncryptedAttribute : Attribute
+    {
+    }
+}

+ 3 - 1
OASystem/OASystem.Domain/Entities/Groups/Grp_VisaCommission.cs

@@ -1,4 +1,5 @@
-using System;
+using OASystem.Domain.Attributes;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -35,6 +36,7 @@ namespace OASystem.Domain.Entities.Groups
         /// <summary>
         /// 数量
         /// </summary>
+        
         [SugarColumn(IsNullable = true, ColumnDataType = "int")]
         public int Quantity { get; set; }
     }

+ 11 - 1
OASystem/OASystem.Domain/Entities/Resource/Res_HotelData.cs

@@ -1,4 +1,5 @@
-using System;
+using OASystem.Domain.Attributes;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -15,46 +16,55 @@ namespace OASystem.Domain.Entities.Resource
         /// <summary>
         /// 所在城市
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(100)")]
         public string City { get; set; }
         /// <summary>
         /// 酒店名称
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(100)")]
         public string Name { get; set; }
         /// <summary>
         /// 酒店星级
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(100)")]
         public string Level { get; set; }
         /// <summary>
         /// 酒店地址
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(225)")]
         public string Address { get; set; }
         /// <summary>
         /// 酒店电话
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(50)")]
         public string Tel { get; set; }
         /// <summary>
         /// 酒店传真
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(50)")]
         public string Fax { get; set; }
         /// <summary>
         /// 联系人
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(50)")]
         public string Contact { get; set; }
         /// <summary>
         /// 联系方式
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(20)")]
         public string ContactPhone { get; set; }
         /// <summary>
         /// 酒店其他信息
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType = "varchar(500)")]
         public string OtherInformation { get; set; }
     }

+ 3 - 1
OASystem/OASystem.Domain/Entities/System/Sys_Users.cs

@@ -1,4 +1,5 @@
-using OASystem.Domain.Entities;
+using OASystem.Domain.Attributes;
+using OASystem.Domain.Entities;
 using SqlSugar;
 namespace OASystem.Domain.Entities.System
 {
@@ -96,6 +97,7 @@ namespace OASystem.Domain.Entities.System
         /// <summary>
         /// 身份证号码
         /// </summary>
+        [Encrypted]
         [SugarColumn(IsNullable = true, ColumnDataType ="varchar(20)")]
         public string IDCard { get; set; }
         /// <summary>

+ 19 - 1
OASystem/OASystem.Domain/ViewModels/Resource/HotelDataView.cs

@@ -1,4 +1,5 @@
-using OASystem.Domain.Entities.Resource;
+using OASystem.Domain.Attributes;
+using OASystem.Domain.Entities.Resource;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -25,22 +26,32 @@ namespace OASystem.Domain.ViewModels.Resource
     {
         public int id { get; set; }
 
+        [Encrypted]
         public string city { get; set; }
 
+        [Encrypted]
         public string name { get; set; }
 
+        [Encrypted]
         public string tel { get; set; }
 
+        [Encrypted]
         public string fax { get; set; }
 
+        [Encrypted]
         public string contact { get; set; }
 
+        [Encrypted]
         public string contactPhone { get; set; }
 
+        [Encrypted]
         public string level { get; set; }
+        [Encrypted]
         public string address { get; set; }
 
+        [Encrypted]
         public string otherInformation { get; set; }
+
         public string remark { get; set; }
 
     }
@@ -51,20 +62,27 @@ namespace OASystem.Domain.ViewModels.Resource
 
         public int Id { get; set; }
 
+        [Encrypted]
         public string City { get; set; }
 
+        [Encrypted]
         public string Name { get; set; }
 
+        [Encrypted]
         public string Tel { get; set; }
 
+        [Encrypted]
         public string Fax { get; set; }
 
+        [Encrypted]
         public string Contact { get; set; }
 
         public string CreateUserName { get; set; }
 
+        [Encrypted]
         public string ContactPhone { get; set; }
 
+
         public DateTime CreateTime { get; set; }
     }
 

+ 1 - 0
OASystem/OASystem.Infrastructure/Repositories/Groups/VisaCommissionRepository.cs

@@ -1,4 +1,5 @@
 using AutoMapper;
+using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Dtos.Groups;
 using OASystem.Domain.Entities.Groups;
 using OASystem.Domain.ViewModels.Groups;

+ 11 - 6
OASystem/OASystem.Infrastructure/Repositories/Resource/HotelDataRepository.cs

@@ -1,6 +1,7 @@
 using AutoMapper;
 using MySqlX.XDevAPI.Common;
 using OASystem.Domain;
+using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Dtos.Groups;
 using OASystem.Domain.Dtos.Resource;
 using OASystem.Domain.Entities.Resource;
@@ -10,6 +11,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using XAct.Users;
 using Result = OASystem.Domain.Result;
 
 namespace OASystem.Infrastructure.Repositories.Resource
@@ -48,6 +50,8 @@ WHERE
             if (portType == 1 || portType == 2 || portType == 3)
             {
                 var info = await _sqlSugar.SqlQueryable<HotelDataInfoView>(sql).FirstAsync();
+                
+                EncryptionProcessor.DecryptProperties(info);// 解密属性
                 if (info != null) return new Result() { Code = 0, Msg = "操作成功!",Data = info };
                 return new Result() { Code = -1, Msg = "暂无数据!" };
             }
@@ -70,7 +74,9 @@ WHERE
                 }
                 else//不存在,可添加
                 {
-                    Res_HotelData _HotelDataDto = _mapper.Map<Res_HotelData>(dto);
+                    var _HotelDataDto = _mapper.Map<Res_HotelData>(dto);
+                    
+                    EncryptionProcessor.EncryptProperties(_HotelDataDto);// 加密属性
                     int id = await AddAsyncReturnId(_HotelDataDto);
                     if (id == 0)
                     {
@@ -82,7 +88,7 @@ WHERE
             }
             else if (dto.Status == 2)//修改
             {
-                bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_HotelData
+                var updateInfo = new Res_HotelData
                 {
                     City = dto.City,
                     Name = dto.Name,
@@ -94,7 +100,9 @@ WHERE
                     ContactPhone = dto.ContactPhone,
                     OtherInformation = dto.OtherInformation,
                     Remark = dto.Remark,
-                });
+                };
+                EncryptionProcessor.EncryptProperties(updateInfo);// 加密属性
+                bool res = await UpdateAsync(a => a.Id == dto.Id, a => updateInfo);
                 if (!res)
                 {
                     return result = new Result() { Code = -1, Msg = "修改失败!" };
@@ -106,8 +114,5 @@ WHERE
                 return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
             }
         }
-
-
-
     }
 }