diff --git a/YD_Event.Application/Menu/SysMenuService.cs b/YD_Event.Application/Menu/SysMenuService.cs index 5c35344..d2c33c8 100644 --- a/YD_Event.Application/Menu/SysMenuService.cs +++ b/YD_Event.Application/Menu/SysMenuService.cs @@ -358,9 +358,16 @@ public class SysMenuService : BaseService, ITransient public async Task CheckPermission(string code) { if (_authManager.IsSuperAdmin) return true; - var cache = await GetAuthButtonCodeList(_authManager.UserId); - var output = cache.FirstOrDefault(x => string.Equals(x.Code, code, StringComparison.CurrentCultureIgnoreCase)); - return output?.Access ?? false; + + // 兼容历史接口:未配置权限码的接口仅要求已登录,不做菜单权限拦截 + var hasConfiguredPermission = await _sysMenuRepository.IsAnyAsync(x => x.Code == code); + if (!hasConfiguredPermission) + { + return true; + } + + var authCodes = await GetAuthPermissionCodeList(_authManager.UserId); + return authCodes.Any(x => string.Equals(x, code, StringComparison.CurrentCultureIgnoreCase)); } /// @@ -404,6 +411,34 @@ public class SysMenuService : BaseService, ITransient return cache.Value; } + [NonAction] + private async Task> GetAuthPermissionCodeList(long userId) + { + var cacheKey = $"{CacheConst.PermissionKey}code.v2.{userId}"; + var cache = await _easyCachingProvider.GetAsync(cacheKey, async () => + { + var list = await _sysMenuRepository.AsSugarClient() + .Queryable() + .InnerJoin((userRole, role) => userRole.RoleId == role.Id) + .InnerJoin((userRole, role, roleMenu) => role.Id == roleMenu.RoleId) + .InnerJoin((userRole, role, roleMenu, menu) => roleMenu.MenuId == menu.Id) + .Where((userRole, role, roleMenu, menu) => + userRole.UserId == userId && + role.Status == AvailabilityStatus.Enable && + menu.Status == AvailabilityStatus.Enable && + !string.IsNullOrEmpty(menu.Code)) + .Select((userRole, role, roleMenu, menu) => menu.Code) + .ToListAsync(); + + return list + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct() + .ToList(); + }, TimeSpan.FromDays(1)); + + return cache.Value ?? new List(); + } + /// /// 移除菜单中的按钮 ///