临时数据

This commit is contained in:
2026-07-06 14:27:56 +08:00
54 changed files with 3228 additions and 421 deletions

View File

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Http;
using QYZH.InteractiveMagazine.Infrastructure.Context;
using QYZH.InteractiveMagazine.Models.Entity;
using Microsoft.AspNetCore.Http;
using QYZH.InteractiveMagazine.Infrastructure.Context;
using SqlSugar;
using System.Linq.Expressions;
using System.Reflection;
@ -120,20 +122,14 @@ namespace QYZH.InteractiveMagazine.Repository.Core
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
var entityValue = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)?.ToString();
//var currnetUserName = "";
//var currentNameIdentifier = ServiceContext.GetRequiredService<IHttpContextAccessor>().HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
//var currentUserId = string.IsNullOrWhiteSpace(currentNameIdentifier) ? 0 : long.Parse(currentNameIdentifier);
var currentClaimName = ServiceContext.GetRequiredService<IHttpContextAccessor>().HttpContext?.User?.FindFirst(ClaimTypes.Name)?.Value;
var currentUserName = GetCurrentUserName();
/*** inset生效 ***/
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
if (entityInfo.PropertyName == "CreatedAt" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == DateTime.MinValue.ToString()))
entityInfo.SetValue(DateTime.Now);//修改CreateTime字段
else if (entityInfo.PropertyName == "CreatedBy" && (string.IsNullOrWhiteSpace(entityValue)))
entityInfo.SetValue(currentClaimName);//修改创建人字段
entityInfo.SetValue(currnetUserName);//修改创建人字段
else if (entityInfo.PropertyName == "IsDeleted" && string.IsNullOrWhiteSpace(entityValue))
entityInfo.SetValue("0");//修改CreateTime字段
}
@ -144,7 +140,7 @@ namespace QYZH.InteractiveMagazine.Repository.Core
if (entityInfo.PropertyName == "UpdatedAt" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == DateTime.MinValue.ToString()))
entityInfo.SetValue(DateTime.Now);//修改UpdatedTime字段
else if (entityInfo.PropertyName == "UpdatedBy" && (string.IsNullOrWhiteSpace(entityValue) || entityValue == "0"))
entityInfo.SetValue(currentClaimName);//修改更新人字段
entityInfo.SetValue(currnetUserName);//修改更新人字段
}
};
return db;
@ -152,6 +148,39 @@ namespace QYZH.InteractiveMagazine.Repository.Core
}
private static string GetCurrentUserName()
{
try
{
var httpContextAccessor = ServiceContext.ServiceProvider?.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
var user = httpContextAccessor?.HttpContext?.User;
var userName = user?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value
?? user?.Identity?.Name;
return string.IsNullOrWhiteSpace(userName) ? "System" : userName;
}
catch
{
return "System";
}
}
private static bool ShouldSetOperatorName(string? entityValue, string currentUserName)
{
if (string.IsNullOrWhiteSpace(entityValue) || entityValue == "0")
{
return true;
}
if (currentUserName != "System" && long.TryParse(entityValue, out _))
{
return true;
}
return false;
}
/// <summary>
/// 把一个字符串转成驼峰规则的字符串
/// </summary>