😎优化系统用户和租户事件订阅

This commit is contained in:
zuohuaijun 2025-01-07 12:45:29 +08:00
parent 57e33bb3e9
commit 57dbfd3e01
10 changed files with 108 additions and 66 deletions

View File

@ -23,7 +23,7 @@
"DocExpansionState": "List", // ListFullNone
"EnableAllGroups": true,
//"ServerDir": "t490", // Nginx Servers
"HideServers": false,
"HideServers": true,
"Servers": [
{
"Url": "https://ip/二级",

View File

@ -0,0 +1,49 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Admin.NET.Core.Service;
using Furion.EventBus;
using Furion.JsonSerialization;
namespace Admin.NET.Application;
/// <summary>
/// 事件订阅
/// </summary>
public class AppEventSubscriber : IEventSubscriber, ISingleton, IDisposable
{
private readonly IServiceScope _serviceScope;
public AppEventSubscriber(IServiceScopeFactory scopeFactory)
{
_serviceScope = scopeFactory.CreateScope();
}
/// <summary>
/// 修改用户
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
[EventSubscribe(UserEventTypeEnum.Update)]
public async Task CreateExLog(EventHandlerExecutingContext context)
{
var addUserInput = context.GetPayload<AddUserInput>();
Console.WriteLine(JSON.Serialize(addUserInput));
await Task.CompletedTask;
//var db = _serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
//await db.CopyNew().Insertable(context.GetPayload<AddUserInput>()).ExecuteCommandAsync();
}
/// <summary>
/// 释放服务作用域
/// </summary>
public void Dispose()
{
_serviceScope.Dispose();
}
}

View File

@ -49,7 +49,7 @@
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1157" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1158" />
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
</ItemGroup>

View File

@ -36,14 +36,4 @@ public class CommonConst
/// 事件-发送异常邮件
/// </summary>
public const string SendErrorMail = "Send:ErrorMail";
/// <summary>
/// 事件-增加租户
/// </summary>
public const string InitTenant = "Add:Tenant";
/// <summary>
/// 事件-删除租户
/// </summary>
public const string DeleteTenant = "Delete:Tenant";
}

View File

@ -4,29 +4,29 @@
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core.Service;
namespace Admin.NET.Core;
/// <summary>
/// 系统用户事件处理
/// 事件类型-系统租户操作枚举
/// </summary>
public class SysUserEventHandler : ISingleton
[Description("事件类型-系统租户操作枚举")]
public enum TenantEventTypeEnum
{
private event EventHandler Event;
/// <summary>
/// 增加租户
/// </summary>
[Description("增加租户")]
Add = 0,
/// <summary>
/// 订阅
/// 更新租户
/// </summary>
/// <param name="eventHandler"></param>
public void Subscribe(EventHandler eventHandler) => Event += eventHandler;
[Description("更新租户")]
Update = 1,
/// <summary>
/// 发布事件
/// 删除租户
/// </summary>
/// <param name="sender"></param>
/// <param name="eventType"></param>
/// <param name="input"></param>
public void OnEvent(object sender, SysUserEventTypeEnum eventType, object input)
{
Event?.Invoke(sender, new SysUserEventArgs(eventType, input));
}
[Description("删除租户")]
Delete = 2,
}

View File

@ -10,53 +10,53 @@ namespace Admin.NET.Core;
/// 事件类型-系统用户操作枚举
/// </summary>
[Description("事件类型-系统用户操作枚举")]
public enum SysUserEventTypeEnum
public enum UserEventTypeEnum
{
/// <summary>
/// 增加用户
/// </summary>
[Description("增加用户")]
Add = 111,
Add = 0,
/// <summary>
/// 更新用户
/// </summary>
[Description("更新用户")]
Update = 222,
/// <summary>
/// 授权用户角色
/// </summary>
[Description("授权用户角色")]
UpdateRole = 333,
Update = 1,
/// <summary>
/// 删除用户
/// </summary>
[Description("删除用户")]
Delete = 444,
Delete = 2,
/// <summary>
/// 授权用户角色
/// </summary>
[Description("授权用户角色")]
UpdateRole = 3,
/// <summary>
/// 设置用户状态
/// </summary>
[Description("设置用户状态")]
SetStatus = 555,
SetStatus = 4,
/// <summary>
/// 修改密码
/// </summary>
[Description("修改密码")]
ChangePwd = 666,
ChangePwd = 5,
/// <summary>
/// 重置密码
/// </summary>
[Description("重置密码")]
ResetPwd = 777,
ResetPwd = 6,
/// <summary>
/// 解除登录锁定
/// </summary>
[Description("解除登录锁定")]
UnlockLogin = 888
UnlockLogin = 7
}

View File

@ -259,7 +259,7 @@ public class SysTenantService : IDynamicApiController, ITransient
await _sysRoleMenuService.GrantRoleMenu(new RoleMenuInput() { Id = newRole.Id, MenuIdList = menuIdList });
// 发布新增租户事件
await _eventPublisher.PublishAsync(CommonConst.InitTenant, tenant);
await _eventPublisher.PublishAsync(TenantEventTypeEnum.Add, tenant);
}
/// <summary>
@ -303,7 +303,7 @@ public class SysTenantService : IDynamicApiController, ITransient
await _sysPosRep.AsDeleteable().Where(u => u.TenantId == input.Id).ExecuteCommandAsync();
// 发布删除租户事件
await _eventPublisher.PublishAsync(CommonConst.DeleteTenant, input);
await _eventPublisher.PublishAsync(TenantEventTypeEnum.Delete, input);
}
/// <summary>
@ -352,6 +352,9 @@ public class SysTenantService : IDynamicApiController, ITransient
await _sysUserRep.UpdateAsync(u => new SysUser() { Account = input.AdminAccount, Phone = input.Phone, Email = input.Email }, u => u.Id == input.UserId);
await CacheTenant(input.Id);
// 发布更新租户事件
await _eventPublisher.PublishAsync(TenantEventTypeEnum.Update, input);
}
/// <summary>

View File

@ -15,14 +15,14 @@ public class SysUserEventArgs : EventArgs
/// 事件类型
/// </summary>
[Required]
public SysUserEventTypeEnum EventType { get; set; }
public UserEventTypeEnum EventType { get; set; }
/// <summary>
/// 接口输入参数
/// </summary>
public object Input { get; set; }
public SysUserEventArgs(SysUserEventTypeEnum eventType, object input)
public SysUserEventArgs(UserEventTypeEnum eventType, object input)
{
this.EventType = eventType;
this.Input = input;

View File

@ -23,7 +23,7 @@ public class SysUserService : IDynamicApiController, ITransient
private readonly SysCacheService _sysCacheService;
private readonly SysUserLdapService _sysUserLdapService;
private readonly SqlSugarRepository<SysUser> _sysUserRep;
private readonly SysUserEventHandler _sysUserEventHandler;
private readonly IEventPublisher _eventPublisher;
public SysUserService(UserManager userManager,
SysOrgService sysOrgService,
@ -36,7 +36,7 @@ public class SysUserService : IDynamicApiController, ITransient
SysCacheService sysCacheService,
SysUserLdapService sysUserLdapService,
SqlSugarRepository<SysUser> sysUserRep,
SysUserEventHandler sysUserEventHandler)
IEventPublisher eventPublisher)
{
_userManager = userManager;
_sysOrgService = sysOrgService;
@ -49,7 +49,7 @@ public class SysUserService : IDynamicApiController, ITransient
_sysCacheService = sysCacheService;
_sysUserLdapService = sysUserLdapService;
_sysUserRep = sysUserRep;
_sysUserEventHandler = sysUserEventHandler;
_eventPublisher = eventPublisher;
}
/// <summary>
@ -122,8 +122,8 @@ public class SysUserService : IDynamicApiController, ITransient
if (!string.IsNullOrWhiteSpace(input.DomainAccount))
await _sysUserLdapService.AddUserLdap(newUser.TenantId!.Value, newUser.Id, newUser.Account, input.DomainAccount);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.Add, input);
// 发布新增用户事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.Add, input);
return newUser.Id;
}
@ -161,8 +161,8 @@ public class SysUserService : IDynamicApiController, ITransient
// 更新域账号
await _sysUserLdapService.AddUserLdap(user.TenantId!.Value, user.Id, user.Account, input.DomainAccount);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.Update, input);
// 发布更新用户事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.Update, input);
}
/// <summary>
@ -216,8 +216,8 @@ public class SysUserService : IDynamicApiController, ITransient
// 删除用户收藏菜单
await _sysUserMenuService.DeleteUserMenuList(input.Id);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.Delete, input);
// 发布删除用户事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.Delete, input);
}
/// <summary>
@ -266,8 +266,8 @@ public class SysUserService : IDynamicApiController, ITransient
user.Status = input.Status;
var rows = await _sysUserRep.AsUpdateable(user).UpdateColumns(u => new { u.Status }).ExecuteCommandAsync();
// 执行订阅事件
if (rows > 0) _sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.SetStatus, input);
// 发布设置用户状态事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.SetStatus, input);
return rows;
}
@ -309,8 +309,8 @@ public class SysUserService : IDynamicApiController, ITransient
await _sysUserRoleService.GrantUserRole(input);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.UpdateRole, input);
// 发布更新用户角色事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.UpdateRole, input);
}
/// <summary>
@ -368,8 +368,8 @@ public class SysUserService : IDynamicApiController, ITransient
user.LastChangePasswordTime = DateTime.Now;
var rows = await _sysUserRep.AsUpdateable(user).UpdateColumns(u => new { u.Password, u.LastChangePasswordTime }).ExecuteCommandAsync();
// 执行订阅事件
if (rows > 0) _sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.ChangePwd, input);
// 发布修改用户密码事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.ChangePwd, input);
return rows;
}
@ -392,8 +392,8 @@ public class SysUserService : IDynamicApiController, ITransient
var keyPasswordErrorTimes = $"{CacheConst.KeyPasswordErrorTimes}{user.Account}";
_sysCacheService.Remove(keyPasswordErrorTimes);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.ResetPwd, input);
// 发布重置用户密码事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.ResetPwd, input);
return password;
}
@ -431,8 +431,8 @@ public class SysUserService : IDynamicApiController, ITransient
var keyPasswordErrorTimes = $"{CacheConst.KeyPasswordErrorTimes}{user.Account}";
_sysCacheService.Remove(keyPasswordErrorTimes);
// 执行订阅事件
_sysUserEventHandler.OnEvent(this, SysUserEventTypeEnum.UnlockLogin, input);
// 发布解除登录锁定事件
await _eventPublisher.PublishAsync(UserEventTypeEnum.UnlockLogin, input);
}
/// <summary>

View File

@ -88,8 +88,8 @@
"@types/node": "^20.17.11",
"@types/nprogress": "^0.2.3",
"@types/sortablejs": "^1.15.8",
"@typescript-eslint/eslint-plugin": "^8.19.0",
"@typescript-eslint/parser": "^8.19.0",
"@typescript-eslint/eslint-plugin": "^8.19.1",
"@typescript-eslint/parser": "^8.19.1",
"@vitejs/plugin-vue": "^5.2.1",
"@vitejs/plugin-vue-jsx": "^4.1.1",
"@vue/compiler-sfc": "^3.5.13",