😎优化登录逻辑
This commit is contained in:
parent
3d50990047
commit
de8b31224f
@ -64,7 +64,13 @@ public class AppAuthService : IDynamicApiController, ITransient
|
||||
throw Oops.Oh(ErrorCodeEnum.D1027);
|
||||
|
||||
// 判断是否开启验证码并校验
|
||||
var tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.WhereIF(input.TenantId > 0, u => u.Id == input.TenantId).FirstOrDefault();
|
||||
var tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == input.TenantId);
|
||||
if (tenant == null)
|
||||
{
|
||||
await Furion.App.GetRequiredService<SysTenantService>().CacheTenant(); // 重新生成租户列表缓存
|
||||
tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == input.TenantId);
|
||||
if (tenant == null) throw Oops.Oh(ErrorCodeEnum.D0007);
|
||||
}
|
||||
if (tenant.Captcha == true && !_captcha.Validate(input.CodeId.ToString(), input.Code))
|
||||
throw Oops.Oh(ErrorCodeEnum.D0008);
|
||||
|
||||
|
||||
@ -56,7 +56,7 @@
|
||||
<PackageReference Include="SSH.NET" Version="2024.2.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1191" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1192" />
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -13,6 +13,12 @@ namespace Admin.NET.Core;
|
||||
[Description("系统错误码")]
|
||||
public enum ErrorCodeEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户不存在
|
||||
/// </summary>
|
||||
[ErrorCodeItemMetadata("租户不存在")]
|
||||
D0007,
|
||||
|
||||
/// <summary>
|
||||
/// 验证码错误
|
||||
/// </summary>
|
||||
|
||||
@ -47,7 +47,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("账号密码登录")]
|
||||
[AllowAnonymous]
|
||||
[AllowAnonymous]
|
||||
public virtual async Task<LoginOutput> Login([Required] LoginInput input)
|
||||
{
|
||||
// 判断密码错误次数(缓存30分钟)
|
||||
@ -59,12 +59,12 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
if (passwordErrorTimes > passwordMaxErrorTimes) throw Oops.Oh(ErrorCodeEnum.D1027);
|
||||
|
||||
// 判断是否开启验证码并校验
|
||||
var tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.WhereIF(input.TenantId > 0, u => u.Id == input.TenantId).FirstOrDefault();
|
||||
// 缓存被清服务不重启这里tenant会为空,出错
|
||||
if (tenant==null && input.TenantId==0)
|
||||
{
|
||||
await App.GetRequiredService<SysTenantService>().CacheTenant();
|
||||
tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.WhereIF(input.TenantId > 0, u => u.Id == input.TenantId).FirstOrDefault();
|
||||
var tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == input.TenantId);
|
||||
if (tenant == null)
|
||||
{
|
||||
await App.GetRequiredService<SysTenantService>().CacheTenant(); // 重新生成租户列表缓存
|
||||
tenant = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == input.TenantId);
|
||||
if (tenant == null) throw Oops.Oh(ErrorCodeEnum.D0007);
|
||||
}
|
||||
if (tenant.Captcha == true && !_captcha.Validate(input.CodeId.ToString(), input.Code))
|
||||
throw Oops.Oh(ErrorCodeEnum.D0008);
|
||||
@ -102,7 +102,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <param name="account"></param>
|
||||
/// <param name="phone"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
[NonAction]
|
||||
public async Task<SysUser> GetLoginUser(long tenantId, string account = null, string phone = null)
|
||||
{
|
||||
//// 若没有传值租户Id,则从请求页URL参数中获取租户Id(空则默认租户)
|
||||
@ -113,8 +113,8 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
//}
|
||||
|
||||
// 判断账号是否存在
|
||||
var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter()
|
||||
//.WhereIF(tenantId > 0, u => u.TenantId == tenantId)
|
||||
var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter()
|
||||
//.WhereIF(tenantId > 0, u => u.TenantId == tenantId)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(account), u => u.Account.Equals(account))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(phone), u => u.Phone.Equals(phone))
|
||||
.FirstAsync();
|
||||
@ -167,7 +167,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("验证锁屏密码")]
|
||||
[DisplayName("验证锁屏密码")]
|
||||
public virtual async Task<bool> UnLockScreen([Required, FromQuery] string password)
|
||||
{
|
||||
// 账号是否存在
|
||||
@ -203,7 +203,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("手机号登录")]
|
||||
[AllowAnonymous]
|
||||
[AllowAnonymous]
|
||||
public virtual async Task<LoginOutput> LoginPhone([Required] LoginPhoneInput input)
|
||||
{
|
||||
// 校验短信验证码
|
||||
@ -221,7 +221,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <param name="user"></param>
|
||||
/// <param name="loginMode"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
[NonAction]
|
||||
internal async Task<LoginOutput> CreateToken(SysUser user, LoginModeEnum loginMode = LoginModeEnum.PC)
|
||||
{
|
||||
// 单用户登录
|
||||
@ -280,7 +280,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// 获取登录账号 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取登录账号")]
|
||||
[DisplayName("获取登录账号")]
|
||||
public virtual async Task<LoginUserOutput> GetUserInfo()
|
||||
{
|
||||
var user = await _sysUserRep.GetByIdAsync(_userManager.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D1011).StatusCode(401);
|
||||
@ -336,7 +336,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <summary>
|
||||
/// 退出系统 🔖
|
||||
/// </summary>
|
||||
[DisplayName("退出系统")]
|
||||
[DisplayName("退出系统")]
|
||||
public async void Logout()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_userManager.Account))
|
||||
@ -360,7 +360,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <returns></returns>
|
||||
[SuppressMonitor]
|
||||
[DisplayName("获取验证码")]
|
||||
[AllowAnonymous]
|
||||
[AllowAnonymous]
|
||||
public CaptchaOutput GetCaptcha()
|
||||
{
|
||||
var codeId = YitIdHelper.NextId().ToString();
|
||||
@ -375,7 +375,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <returns></returns>
|
||||
[Route("/api/swagger/checkUrl"), NonUnify]
|
||||
[ApiDescriptionSettings(Description = "Swagger登录检查", DisableInherite = true)]
|
||||
[AllowAnonymous]
|
||||
[AllowAnonymous]
|
||||
public int SwaggerCheckUrl()
|
||||
{
|
||||
return _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated ? 200 : 401;
|
||||
@ -388,7 +388,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
||||
/// <returns></returns>
|
||||
[Route("/api/swagger/submitUrl"), NonUnify]
|
||||
[ApiDescriptionSettings(Description = "Swagger登录提交", DisableInherite = true)]
|
||||
[AllowAnonymous]
|
||||
[AllowAnonymous]
|
||||
public async Task<int> SwaggerSubmitUrl([FromForm] SpecificationAuth auth)
|
||||
{
|
||||
try
|
||||
|
||||
@ -51,7 +51,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取角色分页列表")]
|
||||
[DisplayName("获取角色分页列表")]
|
||||
public async Task<SqlSugarPagedList<PageRoleOutput>> Page(PageRoleInput input)
|
||||
{
|
||||
// 当前用户已拥有的角色集合
|
||||
@ -76,7 +76,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// 获取角色列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取角色列表")]
|
||||
[DisplayName("获取角色列表")]
|
||||
public async Task<List<RoleOutput>> GetList()
|
||||
{
|
||||
// 当前用户已拥有的角色集合
|
||||
@ -95,7 +95,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Add"), HttpPost]
|
||||
[DisplayName("增加角色")]
|
||||
[DisplayName("增加角色")]
|
||||
public async Task AddRole(AddRoleInput input)
|
||||
{
|
||||
if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code))
|
||||
@ -116,7 +116,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
||||
[DisplayName("更新角色")]
|
||||
[DisplayName("更新角色")]
|
||||
public async Task UpdateRole(UpdateRoleInput input)
|
||||
{
|
||||
if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id))
|
||||
@ -133,7 +133,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
|
||||
[DisplayName("删除角色")]
|
||||
[DisplayName("删除角色")]
|
||||
public async Task DeleteRole(DeleteRoleInput input)
|
||||
{
|
||||
// 禁止删除系统管理员角色
|
||||
@ -164,7 +164,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("授权角色菜单")]
|
||||
[DisplayName("授权角色菜单")]
|
||||
public async Task GrantMenu(RoleMenuInput input)
|
||||
{
|
||||
if (input.MenuIdList == null || input.MenuIdList.Count < 1) return;
|
||||
@ -189,7 +189,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[DisplayName("授权角色表格")]
|
||||
[DisplayName("授权角色表格")]
|
||||
public async Task GrantRoleTable(RoleTableInput input)
|
||||
{
|
||||
await _sysRoleTableService.GrantRoleTable(input);
|
||||
@ -201,7 +201,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[DisplayName("授权角色数据范围")]
|
||||
[DisplayName("授权角色数据范围")]
|
||||
public async Task GrantDataScope(RoleOrgInput input)
|
||||
{
|
||||
// 删除与该角色相关的用户机构缓存
|
||||
@ -247,7 +247,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[DisplayName("授权角色接口")]
|
||||
[DisplayName("授权角色接口")]
|
||||
public async Task GrantApi(RoleApiInput input)
|
||||
{
|
||||
await ClearUserApiCache(input.Id);
|
||||
@ -260,7 +260,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[DisplayName("授权角色用户")]
|
||||
[DisplayName("授权角色用户")]
|
||||
public async Task GrantUser(RoleUserInput input)
|
||||
{
|
||||
await _sysUserRoleService.GrantRoleUser(input);
|
||||
@ -271,7 +271,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("设置角色状态")]
|
||||
[DisplayName("设置角色状态")]
|
||||
public async Task<int> SetStatus(RoleInput input)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(StatusEnum), input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005);
|
||||
@ -286,7 +286,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// 获取所有表格字段 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取所有表格字段")]
|
||||
[DisplayName("获取所有表格字段")]
|
||||
public List<RoleTableOutput> GetAllTableColumnList()
|
||||
{
|
||||
return _sysRoleTableService.HandleTableColumn();
|
||||
@ -297,7 +297,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取角色表格字段集合")]
|
||||
[DisplayName("获取角色表格字段集合")]
|
||||
public async Task<List<string>> GetRoleTable(long roleId)
|
||||
{
|
||||
return await _sysRoleTableService.GetRoleTable(roleId);
|
||||
@ -307,7 +307,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// 获取当前用户表格字段集合 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取当前用户表格字段集合")]
|
||||
[DisplayName("获取当前用户表格字段集合")]
|
||||
public async Task<List<string>> GetUserRoleTableList()
|
||||
{
|
||||
return await _sysRoleTableService.GetUserRoleTableList(_userManager, _sysUserRoleService);
|
||||
@ -318,7 +318,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("根据角色Id获取菜单Id集合")]
|
||||
[DisplayName("根据角色Id获取菜单Id集合")]
|
||||
public async Task<List<long>> GetOwnMenuList([FromQuery] RoleInput input)
|
||||
{
|
||||
return await _sysRoleMenuService.GetRoleMenuIdList(new List<long> { input.Id });
|
||||
@ -329,7 +329,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("根据角色Id获取机构Id集合")]
|
||||
[DisplayName("根据角色Id获取机构Id集合")]
|
||||
public async Task<List<long>> GetOwnOrgList([FromQuery] RoleInput input)
|
||||
{
|
||||
return await _sysRoleOrgService.GetRoleOrgIdList(new List<long> { input.Id });
|
||||
@ -340,7 +340,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取角色接口黑名单集合")]
|
||||
[DisplayName("获取角色接口黑名单集合")]
|
||||
public async Task<List<string>> GetRoleApiList([FromQuery] RoleInput input)
|
||||
{
|
||||
return await _sysRoleApiService.GetRoleApiList(new List<long> { input.Id });
|
||||
@ -353,14 +353,14 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// 获取用户接口集合 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取用户接口集合")]
|
||||
[DisplayName("获取用户接口集合")]
|
||||
public async Task<List<List<string>>> GetUserApiList()
|
||||
{
|
||||
var userId = _userManager.UserId;
|
||||
var apiList = _sysCacheService.Get<List<List<string>>>(CacheConst.KeyUserApi + userId);
|
||||
if (apiList != null) return apiList;
|
||||
|
||||
apiList = new List<List<string>>() { new(), new() };
|
||||
apiList = [[], []];
|
||||
// 超管账号获取所有接口
|
||||
if (_userManager.SuperAdmin)
|
||||
{
|
||||
@ -392,10 +392,12 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
// 接口黑名单集合
|
||||
var roleApiList = await _sysRoleApiService.GetRoleApiList(roleIdList);
|
||||
apiList[1].AddRange(roleApiList);
|
||||
}
|
||||
_sysCacheService.Set(CacheConst.KeyUserApi + userId, apiList, TimeSpan.FromDays(7)); // 缓存7天
|
||||
foreach (var lst in apiList)
|
||||
lst.Sort();
|
||||
}
|
||||
|
||||
// 排序接口名称
|
||||
apiList[0].Sort();
|
||||
apiList[1].Sort();
|
||||
_sysCacheService.Set(CacheConst.KeyUserApi + userId, apiList, TimeSpan.FromDays(7)); // 缓存7天
|
||||
return apiList;
|
||||
}
|
||||
|
||||
@ -420,7 +422,7 @@ public class SysRoleService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
[NonAction]
|
||||
public async Task<List<string>> GetRoleButtonList(List<long> roleIds)
|
||||
{
|
||||
var menuIdList = await _sysRoleMenuService.GetRoleMenuIdList(roleIds);
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"name": "admin.net.pro",
|
||||
"type": "module",
|
||||
"version": "2.4.33",
|
||||
"lastBuildTime": "2025.03.04",
|
||||
"lastBuildTime": "2025.03.05",
|
||||
"description": "Admin.NET 站在巨人肩膀上的 .NET 通用权限开发框架",
|
||||
"author": "zuohuaijun",
|
||||
"license": "MIT",
|
||||
@ -97,7 +97,7 @@
|
||||
"@vue/compiler-sfc": "^3.5.13",
|
||||
"code-inspector-plugin": "^0.20.2",
|
||||
"eslint": "^9.21.0",
|
||||
"eslint-plugin-vue": "^9.32.0",
|
||||
"eslint-plugin-vue": "^9.33.0",
|
||||
"globals": "^16.0.0",
|
||||
"less": "^4.2.2",
|
||||
"prettier": "^3.5.3",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user