李昊 4 lat temu
rodzic
commit
65889287e3

+ 6 - 3
gx_api/GxPress/Api/GxPress.Api/ExceptionMiddleware.cs

@@ -40,12 +40,15 @@ namespace GxPress.Api
                 if (context.User != null)
                 {
                     var ty = _contextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.Role);
-                    if (ty != null && !ty.Value.Equals("Admin"))
+                    if (ty != null && ty.Value.Equals("Admin"))
                     {
-                        _apiLogService.DataSave(context, 1, 2);
+                        var userId = _contextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier);
+                        if (int.TryParse(userId?.Value, out var accountId))
+                        {
+                            _apiLogService.DataSave(context, 1, accountId);
+                        }
                     }
                     // watch.Stop();
-
                 }
             }
             catch (BusinessException be)

+ 5 - 1
gx_api/GxPress/Service/GxPress.Service.Implement/ApiLogService.cs

@@ -16,6 +16,7 @@ namespace GxPress.Service.Implement
         private readonly IConfiguration _configuration;
 
         private readonly Repository<UserLogin> _repository;
+        private readonly Repository<User> _userRepository;
 
         public ApiLogService(IConfiguration configuration, IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
         {
@@ -25,6 +26,7 @@ namespace GxPress.Service.Implement
             //_connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
             var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
             _repository = new Repository<UserLogin>(database);
+            _userRepository = new Repository<User>(database);
         }
 
         public async void DataSave(HttpContext context, long responseTime, int userId)
@@ -43,7 +45,7 @@ namespace GxPress.Service.Implement
                 var _reader = new StreamReader(context.Request.Body);
                 requestBody = _reader.ReadToEnd();
             }
-            var model=new UserLogin
+            var model = new UserLogin
             {
                 AccessToken = accessToken,
                 AccessTime = DateTime.Now,
@@ -57,6 +59,8 @@ namespace GxPress.Service.Implement
                 UserId = userId
             };
             await _repository.InsertAsync(model);
+            //
+            await _userRepository.UpdateAsync(Q.Where(nameof(User.Id), userId).Set(nameof(User.LoginTime), DateTime.Now));
             //}
         }
     }