更新项目配置,新增设备同步模块,优化WebSocket和Swagger配置,增强SCADA系统的免登录接口,支持数据字典项和登录日志的免登录查询与记录。调整Java编译设置,确保更好的开发体验。
This commit is contained in:
136
yy-admin-master/YY.Admin.Services/Service/Menu/SysMenuService.cs
Normal file
136
yy-admin-master/YY.Admin.Services/Service/Menu/SysMenuService.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using Mapster;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YY.Admin.Core.Session;
|
||||
using YY.Admin.Services.Service.Role;
|
||||
using YY.Admin.Services.Service.User;
|
||||
|
||||
namespace YY.Admin.Services.Service.Menu
|
||||
{
|
||||
public class SysMenuService : ISysMenuService, ISingletonDependency
|
||||
{
|
||||
private readonly ISqlSugarClient _dbContext;
|
||||
private readonly SysUserRoleService _sysUserRoleService;
|
||||
private readonly SysRoleMenuService _sysRoleMenuService;
|
||||
public SysMenuService(
|
||||
ISqlSugarClient context,
|
||||
SysRoleMenuService sysRoleMenuService,
|
||||
SysUserRoleService sysUserRoleService) {
|
||||
_dbContext=context;
|
||||
_sysUserRoleService=sysUserRoleService;
|
||||
_sysRoleMenuService=sysRoleMenuService;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取登录菜单树
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<MenuOutput>> GetLoginMenuTree()
|
||||
{
|
||||
var currentUser = AppSession.CurrentUser;
|
||||
if (currentUser == null)
|
||||
{
|
||||
return new List<MenuOutput>();
|
||||
}
|
||||
|
||||
var tenantId = currentUser.TenantId ?? 0;
|
||||
var (query, _) = GetSugarQueryableAndTenantId(tenantId);
|
||||
if (currentUser.IsSuperAdmin || currentUser.IsSysAdmin)
|
||||
{
|
||||
var menuList = await query.Where(u => u.Type != MenuTypeEnum.Btn && u.Status == StatusEnum.Enable)
|
||||
.OrderBy(u => new { u.OrderNo, u.Id })
|
||||
.ToTreeAsync(u => u.Children, u => u.Pid, 0);
|
||||
return menuList.Adapt<List<MenuOutput>>();
|
||||
}
|
||||
var menuIdList = await GetMenuIdList();
|
||||
if (menuIdList == null || menuIdList.Count == 0)
|
||||
{
|
||||
// Jeecg自动建档用户可能暂未分配本地角色,这里回退显示基础菜单
|
||||
var fallbackMenus = await GetFallbackMenuTreeAsync();
|
||||
return fallbackMenus.Adapt<List<MenuOutput>>();
|
||||
}
|
||||
|
||||
var menuTree = await query.Where(u => u.Type != MenuTypeEnum.Btn && u.Status == StatusEnum.Enable)
|
||||
.OrderBy(u => new { u.OrderNo, u.Id }).ToTreeAsync(u => u.Children, u => u.Pid, 0, menuIdList.Select(d => (object)d).ToArray());
|
||||
|
||||
// 角色或租户菜单未配置时,避免左侧功能列表全空白
|
||||
if (menuTree == null || menuTree.Count == 0)
|
||||
{
|
||||
menuTree = await GetFallbackMenuTreeAsync();
|
||||
}
|
||||
|
||||
return menuTree.Adapt<List<MenuOutput>>();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前用户菜单Id集合
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<long>> GetMenuIdList()
|
||||
{
|
||||
var currentUser = AppSession.CurrentUser;
|
||||
if (currentUser == null)
|
||||
{
|
||||
return new List<long>();
|
||||
}
|
||||
|
||||
var roleIdList = await _sysUserRoleService.GetUserRoleIdList(currentUser.Id);
|
||||
return await _sysRoleMenuService.GetRoleMenuIdList(roleIdList);
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据租户id获取构建菜单联表查询实例
|
||||
/// </summary>
|
||||
/// <param name="tenantId"></param>
|
||||
/// <returns></returns>
|
||||
public (ISugarQueryable<SysMenu, SysTenantMenu> query, long tenantId) GetSugarQueryableAndTenantId(long tenantId)
|
||||
{
|
||||
if (!AppSession.CurrentUser!.IsSuperAdmin) tenantId = AppSession.CurrentUser.TenantId!.Value;
|
||||
|
||||
// 超管用户菜单范围:种子菜单 + 租户id菜单
|
||||
ISugarQueryable<SysMenu, SysTenantMenu> query;
|
||||
if (AppSession.CurrentUser.IsSuperAdmin)
|
||||
{
|
||||
if (tenantId <= 0)
|
||||
{
|
||||
query = _dbContext.Queryable<SysMenu>().InnerJoinIF<SysTenantMenu>(false, (u, t) => true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 指定租户的菜单
|
||||
var menuIds = _dbContext.Queryable<SysTenantMenu>().Where(u => u.TenantId == tenantId).ToList(u => u.MenuId) ?? new();
|
||||
|
||||
// 种子菜单
|
||||
//menuIds.AddRange(new SysMenuSeedData().HasData().Select(u => u.Id).ToList());
|
||||
|
||||
menuIds = menuIds.Distinct().ToList();
|
||||
query = _dbContext.Queryable<SysMenu>().InnerJoinIF<SysTenantMenu>(false, (u, t) => true).Where(u => menuIds.Contains(u.Id));
|
||||
}
|
||||
}
|
||||
else if (AppSession.CurrentUser.IsSysAdmin)
|
||||
{
|
||||
// 系统管理员直接读取全量启用菜单,不依赖租户菜单关联表
|
||||
query = _dbContext.Queryable<SysMenu>().InnerJoinIF<SysTenantMenu>(false, (u, t) => true);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = _dbContext.Queryable<SysMenu>().InnerJoinIF<SysTenantMenu>(tenantId > 0, (u, t) => t.TenantId == tenantId && u.Id == t.MenuId);
|
||||
}
|
||||
|
||||
return (query, tenantId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 菜单兜底:当角色/租户未完成绑定时返回可用基础菜单,避免界面空白
|
||||
/// </summary>
|
||||
private async Task<List<SysMenu>> GetFallbackMenuTreeAsync()
|
||||
{
|
||||
return await _dbContext.Queryable<SysMenu>()
|
||||
.Where(u => u.Type != MenuTypeEnum.Btn && u.Status == StatusEnum.Enable)
|
||||
.OrderBy(u => new { u.OrderNo, u.Id })
|
||||
.ToTreeAsync(u => u.Children, u => u.Pid, 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user