自动生成菜单,相应控制其方法加上DisplayName、Description特性

This commit is contained in:
xxred
2018-11-07 17:15:42 +08:00
parent 787a76798f
commit 2ab7ecc9b0
23 changed files with 557 additions and 3 deletions
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
@@ -12,6 +13,8 @@ using Microsoft.AspNetCore.Http;
namespace COMCMS.Web.Areas.AdminCP.Controllers
{
[DisplayName("文章系统")]
[Description("文章系统管理,包括文章、文章栏目")]
public class ArticleController : AdminBaseController
{
#region 文章栏目
@@ -20,6 +23,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
/// </summary>
/// <returns></returns>
[MyAuthorize( "viewlist", "articlecategory")]
[DisplayName("文章栏目")]
public IActionResult ArticleCategoryList()
{
IList<ArticleCategory> list = ArticleCategory.GetListTree(0, -1, false, true);
@@ -189,6 +193,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
/// </summary>
/// <returns></returns>
[MyAuthorize("viewlist", "article")]
[DisplayName("文章")]
public IActionResult ArticleList()
{
//获取上级栏目
@@ -99,7 +99,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
ViewBag.ListKinds = list;
return View();
}
[MyAuthorize("viewlist", "guestbook")]
[MyAuthorize("viewlist", "guestbook","JSON")]
public IActionResult GetGuesbookList(string keyword, int page = 1, int limit = 20)
{
int numPerPage, currentPage, startRowIndex;
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
@@ -19,6 +20,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
/// <summary>
/// 后台用户管理控制器
/// </summary>
[DisplayName("管理员")]
public class MemberController : AdminBaseController
{
#region 修改密码
@@ -115,6 +117,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
#region 管理组管理
[MyAuthorize("viewlist", "adminrole")]
[DisplayName("管理组")]
public IActionResult AdminRole()
{
IList<AdminRoles> list = AdminRoles.FindAll(AdminRoles._.Id > 0, AdminRoles._.Rank.Asc(), null, 0, 0);
@@ -366,6 +369,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
#region 管理员管理
//管理员管理
[MyAuthorize("viewlist", "admins")]
[DisplayName("管理员")]
public IActionResult Admins()
{
//加载管理组
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
@@ -13,6 +14,7 @@ using Microsoft.AspNetCore.Http;
namespace COMCMS.Web.Areas.AdminCP.Controllers
{
[Area("AdminCP")]
[DisplayName("后台权限")]
public class PermissionController : AdminBaseController
{
#region 事件管理
@@ -107,6 +109,7 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
#region 后台栏目管理
//后台菜单列表
[MyAuthorize( "viewlist", "adminmenu")]
[DisplayName("后台栏目")]
public IActionResult AdminMenuList()
{
IList<AdminMenu> list = AdminMenu.GetListTree(0, -1, false, true);
+1 -1
View File
@@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="MailKit" Version="2.0.7" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.5" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.5" />
<PackageReference Include="MimeKit" Version="2.0.7" />
<PackageReference Include="MySql.Data" Version="8.0.13" />
+137
View File
@@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using COMCMS.Core;
using Microsoft.AspNetCore.Mvc;
using NewLife.Reflection;
using XCode;
namespace COMCMS.Web.Common
{
public class MenuRegister
{
private static void StartRegister()
{
// 操作项定义
var menuEventDic = new Dictionary<string, KeyValuePair<int, string>>
{
{ "add",new KeyValuePair<int, string>(1,"添加")},
{ "edit",new KeyValuePair<int, string>(2,"修改")},
{ "del",new KeyValuePair<int, string>(3,"删除")},
{ "view",new KeyValuePair<int, string>(4,"查看")},
{ "viewlist",new KeyValuePair<int, string>(5,"查看列表")},
{ "import",new KeyValuePair<int, string>(6,"导入")},
{ "export",new KeyValuePair<int, string>(7,"导出")},
{ "filter",new KeyValuePair<int, string>(8,"搜索")},
{ "batch",new KeyValuePair<int, string>(9,"批量操作")},
{ "recycle",new KeyValuePair<int, string>(10,"回收站")},
{ "confirm",new KeyValuePair<int, string>(11,"确认")},
};
var asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (var asmItem in asms)
{
var types = asmItem.GetTypes().Where(e => e.Name.EndsWith("Controller")).ToList();
if (types.Count == 0) continue;
foreach (var type in types)
{
// 控制器名称
var ctrName = type.Name.TrimEnd("Controller");
// 控制器展示名
var ctrDpName = type.GetDisplayName();
var ctrDes = type.GetDescription();
var url = ctrName;
var fAdminMenu = AdminMenu.Find(AdminMenu._.MenuKey == ctrName + "Sys" & AdminMenu._.Link == "#");
var nAdminMenu = new AdminMenu
{
Description = ctrDes,
IsHide = 0,
Level = 0,
Link = "#",
Location = "0,",
MenuKey = ctrName + "Sys",
MenuName = ctrDpName,
PId = 0
};
var pAdminMenu = fAdminMenu ?? nAdminMenu;
if (pAdminMenu.Id > 0)
{
pAdminMenu.Description = nAdminMenu.Description;
pAdminMenu.MenuName = nAdminMenu.MenuName;
}
pAdminMenu.Save();
// 控制器所有包含MyAuthorizeAttribute的方法
var acts = type.GetMethods()
.Where(w => w.IsDefined(typeof(MyAuthorizeAttribute)))
.ToList();
if (!acts.Any()) continue;
var eventKeyDic = new Dictionary<string, int>();
var adminMenuEventList = new List<AdminMenuEvent>();
foreach (var method in acts)
{
var dn = method.GetDisplayName();
var methodDes = method.GetDescription();
var actName = method.Name;
var myAuthorize = method.GetCustomAttribute<MyAuthorizeAttribute>();
var menuKey = myAuthorize.GetValue("_menuKey").ToString();
var eventKey = myAuthorize.GetValue("_eventKey").ToString();
var returnType = myAuthorize.GetValue("_returnType").ToString();
//保存菜单
if (eventKey == "viewlist" && returnType == "HTML")
{
var adminMenu = AdminMenu.Find(AdminMenu._.MenuKey == menuKey & AdminMenu._.Link == url + "/" + actName) ?? new AdminMenu
{
Description = methodDes,
IsHide = 0,
Level = pAdminMenu.Level + 1,
Link = url + "/" + actName,
Location = pAdminMenu.Location + ",",
MenuKey = menuKey,
MenuName = dn,
PId = pAdminMenu.Id
};
adminMenu.Save();
eventKeyDic[menuKey] = adminMenu.Id;
}
// 保存操作
var adminmenuevent = AdminMenuEvent.Find(AdminMenuEvent._.MenuId == menuEventDic[eventKey].Key
& AdminMenuEvent._.MenuKey == menuKey & AdminMenuEvent._.EventKey == eventKey)
?? new AdminMenuEvent
{
MenuKey = menuKey,
MenuId = menuEventDic[eventKey].Key,
EventKey = eventKey,
EventName = menuEventDic[eventKey].Value
};
adminMenuEventList.Add(adminmenuevent);
}
adminMenuEventList.ForEach(f => f.MenuId = eventKeyDic.ContainsKey(f.MenuKey) ? eventKeyDic[f.MenuKey] : 0);
adminMenuEventList.Save();
}
}
}
public static void Register()
{
Task.Run(() => { StartRegister(); }).LogException();
}
}
}
+3
View File
@@ -43,6 +43,9 @@ namespace COMCMS.Web
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// 注册菜单
MenuRegister.Register();
//注入自己的HttpContext
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//services.AddMvc().AddSessionStateTempDataProvider();