😎1、优化没有对应接口的按钮增加到授权集合 2、清理代码

This commit is contained in:
zuohuaijun 2024-10-27 16:45:36 +08:00
parent 8210fe8753
commit 2e64a7f29c
6 changed files with 98 additions and 109 deletions

View File

@ -11,5 +11,4 @@ namespace Admin.NET.Application;
/// </summary>
public class AppClaimConst : ClaimConst
{
}

View File

@ -160,10 +160,6 @@ public class AppAuthService : IDynamicApiController, ITransient
[NonAction]
public virtual async Task<LoginOutput> CreateToken(SysUser user, LoginModeEnum loginMode)
{
// 默认PC端登录模式
if (loginMode == 0)
loginMode = LoginModeEnum.PC;
// 单用户登录
await _sysOnlineUserService.SingleLogin(user.Id, loginMode);
@ -189,9 +185,6 @@ public class AppAuthService : IDynamicApiController, ITransient
// 设置响应报文头
_httpContextAccessor.HttpContext.SetTokensOfResponseHeaders(accessToken, refreshToken);
// Swagger Knife4UI-AfterScript登录脚本
// ke.global.setAllHeader('Authorization', 'Bearer ' + ke.response.headers['access-token']);
return new LoginOutput
{
AccessToken = accessToken,

View File

@ -18,5 +18,4 @@ public class AppUserManager : UserManager
}
// 扩展属性
}

View File

@ -38,7 +38,7 @@ public class LoginInput
/// <summary>
/// 登录模式
/// </summary>
public LoginModeEnum LoginMode { get; set; }
public LoginModeEnum LoginMode { get; set; } = LoginModeEnum.PC;
}
public class LoginPhoneInput

View File

@ -313,31 +313,29 @@ public class SysRoleService : IDynamicApiController, ITransient
apiList[0].AddRange(controller.Children.Select(u => u.Route));
}
// 按钮权限集合
var allButtonList = await GetButtonList(new());
// 没有对应接口的按钮集合
var diffList = allButtonList.Except(apiList[0]).ToList();
apiList[0].AddRange(diffList);
// 所有按钮权限集合
var allButtonList = await GetButtonList();
// 没有接口对应的按钮权限集合
var diffButtonList = allButtonList.Except(apiList[0]).ToList(); // 差集
apiList[0].AddRange(diffButtonList);
}
else
{
// 当前账号所有角色集合
var roleIds = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId);
var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId);
// 已勾选按钮权限集合
apiList[0] = await GetRoleButtonList(roleIds);
apiList[0] = await GetRoleButtonList(roleIdList);
// 未勾选按钮权限集合(放到接口黑名单里面)
var allButtonList = await GetButtonList(new());
var allButtonList = await GetButtonList();
apiList[1] = allButtonList.Except(apiList[0]).ToList(); // 差集
// 接口黑名单集合
var roleApis = await _sysRoleApiService.GetRoleApiList(roleIds);
apiList[1].AddRange(roleApis);
var roleApiList = await _sysRoleApiService.GetRoleApiList(roleIdList);
apiList[1].AddRange(roleApiList);
}
_sysCacheService.Set(CacheConst.KeyUserApi + userId, apiList, TimeSpan.FromDays(7));
_sysCacheService.Set(CacheConst.KeyUserApi + userId, apiList, TimeSpan.FromDays(7)); // 缓存7天
}
return apiList;
}
@ -375,11 +373,11 @@ public class SysRoleService : IDynamicApiController, ITransient
/// </summary>
/// <param name="menuIds"></param>
/// <returns></returns>
private async Task<List<string>> GetButtonList(List<long> menuIds)
private async Task<List<string>> GetButtonList(List<long> menuIds = null)
{
return await _sysRoleRep.ChangeRepository<SqlSugarRepository<SysMenu>>().AsQueryable()
.Where(u => u.Type == MenuTypeEnum.Btn)
.WhereIF(menuIds.Count > 0, u => menuIds.Contains(u.Id))
.WhereIF(menuIds != null && menuIds.Count > 0, u => menuIds.Contains(u.Id))
.Select(u => u.Permission).ToListAsync();
}