From 9d87d75a092df7feda245e1f16ed43c4f09be88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sat, 17 Jun 2023 09:45:45 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=A7=92=E8=89=B2=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/System/SysRoleController.cs | 28 +++++++++++++++- ZR.Model/System/Dto/MenuDto.cs | 11 ++++++- ZR.Model/System/Dto/RoleMenuExportDto.cs | 30 +++++++++++++++++ ZR.Model/System/Enums/MenuStatus.cs | 15 +++++++++ ZR.Model/System/Enums/MenuType.cs | 19 +++++++++++ ZR.Service/System/IService/ISysMenuService.cs | 2 ++ ZR.Service/System/SysMenuService.cs | 33 ++++++++++++++++++- 7 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 ZR.Model/System/Dto/RoleMenuExportDto.cs create mode 100644 ZR.Model/System/Enums/MenuStatus.cs create mode 100644 ZR.Model/System/Enums/MenuType.cs diff --git a/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs b/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs index 37b5276..10b89d9 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysRoleController.cs @@ -11,6 +11,11 @@ using ZR.Service.System.IService; using ZR.Admin.WebApi.Extensions; using ZR.Model.System.Dto; using Mapster; +using ZR.Service; +using Microsoft.AspNetCore.Authorization; +using Aliyun.OSS; +using MiniExcelLibs.OpenXml; +using MiniExcelLibs; namespace ZR.Admin.WebApi.Controllers.System { @@ -22,11 +27,14 @@ namespace ZR.Admin.WebApi.Controllers.System public class SysRoleController : BaseController { private readonly ISysRoleService sysRoleService; + private readonly ISysMenuService sysMenuService; public SysRoleController( - ISysRoleService sysRoleService) + ISysRoleService sysRoleService, + ISysMenuService sysMenuService) { this.sysRoleService = sysRoleService; + this.sysMenuService = sysMenuService; } /// @@ -179,5 +187,23 @@ namespace ZR.Admin.WebApi.Controllers.System string sFileName = ExportExcel(list, "sysrole", "角色"); return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName }); } + + /// + /// 导出角色菜单 + /// + /// + /// + [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "角色菜单导出")] + [HttpGet("exportRoleMenu")] + [AllowAnonymous] + public IActionResult ExportRoleMenu(int roleId) + { + MenuQueryDto dto = new() { Status = "0", MenuTypeIds = "M,C,F" }; + + var list = sysMenuService.SelectRoleMenuListByRole(dto, roleId); + + var result = ExportExcelMini(list, roleId.ToString(), "角色菜单"); + return ExportExcel(result.Item2, result.Item1); + } } } diff --git a/ZR.Model/System/Dto/MenuDto.cs b/ZR.Model/System/Dto/MenuDto.cs index 5c21422..32ffaed 100644 --- a/ZR.Model/System/Dto/MenuDto.cs +++ b/ZR.Model/System/Dto/MenuDto.cs @@ -1,4 +1,6 @@ -using System; +using MiniExcelLibs.Attributes; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace ZR.Model.System.Dto @@ -7,7 +9,9 @@ namespace ZR.Model.System.Dto { //{"parentId":0,"menuName":"aaa","icon":"documentation","menuType":"M","orderNum":999,"visible":0,"status":0,"path":"aaa"} [Required(ErrorMessage = "菜单id不能为空")] + [ExcelColumn(Name = "菜单id")] public int MenuId { get; set; } + [ExcelColumn(Name = "菜单名")] public string MenuName { get; set; } /// /// 父菜单ID @@ -22,17 +26,20 @@ namespace ZR.Model.System.Dto /// /// 路由地址 /// + [ExcelColumn(Name = "路由地址")] public string Path { get; set; } = "#"; /// /// 组件路径 /// + [ExcelColumn(Name = "组件地址")] public string Component { get; set; } /// /// 是否缓存(1缓存 0不缓存) /// [Required(ErrorMessage = "是否缓存不能为空")] + [ExcelColumn(Name = "是否缓存")] public int IsCache { get; set; } /// /// 是否外链 1、是 0、否 @@ -60,6 +67,7 @@ namespace ZR.Model.System.Dto /// /// 权限字符串 /// + [ExcelColumn(Name = "权限字符串")] public string Perms { get; set; } /// @@ -70,6 +78,7 @@ namespace ZR.Model.System.Dto /// 翻译key /// public string MenuNameKey { get; set; } + public List Children { get; set; } = new List(); } public class MenuQueryDto diff --git a/ZR.Model/System/Dto/RoleMenuExportDto.cs b/ZR.Model/System/Dto/RoleMenuExportDto.cs new file mode 100644 index 0000000..9145137 --- /dev/null +++ b/ZR.Model/System/Dto/RoleMenuExportDto.cs @@ -0,0 +1,30 @@ +using MiniExcelLibs.Attributes; +using SqlSugar.DbConvert; +using ZR.Model.System.Enums; + +namespace ZR.Model.System.Dto +{ + public class RoleMenuExportDto + { + /// + /// 一级目录名 + /// + [ExcelColumn(Name = "菜单", Width = 50)] + public string MenuName { get; set; } + //[ExcelColumn(Name = "菜单名", Width = 20)] + //public string MenuName1 { get; set; } + //[ExcelColumn(Name = "权限按钮", Width = 20)] + //public string MenuName2 { get; set; } + [ExcelColumn(Name = "路径", Width = 20)] + public string Path { get; set; } + [ExcelColumn(Name = "组件名", Width = 20)] + public string Component { get; set; } + [ExcelColumn(Name = "权限字符", Width = 20)] + public string Perms { get; set; } + //[ExcelColumn(Name = "菜单类型")] + //[SqlSugar.SugarColumn(SqlParameterDbType = typeof(EnumToStringConvert))] + //public MenuType MenuType { get; set; } + //[ExcelColumn(Name = "菜单状态")] + //public MenuStatus Status { get; set; } + } +} diff --git a/ZR.Model/System/Enums/MenuStatus.cs b/ZR.Model/System/Enums/MenuStatus.cs new file mode 100644 index 0000000..b648ca1 --- /dev/null +++ b/ZR.Model/System/Enums/MenuStatus.cs @@ -0,0 +1,15 @@ +using System.ComponentModel; + +namespace ZR.Model.System.Enums +{ + /// + /// 菜单状态(0正常 1停用) + /// + public enum MenuStatus + { + [Description("正常")] + 正常 = 0, + [Description("停用")] + 停用 = 1, + } +} diff --git a/ZR.Model/System/Enums/MenuType.cs b/ZR.Model/System/Enums/MenuType.cs new file mode 100644 index 0000000..4325223 --- /dev/null +++ b/ZR.Model/System/Enums/MenuType.cs @@ -0,0 +1,19 @@ +using System.ComponentModel; + +namespace ZR.Model.System.Enums +{ + /// + /// M目录 C菜单 F按钮 L链接 + /// + public enum MenuType + { + [Description("目录")] + M, + [Description("菜单")] + C, + [Description("按钮")] + F, + [Description("链接")] + L + } +} diff --git a/ZR.Service/System/IService/ISysMenuService.cs b/ZR.Service/System/IService/ISysMenuService.cs index 78f8a36..29bd021 100644 --- a/ZR.Service/System/IService/ISysMenuService.cs +++ b/ZR.Service/System/IService/ISysMenuService.cs @@ -40,6 +40,8 @@ namespace ZR.Service.System.IService List BuildMenuTreeSelect(List menus); void AddSysMenu(GenTable genTableInfo, string permPrefix, bool showEdit, bool showExport); + List SelectTreeMenuListByRoles(MenuQueryDto menu, List roles); + List SelectRoleMenuListByRole(MenuQueryDto menu, int roleId); } /// diff --git a/ZR.Service/System/SysMenuService.cs b/ZR.Service/System/SysMenuService.cs index cb989f6..045046b 100644 --- a/ZR.Service/System/SysMenuService.cs +++ b/ZR.Service/System/SysMenuService.cs @@ -1,5 +1,6 @@ using Infrastructure.Attribute; using Infrastructure.Extensions; +using JinianNet.JNTemplate.Dynamic; using SqlSugar; using System; using System.Collections.Generic; @@ -7,6 +8,7 @@ using System.Linq; using ZR.Common; using ZR.Model.System; using ZR.Model.System.Dto; +using ZR.Model.System.Enums; using ZR.Model.System.Generate; using ZR.Model.System.Vo; using ZR.Service.System.IService; @@ -220,7 +222,7 @@ namespace ZR.Service /// /// 用户角色集合 /// - private List SelectTreeMenuListByRoles(MenuQueryDto menu, List roles) + public List SelectTreeMenuListByRoles(MenuQueryDto menu, List roles) { var roleMenus = Context.Queryable() .Where(r => roles.Contains(r.Role_id)) @@ -237,6 +239,35 @@ namespace ZR.Service .ToTree(it => it.Children, it => it.ParentId, 0); } + /// + /// 根据用户查询系统菜单列表 + /// + /// + /// 用户角色 + /// + public List SelectRoleMenuListByRole(MenuQueryDto menu, int roleId) + { + var menuIds = Context.Queryable() + .Where(r => r.Role_id == roleId) + .Select(f => f.Menu_id).Distinct().ToList(); + + return Context.Queryable() + .InnerJoin((t1, t2) => t1.MenuId == t2.ParentId) + .InnerJoin((t1, t2, t3) => t2.MenuId == t3.ParentId) + .Where((t1, t2, t3) => menuIds.Contains(t1.MenuId)) + .Select((t1, t2, t3) => new RoleMenuExportDto() + { + MenuName = $"{t1.MenuName}->{t2.MenuName}->{t3.MenuName}", + //MenuName1 = t2.MenuName, + //MenuName2 = t3.MenuName, + Path = t2.Path, + Component = t2.Component, + Perms = t3.Perms, + //MenuType = (MenuType)Enum.Parse(typeof(MenuType), t3.MenuType) //(MenuType)t3.MenuType, + //Status = t3.Status + }).ToList(); + } + /// /// 获取所有菜单 ///