49755ef0
李曜臣
6-12代码优化
|
1
|
using Mapster;
|
59e51671
“wangming”
1
|
2
3
4
5
|
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
|
49755ef0
李曜臣
6-12代码优化
|
6
|
using Volo.Abp.Uow;
|
59e51671
“wangming”
1
|
7
8
9
|
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
49755ef0
李曜臣
6-12代码优化
|
10
11
12
13
14
15
|
namespace Yi.Framework.Rbac.Domain.EventHandlers;
/// <summary>
/// 登录成功后写 LoginLog;写库失败不得影响登录接口(如泰额 host 库无 LoginLog 表)。
/// </summary>
public class LoginEventHandler : ILocalEventHandler<LoginEventArgs>, ITransientDependency
|
59e51671
“wangming”
1
|
16
|
{
|
49755ef0
李曜臣
6-12代码优化
|
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private readonly ILogger<LoginEventHandler> _logger;
private readonly IRepository<LoginLogAggregateRoot> _loginLogRepository;
public LoginEventHandler(
ILogger<LoginEventHandler> logger,
IRepository<LoginLogAggregateRoot> loginLogRepository)
{
_logger = logger;
_loginLogRepository = loginLogRepository;
}
[UnitOfWork(isTransactional: false)]
public async Task HandleEventAsync(LoginEventArgs eventData)
|
59e51671
“wangming”
1
|
30
|
{
|
49755ef0
李曜臣
6-12代码优化
|
31
|
try
|
59e51671
“wangming”
1
|
32
|
{
|
49755ef0
李曜臣
6-12代码优化
|
33
34
|
_logger.LogInformation("用户【{UserId}:{UserName}】登入系统", eventData.UserId, eventData.UserName);
|
59e51671
“wangming”
1
|
35
36
37
38
|
var loginLogEntity = eventData.Adapt<LoginLogAggregateRoot>();
loginLogEntity.LogMsg = eventData.UserName + "登录系统";
loginLogEntity.LoginUser = eventData.UserName;
loginLogEntity.CreatorId = eventData.UserId;
|
49755ef0
李曜臣
6-12代码优化
|
39
40
41
42
43
44
45
46
47
|
await _loginLogRepository.InsertAsync(loginLogEntity, autoSave: true);
}
catch (Exception ex)
{
_logger.LogWarning(
ex,
"LoginLog insert skipped for user {UserId} ({UserName}); current database may not have LoginLog table.",
eventData.UserId,
eventData.UserName);
|
59e51671
“wangming”
1
|
48
49
50
|
}
}
}
|