From 3cbe8d5b48d8d9a3afb1ab71bb54d8168465d176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Tue, 23 Jun 2026 22:18:53 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YD_Event.Application/Menu/SysMenuService.cs | 41 +++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) 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(); + } + /// /// 移除菜单中的按钮 ///