Pārlūkot izejas kodu

市场部客户资料 --> 操作记录code调整

yuanrf 3 mēneši atpakaļ
vecāks
revīzija
5139b92dd0

+ 49 - 37
OASystem/OASystem.Api/Middlewares/RecordAPIOperationMiddleware.cs

@@ -1,19 +1,9 @@
-using AlibabaCloud.OpenApiClient.Models;
-using Aspose.Words;
-using NPOI.HSSF.Record;
-using OASystem.API.OAMethodLib;
+using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Attributes;
 using OASystem.Domain.Entities.Customer;
-using OASystem.Infrastructure.Repositories.CRM;
-using System.ComponentModel.DataAnnotations;
-using System.Diagnostics;
-using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
-using static OASystem.API.OAMethodLib.JWTHelper;
+using Org.BouncyCastle.Asn1.Pkcs;
 using UAParser;
-using System.Collections.Generic;
-using XAct;
-using NPOI.SS.Formula.Functions;
-using QuzrtzJob.Factory;
+using static OASystem.API.OAMethodLib.JWTHelper;
 
 namespace OASystem.API.Middlewares
 {
@@ -69,12 +59,10 @@ namespace OASystem.API.Middlewares
                         bool exists5 = requestBodyJson.TryGetValue("id", out param5);
                         bool exists6 = requestBodyJson.TryGetValue("status", out param6);
                         bool exists7 = requestBodyJson.TryGetValue("operationUserId", out param7);
-                        bool exists8 = requestBodyJson.TryGetValue("deleteUserId", out param8); 
+                        bool exists8 = requestBodyJson.TryGetValue("deleteUserId", out param8);
 
                         if (exists1) int.TryParse(param1.ToString(), out portType);
 
-                        
-
                         //用户Id处理
                         if (userId < 1)
                         {
@@ -98,7 +86,7 @@ namespace OASystem.API.Middlewares
                         }
                         //编辑前数据查询;
                         if (exists5) param5Bool = int.TryParse(param5.ToString(), out id);
-                        if (exists5) param6Bool = int.TryParse(param6.ToString(), out status);
+                        if (exists6) param6Bool = int.TryParse(param6.ToString(), out status);
 
                         if (param6Bool)
                         {
@@ -112,12 +100,15 @@ namespace OASystem.API.Middlewares
                         }
                         else if (param5Bool)
                         {
-                            //  id > 0 修改 
-                            if (id < 1) apiLogAttribute.OperationEnum = OperationEnum.Add;
-                            else if (id > 0)
+                            if (apiLogAttribute.OperationEnum != OperationEnum.Del)
                             {
-                                apiLogAttribute.OperationEnum = OperationEnum.Edit;
-                                updatePreData = await TableInfoToJson(apiLogAttribute.TableName, id);
+                                //  id > 0 修改 
+                                if (id < 1) apiLogAttribute.OperationEnum = OperationEnum.Add;
+                                else if (id > 0)
+                                {
+                                    apiLogAttribute.OperationEnum = OperationEnum.Edit;
+                                    updatePreData = await TableInfoToJson(apiLogAttribute.TableName, id);
+                                }
                             }
                         }
                     }
@@ -142,7 +133,7 @@ namespace OASystem.API.Middlewares
 
                 // 将响应体内容写回原始响应体流
                 await responseMemoryStream.CopyToAsync(originalResponseBody);
-               
+
                 //修改后数据查询
                 if (param6Bool)
                 {
@@ -151,10 +142,14 @@ namespace OASystem.API.Middlewares
                 }
                 else if (param5Bool)
                 {
-                    //  id > 0 修改 
-                    if (id > 0) updateBefData = await TableInfoToJson(apiLogAttribute.TableName, id);
+
+                    if (apiLogAttribute.OperationEnum != OperationEnum.Del)
+                    {
+                        //  id > 0 修改 
+                        if (id > 0) updateBefData = await TableInfoToJson(apiLogAttribute.TableName, id);
+                    }
                 }
-                string remoteIp = string.Empty, 
+                string remoteIp = string.Empty,
                        location = string.Empty;
 
                 // 检查请求头中的X-Forwarded-For,以获取真实的客户端IP地址
@@ -189,7 +184,7 @@ namespace OASystem.API.Middlewares
                     os = client.OS.Family; // 操作系统名称
 
                     var osVersion = string.Empty; // 操作系统版本
-                    if (!string.IsNullOrEmpty(client.OS.Major)) osVersion += client.OS.Major ;
+                    if (!string.IsNullOrEmpty(client.OS.Major)) osVersion += client.OS.Major;
                     if (!string.IsNullOrEmpty(client.OS.Minor)) osVersion += "." + client.OS.Minor;
                     if (!string.IsNullOrEmpty(client.OS.Patch)) osVersion += "." + client.OS.Patch;
 
@@ -205,7 +200,8 @@ namespace OASystem.API.Middlewares
                 // 计算耗时
                 var duration = (long)(endTime - startTime).TotalMilliseconds;
 
-                var logInfo = new Crm_TableOperationRecord() {
+                var logInfo = new Crm_TableOperationRecord()
+                {
                     TableName = apiLogAttribute.TableName,
                     PortType = portType,
                     OperationItem = apiLogAttribute.OperationEnum,
@@ -213,20 +209,20 @@ namespace OASystem.API.Middlewares
                     RequestUrl = context.Request.Path,
                     RemoteIp = remoteIp,
                     Location = location,
-                    RequestParam =!string.IsNullOrEmpty(requestBodyText) ? JsonConvert.SerializeObject(requestBodyText) : null,
+                    RequestParam = !string.IsNullOrEmpty(requestBodyText) ? JsonConvert.SerializeObject(requestBodyText) : null,
                     ReturnResult = !string.IsNullOrEmpty(responseBodyText) ? JsonConvert.SerializeObject(responseBodyText) : null,
                     Elapsed = duration,
                     Status = context.Response.StatusCode.ToString(),
                     CreateUserId = userId,
                     UpdatePreData = updatePreData,
                     UpdateBefData = updateBefData,
-                    Browser= browser,
+                    Browser = browser,
                     Os = os,
                     DeviceType = deviceType,
                 };
 
                 // 存储到数据库
-                await  _sqlSugar.Insertable(logInfo).ExecuteCommandAsync();
+                await _sqlSugar.Insertable(logInfo).ExecuteCommandAsync();
             }
             else
             {
@@ -234,12 +230,28 @@ namespace OASystem.API.Middlewares
             }
         }
 
-        private async Task<string> TableInfoToJson(string tableName,int id)
+        private async Task<string> TableInfoToJson(string tableName, int id)
         {
             if (_sqlSugar.DbMaintenance.IsAnyTable(tableName))
             {
-                var info = await _sqlSugar.Queryable<dynamic>(tableName).Where("id=@id", new { id = id }).FirstAsync();
-                return JsonConvert.SerializeObject(info);
+                string jsonLabel = string.Empty;
+                if (tableName.Equals("Crm_NewClientData"))
+                {
+                    var info = await _sqlSugar.Queryable<Crm_NewClientData>().FirstAsync(x => x.Id == id);
+                    if (info != null)
+                    {
+                        EncryptionProcessor.DecryptProperties(info);
+                        if (info != null) jsonLabel = JsonConvert.SerializeObject(info);
+                    }
+                }
+                else
+                {
+                    var sql = string.Format(" Select * From {0} Where Id={1}", tableName, id);
+
+                    var info = await _sqlSugar.SqlQueryable<dynamic>(sql).FirstAsync();
+                    if (info != null) jsonLabel = JsonConvert.SerializeObject(info);
+                }
+                return jsonLabel;
             }
 
             return string.Empty;
@@ -292,7 +304,7 @@ namespace OASystem.API.Middlewares
             return 0;
         }
 
-        private async Task<(string ip,string local)> GetIpInfo()
+        private async Task<(string ip, string local)> GetIpInfo()
         {
             string ip = string.Empty, local = string.Empty;
             var response = await _httpClient.GetAsync("https://api.vore.top/api/IPdata");
@@ -306,7 +318,7 @@ namespace OASystem.API.Middlewares
                 local = $"{ipInfo.adcode.o}";
             }
 
-            return (ip,local);
+            return (ip, local);
         }
 
 
@@ -326,6 +338,6 @@ namespace OASystem.API.Middlewares
             return $"{ipInfo.country}, {ipInfo.city}";
         }
 
-       
+
     }
 }