田爽 10 ماه پیش
والد
کامیت
16ad406a53

+ 14 - 5
src/Hotline.Api/Controllers/IdentityController.cs

@@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
 using System.Security.Cryptography;
 using System.Text;
+using Hotline.Api.Filter;
+using Hotline.Application.Systems;
 using XC.RSAUtil;
 using XF.Domain.Constants;
 using XF.Domain.Exceptions;
@@ -15,7 +17,9 @@ public class IdentityController : BaseController
 {
     private readonly IIdentityAppService _identityAppService;
     private readonly ISystemSettingCacheManager _systemSettingCacheManager;
-    private const string PublicKey = @"-----BEGIN PUBLIC KEY-----
+    private readonly ISystemLogApplication _iSystemLogApplication;
+
+	private const string PublicKey = @"-----BEGIN PUBLIC KEY-----
 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgw+/x6IQPkH0A4eoF63j
 kLThsOXWyNBdcL9LATGy/G1yTHOr1RyKJB//iNug+V8DIoIHuFTlhgLHDbSqxvRW
 MONxIIF289riS6bDI4Ox/pFmOfmElFRk0lKGihaTE2Aefd6g/N+RfLLaHWztY+/v
@@ -52,11 +56,12 @@ Q9PP8NTEmKqdI3WVFYqW/OlOFC6sjiscTOOn9Tc5Mrcn8ocCjAPjkhkCCVRMiJnv
 jxrWXHbT1FB6DqkdOnBbQqS1Azqz5HxLlSyEK3F60e3SgB5iZsDZ
 -----END RSA PRIVATE KEY-----";
 
-    public IdentityController(IIdentityAppService identityAppService, ISystemSettingCacheManager systemSettingCacheManager)
+    public IdentityController(IIdentityAppService identityAppService, ISystemSettingCacheManager systemSettingCacheManager, ISystemLogApplication iSystemLogApplication)
     {
         _identityAppService = identityAppService;
         _systemSettingCacheManager = systemSettingCacheManager;
-    }
+        _iSystemLogApplication = iSystemLogApplication;
+	}
 
     /// <summary>
     /// 登录
@@ -65,10 +70,14 @@ jxrWXHbT1FB6DqkdOnBbQqS1Azqz5HxLlSyEK3F60e3SgB5iZsDZ
     /// <returns></returns>
     [AllowAnonymous]
     [HttpPost("login")]
-    public async Task<string> Login([FromBody] LoginDto dto)
+    [LogFilter("",false)]
+	public async Task<string> Login([FromBody] LoginDto dto)
     {
         dto = Decrypt(dto);
-        return await _identityAppService.LoginAsync(dto, HttpContext.RequestAborted);
+        var res = await _identityAppService.LoginAsync(dto, HttpContext.RequestAborted);
+        dto.Password = string.Empty;
+        await _iSystemLogApplication.AddLog("账号登录", res, dto, HttpContext,dto.Username);
+		return res ;
     }
 
     [AllowAnonymous]

+ 14 - 0
src/Hotline.Application/Systems/ISystemLogApplication.cs

@@ -0,0 +1,14 @@
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hotline.Application.Systems
+{
+	public interface ISystemLogApplication
+	{
+		Task AddLog<T>(string name, string res, T entity, HttpContext context,string crName ="");
+	}
+}

+ 47 - 0
src/Hotline.Application/Systems/SystemLogApplication.cs

@@ -0,0 +1,47 @@
+using Hotline.Settings;
+using Hotline.Tools;
+using MapsterMapper;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+using Org.BouncyCastle.Asn1.Ocsp;
+using System.Text;
+using XF.Domain.Dependency;
+using XF.Domain.Repository;
+
+namespace Hotline.Application.Systems
+{
+	public class SystemLogApplication: ISystemLogApplication, IScopeDependency
+	{
+		private readonly IMapper _mapper;
+		private readonly IRepository<SystemLog> _systemLogRepository;
+		public SystemLogApplication(IMapper mapper, IRepository<SystemLog> systemLogRepository) { _mapper = mapper; _systemLogRepository = systemLogRepository; }
+
+		public async Task AddLog<T>(string name, string res, T entity, HttpContext context, string crName ="") {
+			SystemLog log = new SystemLog();
+			log.Name = name;
+			var Method = "";
+			switch (context.Request.Method.ToUpper())
+			{
+				case "GET":
+					Method = "GET";
+					log.ExecuteParam = context.Request.QueryString.Value;
+					break;
+
+				case "POST":
+					Method = "POST";
+					log.ExecuteUrl += context.Request.QueryString.Value;
+					log.ExecuteParam = JsonConvert.SerializeObject(entity);
+					break;
+			}
+			log.ExecuteUrl = Method + "|" + context.Request.Path;
+			log.Status = 1;
+			log.ExecuteResult = JsonConvert.SerializeObject(res);
+			log.IpUrl = context.Connection.RemoteIpAddress.ToString();
+			log.InitId();
+			await  _systemLogRepository.AddAsync(log);
+			if (!string.IsNullOrEmpty(crName))
+				await _systemLogRepository.Updateable().SetColumns(l => new SystemLog() { CreatorName = crName }).Where(l => l.Id == log.Id).ExecuteCommandAsync();
+		}
+	}
+}

+ 4 - 0
src/Hotline.Share/Dtos/Settings/SystemLogDto.cs

@@ -60,6 +60,10 @@ namespace Hotline.Share.Dtos.Settings
 		///</summary>
 		public string? ExecuteUrl { get; set; }
 		/// <summary>
+		/// ip地址 
+		///</summary>
+		public string? IpUrl { get; set; }
+		/// <summary>
 		/// 请求参数 
 		///</summary>
 		public string? ExecuteParam { get; set; }

+ 5 - 0
src/Hotline/Settings/SystemLog.cs

@@ -29,6 +29,11 @@ namespace Hotline.Settings
 		/// 接口地址 
 		///</summary>
 		public string? ExecuteUrl { get; set; }
+
+		/// <summary>
+		/// ip地址 
+		///</summary>
+		public string? IpUrl { get; set; }
 		/// <summary>
 		/// 请求参数 
 		///</summary>