41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
| using EntityFrameworkCore.Triggered;
 | |
| using IP2Region.Net.Abstractions;
 | |
| 
 | |
| namespace IRaCIS.Core.Application.Triggers
 | |
| {
 | |
|     public class UserLogTrigger(
 | |
|        ISearcher _searcher) : IBeforeSaveTrigger<UserLog>
 | |
|     {
 | |
| 
 | |
| 
 | |
|         //国家|区域|省份|城市|ISP   缺省的地域信息默认是0
 | |
|         //0|0|0|内网IP|内网IP
 | |
|         // 中国|0|湖北省|武汉市|电信
 | |
|         public async Task BeforeSave(ITriggerContext<UserLog> context, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var userLog = context.Entity;
 | |
| 
 | |
|             if (context.ChangeType == ChangeType.Added)
 | |
|             {
 | |
| 
 | |
|                 var ipinfo = _searcher.Search(userLog.IP);
 | |
| 
 | |
|                 userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
 | |
| 
 | |
| 
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public class UserAddTrigger(IUserInfo _userInfo, IRepository<UserLog> _userLogReposiotry) : IBeforeSaveTrigger<User>
 | |
|     {
 | |
|         public async Task BeforeSave(ITriggerContext<User> context, CancellationToken cancellationToken)
 | |
|         {
 | |
|             var user = context.Entity;
 | |
| 
 | |
|             await _userLogReposiotry.AddAsync(new UserLog() { OptType = UserOptType.AddUser, OptUserId = user.Id, LoginUserId = _userInfo.Id, IP = _userInfo.IP });
 | |
|         }
 | |
|     }
 | |
| }
 |