diff --git a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs index adb7c426..f42b28ae 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Text; using System.Threading.Tasks; +using JNPF.Common.Const; +using JNPF.Common.Security; using SqlSugar; namespace Tnb.WarehouseMgr.Entities.Exceptions @@ -17,28 +20,37 @@ namespace Tnb.WarehouseMgr.Entities.Exceptions public string UserName { get; set; } public string RequestURL { get; set; } public string RequestMethod { get; set; } - public ConnectionConfigOptions options { get; set; } + public ClaimsPrincipal UserIdentity { get; set; } + public ConnectionConfigOptions? options { get; set; } public TimedTaskException(string message, - string method, - string userId, - string userName, string requestUrl, string requestMethod, - ConnectionConfigOptions options, + ClaimsPrincipal userIdentity, Exception innerException) : base(message, innerException) { - this.Method = method; - this.UserId = userId; - this.UserName = userName; + this.UserId = userIdentity.FindFirst(ClaimConst.CLAINMUSERID)?.Value ?? string.Empty; + this.UserName = userIdentity.FindFirst(ClaimConst.CLAINMREALNAME)?.Value ?? string.Empty; + this.options = userIdentity.FindFirst(ClaimConst.CONNECTIONCONFIG)?.ToObject() ?? null; this.RequestURL = requestUrl; this.RequestMethod = requestMethod; - this.options = options; } } public static class TimedTaskExceptionExtensions { + public static TimedTaskException ToTimedTaskException(this Exception exception, ErrorInfo exceptionInfo) + { + TimedTaskException ex = new(exception.Message, exceptionInfo.RequestURL, exceptionInfo.RequestMethod, exceptionInfo.userIdentity, exception); + return ex; + } + } + + public class ErrorInfo + { + public string RequestURL { get; set; } + public string RequestMethod { get; set; } + public ClaimsPrincipal userIdentity { get; set; } } } diff --git a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs index 6b7c22a3..8eb61c17 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs @@ -15,6 +15,7 @@ using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Message.Interfaces.Message; using JNPF.Systems.Entitys.System; +using Mapster; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -35,7 +36,7 @@ namespace Tnb.WarehouseMgr protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() => { - //_eventPublisher = App.GetRequiredService(); + _eventPublisher = App.GetRequiredService(); List toUserIds = new List() { "25398501929509" }; //生成任务执行 CancellationTokenSource genTaskCTS = new(); @@ -64,19 +65,21 @@ namespace Tnb.WarehouseMgr { await action(cts).Catch(async ex => { - if (ex is TimedTaskException timedTaskEx) + if (ex is TimedTaskException timedTaskEx and not null) { - //await _eventPublisher.PublishAsync(new LogEventSource("Log:CreateExLog", options, new SysLogEntity - //{ - // Id = SnowflakeIdHelper.NextId(), - // Category = 4, - // IPAddress = NetHelper.Ip, - // RequestURL = httpRequest.Path, - // RequestMethod = httpRequest.Method, - // Json = timedTaskEx + "\n" + context.Exception.StackTrace + "\n" + context.Exception.TargetSite.GetParameters().ToString(), - // PlatForm = string.Format("{0}-{1}", userAgent.OS.ToString(), userAgent.RawValue), - // CreatorTime = DateTime.Now - //})); + await _eventPublisher.PublishAsync(new LogEventSource("Log:CreateExLog", timedTaskEx.options!, new SysLogEntity + { + Id = SnowflakeIdHelper.NextId(), + Category = 4, + UserId = timedTaskEx.UserId, + UserName = timedTaskEx.UserName, + IPAddress = NetHelper.Ip, + RequestURL = timedTaskEx.RequestURL, + RequestMethod = timedTaskEx.RequestMethod, + Json = timedTaskEx + "\n" + timedTaskEx.InnerException?.StackTrace + "\n" + timedTaskEx?.TargetSite?.GetParameters().ToString(), + //PlatForm = string.Format("{0}-{1}", userAgent.OS.ToString(), userAgent.RawValue), + CreatorTime = DateTime.Now + })); } }); await Task.Delay(1000); diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs index 64df0065..dbef621c 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs @@ -9,13 +9,16 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.Encodings.Web; using System.Threading.Tasks; +using Aliyun.Credentials.Http; using Aop.Api.Domain; using Aspose.Cells.Drawing; using JNPF; +using JNPF.Common.Const; using JNPF.Common.Contracts; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Extension; +using JNPF.Common.Manager; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; @@ -59,12 +62,15 @@ namespace Tnb.WarehouseMgr private readonly IDictionaryDataService _dictionaryDataService; private readonly IBillRullService _billRullService; private readonly IUserManager _userManager; - public WareHouseService(ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService, IBillRullService billRullService, IUserManager userManager) + private readonly ICacheManager _cacheManager; + + public WareHouseService(ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService, IBillRullService billRullService, IUserManager userManager, ICacheManager cacheManager) { _db = repository.AsSugarClient(); _dictionaryDataService = dictionaryDataService; _billRullService = billRullService; _userManager = userManager; + _cacheManager = cacheManager; } /// @@ -221,6 +227,12 @@ namespace Tnb.WarehouseMgr { Stopwatch sw = Stopwatch.StartNew(); CancellationTokenSource agvCts = new(); + + //获取用户登录令牌 + var aToken = await _cacheManager.GetAsync("AsscessToken"); + if (aToken.IsNullOrWhiteSpace()) return; + var curUser = await GetUserIdentity(aToken); + var db = _db.CopyNew(); try { @@ -366,9 +378,18 @@ namespace Tnb.WarehouseMgr catch (Exception ex) { Log.Error("生成预任务执行时出现错误", ex); + var opts = curUser.FindFirst(ClaimConst.CONNECTIONCONFIG)?.Value; + + ErrorInfo ei = new() + { + RequestURL = App.HttpContext?.Request?.Path, + RequestMethod = App.HttpContext?.Request?.Method, + userIdentity = curUser, + }; + var timedTaskEx = ex.ToTimedTaskException(ei); await db.Ado.RollbackTranAsync(); cts?.Cancel(); - throw; + throw timedTaskEx; } finally {