From 06728127d3814f401c36c07d13f1974f5de793ba Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 12:08:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8Menu=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=88=B0MenuHelper=E7=B1=BB=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/MenusController.cs | 2 +- Bootstrap.Admin/Query/QueryMenuOption.cs | 21 +++++-- Bootstrap.Admin/wwwroot/js/menus.js | 6 +- Bootstrap.DataAccess.MongoDB/Menu.cs | 57 ++++++++++++++++++- .../MongoDbAccessManager.cs | 11 ++++ Bootstrap.DataAccess.SQLite/Menu.cs | 3 +- Bootstrap.DataAccess/Helper/MenuHelper.cs | 45 +++++++++++---- Bootstrap.DataAccess/Menu.cs | 3 - 8 files changed, 125 insertions(+), 23 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/MenusController.cs b/Bootstrap.Admin/Controllers/Api/MenusController.cs index 524feb03..7d73b890 100644 --- a/Bootstrap.Admin/Controllers/Api/MenusController.cs +++ b/Bootstrap.Admin/Controllers/Api/MenusController.cs @@ -20,7 +20,7 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpGet] - public QueryData Get(QueryMenuOption value) + public QueryData Get(QueryMenuOption value) { return value.RetrieveData(User.Identity.Name); } diff --git a/Bootstrap.Admin/Query/QueryMenuOption.cs b/Bootstrap.Admin/Query/QueryMenuOption.cs index 67cb9b51..014731c6 100644 --- a/Bootstrap.Admin/Query/QueryMenuOption.cs +++ b/Bootstrap.Admin/Query/QueryMenuOption.cs @@ -1,5 +1,4 @@ using Bootstrap.DataAccess; -using Bootstrap.Security; using Longbow.Web.Mvc; using System.Linq; @@ -31,7 +30,7 @@ namespace Bootstrap.Admin.Query /// /// /// - public QueryData RetrieveData(string userName) + public QueryData RetrieveData(string userName) { var data = MenuHelper.RetrieveMenusByUserName(userName); if (!string.IsNullOrEmpty(ParentName)) @@ -50,7 +49,7 @@ namespace Bootstrap.Admin.Query { data = data.Where(t => t.IsResource.ToString() == IsResource); } - var ret = new QueryData(); + var ret = new QueryData(); ret.total = data.Count(); switch (Sort) { @@ -78,7 +77,21 @@ namespace Bootstrap.Admin.Query default: break; } - ret.rows = data.Skip(Offset).Take(Limit); + ret.rows = data.Skip(Offset).Take(Limit).Select(p => new + { + p.Id, + p.ParentId, + p.ParentName, + p.Name, + p.Order, + p.Icon, + p.Url, + p.Category, + p.CategoryName, + p.Target, + p.IsResource, + p.ApplicationCode + }); return ret; } } diff --git a/Bootstrap.Admin/wwwroot/js/menus.js b/Bootstrap.Admin/wwwroot/js/menus.js index 8a22dc6a..137b4d3f 100644 --- a/Bootstrap.Admin/wwwroot/js/menus.js +++ b/Bootstrap.Admin/wwwroot/js/menus.js @@ -75,7 +75,11 @@ sortName: 'Order', queryParams: function (params) { return $.extend(params, { parentName: $('#txt_parent_menus_name').val(), name: $("#txt_menus_name").val(), category: $('#sel_menus_category').val(), isresource: $('#sel_menus_res').val() }); }, //传递参数(*) columns: [ - { title: "父级菜单", field: "ParentName", sortable: true }, + { + title: "父级菜单", field: "ParentName", sortable: true, formatter: function (value, row, index) { + return (value === "0" || value === null) ? "" : value; + } + }, { title: "菜单名称", field: "Name", sortable: true }, { title: "菜单序号", field: "Order", sortable: true }, { diff --git a/Bootstrap.DataAccess.MongoDB/Menu.cs b/Bootstrap.DataAccess.MongoDB/Menu.cs index ffaaabb2..d6a53a69 100644 --- a/Bootstrap.DataAccess.MongoDB/Menu.cs +++ b/Bootstrap.DataAccess.MongoDB/Menu.cs @@ -1,6 +1,7 @@ using Bootstrap.Security; using MongoDB.Driver; using System.Collections.Generic; +using System.Linq; namespace Bootstrap.DataAccess.MongoDB { @@ -16,8 +17,60 @@ namespace Bootstrap.DataAccess.MongoDB /// public override IEnumerable RetrieveAllMenus(string userName) { - var menus = MongoDbAccessManager.DBAccess.GetCollection("Navigations"); - return menus.Find(FilterDefinition.Empty).ToList(); + var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单"); + + var menus = MongoDbAccessManager.Menus.Find(FilterDefinition.Empty).ToList(); + menus.ForEach(m => + { + m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name; + if (m.ParentId != "0") m.ParentName = menus.FirstOrDefault(p => p.Id == m.ParentId)?.Name; + }); + return menus; + } + + /// + /// + /// + /// + /// + public override bool SaveMenu(BootstrapMenu p) + { + if (p.Id == "0") + { + p.Id = null; + MongoDbAccessManager.Menus.InsertOne(p); + return true; + } + else + { + var update = Builders.Update.Set(md => md.ParentId, p.ParentId) + .Set(md => md.Name, p.Name) + .Set(md => md.Order, p.Order) + .Set(md => md.Icon, p.Icon) + .Set(md => md.Url, p.Url) + .Set(md => md.Category, p.Category) + .Set(md => md.Target, p.Target) + .Set(md => md.IsResource, p.IsResource) + .Set(md => md.ApplicationCode, p.ApplicationCode); + MongoDbAccessManager.Menus.UpdateOne(md => md.Id == p.Id, update); + return true; + } + } + + /// + /// + /// + /// + /// + public override bool DeleteMenu(IEnumerable value) + { + var list = new List>(); + foreach (var id in value) + { + list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id))); + } + MongoDbAccessManager.Menus.BulkWrite(list); + return true; } } } diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index 855c7ea5..ff84eaef 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -103,6 +103,17 @@ namespace Bootstrap.DataAccess.MongoDB } } + /// + /// + /// + public static IMongoCollection Menus + { + get + { + return DBAccess.GetCollection("Navigations"); + } + } + private static void InitDb() { var connectString = DbAdapterManager.GetConnectionString("ba"); diff --git a/Bootstrap.DataAccess.SQLite/Menu.cs b/Bootstrap.DataAccess.SQLite/Menu.cs index 5ab50c85..8da7ba2d 100644 --- a/Bootstrap.DataAccess.SQLite/Menu.cs +++ b/Bootstrap.DataAccess.SQLite/Menu.cs @@ -32,7 +32,6 @@ namespace Bootstrap.DataAccess.SQLite DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); transaction.CommitTransaction(); - CacheCleanUtility.ClearCache(menuIds: value); ret = true; } catch (Exception ex) @@ -44,6 +43,7 @@ namespace Bootstrap.DataAccess.SQLite } return ret; } + /// /// /// 通过角色ID保存当前授权菜单 @@ -71,7 +71,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Helper/MenuHelper.cs b/Bootstrap.DataAccess/Helper/MenuHelper.cs index 299e6b76..6e290ae8 100644 --- a/Bootstrap.DataAccess/Helper/MenuHelper.cs +++ b/Bootstrap.DataAccess/Helper/MenuHelper.cs @@ -16,41 +16,63 @@ namespace Bootstrap.DataAccess /// /// public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId"; + /// /// /// public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; + + /// + /// + /// + /// + /// + public static bool SaveMenu(BootstrapMenu p) + { + var ret = DbAdapterManager.Create().SaveMenu(p); + if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); + return ret; + } + /// /// /// /// /// - public static bool SaveMenu(BootstrapMenu value) => DbAdapterManager.Create().SaveMenu(value); - /// - /// - /// - /// - /// - public static bool DeleteMenu(IEnumerable value) => DbAdapterManager.Create().DeleteMenu(value); + public static bool DeleteMenu(IEnumerable value) + { + var ret = DbAdapterManager.Create().DeleteMenu(value); + if (ret) CacheCleanUtility.ClearCache(menuIds: value); + return ret; + } + /// /// 通过用户名获得所有菜单 /// /// /// public static IEnumerable RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName); + /// /// /// /// /// public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); + /// /// /// - /// - /// + /// + /// /// - public static bool SaveMenusByRoleId(string id, IEnumerable value) => DbAdapterManager.Create().SaveMenusByRoleId(id, value); + public static bool SaveMenusByRoleId(string roleId, IEnumerable menuIds) + { + var ret = DbAdapterManager.Create().SaveMenusByRoleId(roleId, menuIds); + if (ret) CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List() { roleId }); + return ret; + } + /// /// /// @@ -66,6 +88,7 @@ namespace Bootstrap.DataAccess DbHelper.CascadeMenus(menus, root); return root; } + /// /// 通过当前用户名获得后台菜单,层次化后集合 /// @@ -82,6 +105,7 @@ namespace Bootstrap.DataAccess DbHelper.CascadeMenus(menus, root); return root; } + /// /// 通过当前用户名获得所有菜单,层次化后集合 /// @@ -94,6 +118,7 @@ namespace Bootstrap.DataAccess DbHelper.CascadeMenus(menus, root); return root; } + /// /// 通过用户获得所有菜单 /// diff --git a/Bootstrap.DataAccess/Menu.cs b/Bootstrap.DataAccess/Menu.cs index 3bd3d378..c52ee502 100644 --- a/Bootstrap.DataAccess/Menu.cs +++ b/Bootstrap.DataAccess/Menu.cs @@ -28,7 +28,6 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; } - CacheCleanUtility.ClearCache(menuIds: value); return ret; } /// @@ -60,7 +59,6 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode)); ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; } - CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); return ret; } /// @@ -120,7 +118,6 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex)