|
|
|
|
using AutoMapper;
|
|
|
|
|
using Common.Enum;
|
|
|
|
|
using Entity.DbModel.System.SysBaseObject;
|
|
|
|
|
using Entity.Dto.Req;
|
|
|
|
|
using Entity.Dto.Resp;
|
|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Repository.System;
|
|
|
|
|
using Service.Mgr;
|
|
|
|
|
|
|
|
|
|
namespace Service.System
|
|
|
|
|
{
|
|
|
|
|
[Scope("SingleInstance")]
|
|
|
|
|
public class SysMenuService : BaseServices<SysMenu>
|
|
|
|
|
{
|
|
|
|
|
//private readonly UserManager _userManager;
|
|
|
|
|
private readonly SysMenuRepository _sysMenuRep;
|
|
|
|
|
private readonly SysRoleMenuMgr _sysRoleMenuMgr;
|
|
|
|
|
private readonly SysUserRoleMgr _sysUserRoleMgr;
|
|
|
|
|
|
|
|
|
|
public SysMenuService(
|
|
|
|
|
SysMenuRepository sysMenuRep,
|
|
|
|
|
SysRoleMenuMgr sysRoleMenuService,
|
|
|
|
|
SysUserRoleMgr sysUserRoleService)
|
|
|
|
|
{
|
|
|
|
|
_sysMenuRep = sysMenuRep;
|
|
|
|
|
_sysRoleMenuMgr = sysRoleMenuService;
|
|
|
|
|
_sysUserRoleMgr = sysUserRoleService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取登录菜单树 🔖
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<List<MenuResp>> GetLoginMenuTree()
|
|
|
|
|
{
|
|
|
|
|
if (UserManager.SuperAdmin)
|
|
|
|
|
{
|
|
|
|
|
var config = new MapperConfiguration(u => u.CreateMap<MenuResp, SysMenu>().ReverseMap());
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
var menuList = await _sysMenuRep.QueryTreeByClauseAsync((u => u.Type != MenuTypeEnum.Btn && u.Status == StatusEnum.Enable),
|
|
|
|
|
u => new { u.OrderNo, u.Id },
|
|
|
|
|
u => u.Children, u => u.Pid, 0);
|
|
|
|
|
return menuList.Adapt<List<MenuResp>>();
|
|
|
|
|
//return mapper.Map<List<MenuResp>>(menuList);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var menuIdList = await GetMenuIdList();
|
|
|
|
|
var menuTree = await _sysMenuRep.QueryTreeByClauseAsync(u => u.Status == StatusEnum.Enable, u => new { u.OrderNo, u.Id },
|
|
|
|
|
u => u.Children, u => u.Pid, 0, menuIdList.Select(d => (object)d).ToArray());
|
|
|
|
|
DeleteBtnFromMenuTree(menuTree);
|
|
|
|
|
return menuTree.Adapt<List<MenuResp>>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除登录菜单树里面的按钮
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DeleteBtnFromMenuTree(List<SysMenu> menuList)
|
|
|
|
|
{
|
|
|
|
|
if (menuList == null) return;
|
|
|
|
|
for (var i = menuList.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var menu = menuList[i];
|
|
|
|
|
if (menu.Type == MenuTypeEnum.Btn)
|
|
|
|
|
menuList.Remove(menu);
|
|
|
|
|
else if (menu.Children.Count > 0)
|
|
|
|
|
DeleteBtnFromMenuTree(menu.Children);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取菜单列表 🔖
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<List<SysMenu>> GetList([FromQuery] MenuReq input)
|
|
|
|
|
{
|
|
|
|
|
var menuIdList = UserManager.SuperAdmin ? new List<long>() : await GetMenuIdList();
|
|
|
|
|
|
|
|
|
|
// 有筛选条件时返回list列表(防止构造不出树)
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.Title) || input.Type is > 0)
|
|
|
|
|
{
|
|
|
|
|
return await _sysMenuRep.QueryListByClauseAsync(
|
|
|
|
|
!string.IsNullOrWhiteSpace(input.Title), u => u.Title.Contains(input.Title),
|
|
|
|
|
input.Type is > 0, u => u.Type == input.Type,
|
|
|
|
|
menuIdList.Count > 1, u => menuIdList.Contains(u.Id), (u => u.OrderNo)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return UserManager.SuperAdmin ?
|
|
|
|
|
await _sysMenuRep.QueryTreeByClauseAsync(u => u.OrderNo, u => u.Children, u => u.Pid, 0) :
|
|
|
|
|
await _sysMenuRep.QueryTreeByClauseAsync(u => u.OrderNo, u => u.Children, u => u.Pid, 0, menuIdList.Select(d => (object)d).ToArray()); // 角色菜单授权时
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 增加菜单 🔖
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<string> AddMenu(AddMenuReq input)
|
|
|
|
|
{
|
|
|
|
|
string result = "";
|
|
|
|
|
var isExist = input.Type != MenuTypeEnum.Btn
|
|
|
|
|
? await _sysMenuRep.QueryByClauseAsync(u => u.Title == input.Title && u.Pid == input.Pid)
|
|
|
|
|
: await _sysMenuRep.QueryByClauseAsync(u => u.Permission == input.Permission && u.Pid == input.Pid);
|
|
|
|
|
if (isExist != null)
|
|
|
|
|
{
|
|
|
|
|
result = "菜单已存在";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.Name))
|
|
|
|
|
{
|
|
|
|
|
if (await _sysMenuRep.QueryByClauseAsync(u => u.Name == input.Name) != null)
|
|
|
|
|
return result = "路由名称重复";
|
|
|
|
|
}
|
|
|
|
|
if (input.Pid != 0)
|
|
|
|
|
{
|
|
|
|
|
if (await _sysMenuRep.QueryByClauseAsync(u => u.Id == input.Pid && u.Type == MenuTypeEnum.Btn) != null)
|
|
|
|
|
return result = "父节点不能为按钮类型";
|
|
|
|
|
}
|
|
|
|
|
// 校验菜单参数
|
|
|
|
|
var sysMenu = input.Adapt<SysMenu>();
|
|
|
|
|
string checkResult = CheckMenuParam(sysMenu);
|
|
|
|
|
if (checkResult == "")
|
|
|
|
|
{
|
|
|
|
|
await _sysMenuRep.InsertAsync(sysMenu);
|
|
|
|
|
return result = "增加菜单成功";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return result = "增加菜单失败";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新菜单 🔖
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<int> UpdateMenu(UpdateMenuReq input)
|
|
|
|
|
{
|
|
|
|
|
int updateResult = 0;
|
|
|
|
|
if (input.Id == input.Pid)
|
|
|
|
|
throw new ArgumentException($"禁止本节点与父节点相同");
|
|
|
|
|
var isExist = input.Type != MenuTypeEnum.Btn
|
|
|
|
|
? await _sysMenuRep.QueryByClauseAsync(u => u.Title == input.Title && u.Type == input.Type && u.Pid == input.Pid && u.Id != input.Id)
|
|
|
|
|
: await _sysMenuRep.QueryByClauseAsync(u => u.Permission == input.Permission && u.Type == input.Type && u.Pid == input.Pid && u.Id != input.Id);
|
|
|
|
|
if (isExist != null)
|
|
|
|
|
throw new ArgumentException($"菜单已存在");
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.Name))
|
|
|
|
|
{
|
|
|
|
|
if ((await _sysMenuRep.QueryByClauseAsync(u => u.Id != input.Id && u.Name == input.Name)) != null)
|
|
|
|
|
throw new ArgumentException($"路由名称重复");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.Pid != 0)
|
|
|
|
|
{
|
|
|
|
|
if ((await _sysMenuRep.QueryByClauseAsync(u => u.Id == input.Pid && u.Type == MenuTypeEnum.Btn)) != null)
|
|
|
|
|
throw new ArgumentException($"父节点不能为按钮类型");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验菜单参数
|
|
|
|
|
var sysMenu = input.Adapt<SysMenu>();
|
|
|
|
|
CheckMenuParam(sysMenu);
|
|
|
|
|
|
|
|
|
|
bool result = await _sysMenuRep.UpdateAsync(sysMenu);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
updateResult = 200;
|
|
|
|
|
return updateResult;
|
|
|
|
|
}
|
|
|
|
|
return updateResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除菜单 🔖
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task DeleteMenu(DeleteMenuReq input)
|
|
|
|
|
{
|
|
|
|
|
var menuTreeList = await _sysMenuRep.QueryTreeByClauseAsync(u => u.Pid, input.Id, true);
|
|
|
|
|
var menuIdList = menuTreeList.Select(u => u.Id).ToList();
|
|
|
|
|
|
|
|
|
|
await _sysMenuRep.DeleteAsync(u => menuIdList.Contains(u.Id));
|
|
|
|
|
|
|
|
|
|
// 级联删除角色菜单数据
|
|
|
|
|
await _sysRoleMenuMgr.DeleteRoleMenuByMenuIdList(menuIdList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 增加和编辑时检查菜单数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menu"></param>
|
|
|
|
|
private static string CheckMenuParam(SysMenu menu)
|
|
|
|
|
{
|
|
|
|
|
string result = "";
|
|
|
|
|
var permission = menu.Permission;
|
|
|
|
|
if (menu.Type == MenuTypeEnum.Btn)
|
|
|
|
|
{
|
|
|
|
|
menu.Name = null;
|
|
|
|
|
menu.Path = null;
|
|
|
|
|
menu.Component = null;
|
|
|
|
|
menu.Icon = null;
|
|
|
|
|
menu.Redirect = null;
|
|
|
|
|
menu.OutLink = null;
|
|
|
|
|
menu.IsHide = false;
|
|
|
|
|
menu.IsKeepAlive = true;
|
|
|
|
|
menu.IsAffix = false;
|
|
|
|
|
menu.IsIframe = false;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(permission))
|
|
|
|
|
return result = "权限标识格式为空";
|
|
|
|
|
if (!permission.Contains(':'))
|
|
|
|
|
return result = "权限标识格式错误 如xxx:xxx";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
menu.Permission = null;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前用户菜单Id集合
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task<List<long>> GetMenuIdList()
|
|
|
|
|
{
|
|
|
|
|
var roleIdList = await _sysUserRoleMgr.GetUserRoleIdList(UserManager.UserId);
|
|
|
|
|
return await _sysRoleMenuMgr.GetRoleMenuIdList(roleIdList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|