From 1fef5509d132ca6669dbbabfe85f079c2457460b Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 7 Apr 2025 15:24:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E6=9E=90IP?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E4=BD=8D=E7=BD=AE=E7=9A=84=E5=BA=93=20?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=BC=82=E5=9C=B0=E7=99=BB=E5=BD=95=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/IPCityHelper.cs | 42 +++++++++++++++++++ .../Management/DTO/UserLogViewModel.cs | 2 +- .../Service/Management/UserService.cs | 36 ++++++++-------- IRaCIS.Core.Application/TestService.cs | 26 +----------- .../BeforeSaveTrigger/UserLogTrigger.cs | 7 +++- 5 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 IRaCIS.Core.Application/Helper/IPCityHelper.cs diff --git a/IRaCIS.Core.Application/Helper/IPCityHelper.cs b/IRaCIS.Core.Application/Helper/IPCityHelper.cs new file mode 100644 index 000000000..46a91a642 --- /dev/null +++ b/IRaCIS.Core.Application/Helper/IPCityHelper.cs @@ -0,0 +1,42 @@ +using IRaCIS.Core.Domain.Models; +using MaxMind.GeoIP2; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Helper +{ + public static class IPCityHelper + { + + public static string GetCityResponse(string ip) + { + var path = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "GeoLite2-City.mmdb"); + + try + { + using (var reader = new DatabaseReader(path)) + { + + var city = reader.City(ip); + + //Console.WriteLine(city.Country.IsoCode); // 'US' 'CN' + //Console.WriteLine(city.Country.Name); // 'United States' 'China' + ////Console.WriteLine(city.Country.Names["zh-CN"]); // '美国' + //Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei' + //Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan' + + return $"{city.Country.Name} | {city.MostSpecificSubdivision.Name} | {city.City.Name}"; + + } + } + catch (Exception ex) + { + + return $"UN | UN | {ip}"; + } + } + } +} diff --git a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs index 3e8c4f01e..a0784541e 100644 --- a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs +++ b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs @@ -48,7 +48,7 @@ namespace IRaCIS.Core.Application.ViewModel { public Guid? TrialId { get; set; } - public List OptTypeList { get; set; } + public List? OptTypeList { get; set; } public string? IP { get; set; } diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index cbca0e14a..4aedcfe5e 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -850,7 +850,7 @@ namespace IRaCIS.Core.Application.Service .WhereIf(inQuery.TargetIdentityUserId != null, t => t.TargetIdentityUserId == inQuery.TargetIdentityUserId) .WhereIf(inQuery.TrialId != null, t => t.ActionIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId) || t.TargetIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId)) .WhereIf(trialCreateTime != null, t => t.CreateTime >= trialCreateTime) - .WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count>0, t => inQuery.OptTypeList.Contains(t.OptType)) + .WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count > 0, t => inQuery.OptTypeList.Contains(t.OptType)) .WhereIf(inQuery.BeginDate != null, t => t.CreateTime >= inQuery.BeginDate) .WhereIf(inQuery.EndDate != null, t => t.CreateTime <= inQuery.EndDate) .WhereIf(inQuery.IsLoginUncommonly != null, t => t.IsLoginUncommonly == inQuery.IsLoginUncommonly) @@ -958,29 +958,31 @@ namespace IRaCIS.Core.Application.Service //账号在系统存在 if (isExistAccount || loginUser != null) { - var ipinfo = _searcher.Search(_userInfo.IP); + //var ipinfo = _searcher.Search(_userInfo.IP); - var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + //var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); - string SplitAndConcatenate(string input) + //string SplitAndConcatenate(string input) + //{ + // string[] parts = input.Split('|'); + // return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts); + //} + + var iPRegion = IPCityHelper.GetCityResponse(_userInfo.IP); + + //设置本次登录的IP + await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser() { - string[] parts = input.Split('|'); - return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts); - } + LastLoginIP = iPRegion, + LastLoginTime = DateTime.Now + + }); if (existUserLoginInfo.LastLoginIP != string.Empty) { - //设置上次登录的IP - await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser() - { - LastLoginIP = iPRegion, - LastLoginTime = DateTime.Now - - }); - - // 与上一次区域不一致 - if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion)) + //if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion)) + if (existUserLoginInfo.LastLoginIP != iPRegion) { isLoginUncommonly = true; diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 49176cf72..9053cb529 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -106,34 +106,12 @@ namespace IRaCIS.Core.Application.Service foreach (var ip in ipList) { - try - { - using (var reader = new DatabaseReader(path)) - { + var ipRegion = IPCityHelper.GetCityResponse(ip); - var city = reader.City(ip); + await _userLogRepository.BatchUpdateNoTrackingAsync(t => t.IP == ip, u => new UserLog() { IPRegion = ipRegion }); - Console.WriteLine(city.Country.IsoCode); // 'US' 'CN' - Console.WriteLine(city.Country.Name); // 'United States' 'China' - //Console.WriteLine(city.Country.Names["zh-CN"]); // '美国' - Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei' - Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan' - - - Console.WriteLine("----------------"); - } - } - catch (Exception ex) - { - - Console.WriteLine($"{ip}在数据库无法解析 {ex.Message}"); // 44.9733 - } - } - - - return ResponseOutput.Ok(); } diff --git a/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs b/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs index ca7dd488a..17147b556 100644 --- a/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs +++ b/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs @@ -1,5 +1,7 @@ using EntityFrameworkCore.Triggered; using IP2Region.Net.Abstractions; +using IRaCIS.Core.Application.Helper; +using MaxMind.GeoIP2; namespace IRaCIS.Core.Application.Triggers { @@ -18,10 +20,11 @@ namespace IRaCIS.Core.Application.Triggers if (context.ChangeType == ChangeType.Added) { - var ipinfo = _searcher.Search(userLog.IP); + //var ipinfo = _searcher.Search(userLog.IP); - userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + //userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + userLog.IPRegion = IPCityHelper.GetCityResponse(userLog.IP); } } From c68004b804c5f03e17f3fd98e2750e7081a472e3 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 7 Apr 2025 15:45:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=8C=BA=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IRaCIS.Core.Application.xml | 7 +++++++ .../Service/Management/UserService.cs | 2 +- IRaCIS.Core.Application/TestService.cs | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 0917362b4..c31bc7f07 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -1449,6 +1449,13 @@ + + + 复制文件或者文件夹 + + + + 获取历史版本 diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 4aedcfe5e..913a1823f 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -1021,7 +1021,7 @@ namespace IRaCIS.Core.Application.Service } - + //登录成功的,才会算异地登录 if (loginUser.Status == 0) { await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, ActionIdentityUserId = loginUser.IdentityUserId, ActionUserName = loginUser.UserName, OptType = UserOptType.LoginLockedAccount, IsLoginUncommonly = isLoginUncommonly }, true); diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 9053cb529..a0909f0e2 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -97,7 +97,7 @@ namespace IRaCIS.Core.Application.Service } [AllowAnonymous] - public async Task NewIP([FromServices] IRepository _userLogRepository) + public async Task NewIP([FromServices] IRepository _userLogRepository, [FromServices] IRepository _identityUserRepository) { var path = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "GeoLite2-City.mmdb"); @@ -112,6 +112,20 @@ namespace IRaCIS.Core.Application.Service } + + #region 用户表最后一次登录的ipRegion 需要维护 + + var userList = _identityUserRepository.Where(t => t.LastLoginIP != "").Select(t => new { t.Id, t.LastLoginIP, t.UpdateUserId }).ToList(); + + foreach (var user in userList) + { + var lastLoginRegion = _userLogRepository.Where(t => t.ActionIdentityUserId == user.Id && (t.OptType == UserOptType.Login || t.OptType == UserOptType.LoginLockedAccount)).OrderByDescending(t => t.CreateTime).Select(t => t.IPRegion).FirstOrDefault()??string.Empty; + + await _identityUserRepository.BatchUpdateNoTrackingAsync(t => t.Id == user.Id, u => new Domain.Models.IdentityUser() { LastLoginIP = lastLoginRegion, UpdateUserId = user.UpdateUserId }); + } + + #endregion + return ResponseOutput.Ok(); }