重构代码:精简通过角色ID保存菜单选项方法

This commit is contained in:
Argo-MacBookPro 2018-09-13 17:55:24 +08:00
parent efc0106f39
commit 8dd846824c
5 changed files with 19 additions and 37 deletions

View File

@ -16,7 +16,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class MenusController : Controller public class MenusController : Controller
{ {
/// <summary> /// <summary>
/// /// 获得所有菜单列表调用
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
/// <returns></returns> /// <returns></returns>
@ -26,7 +26,7 @@ namespace Bootstrap.Admin.Controllers.Api
return value.RetrieveData(User.Identity.Name); return value.RetrieveData(User.Identity.Name);
} }
/// <summary> /// <summary>
/// /// 保存菜单调用
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
[HttpPost] [HttpPost]
@ -35,7 +35,7 @@ namespace Bootstrap.Admin.Controllers.Api
return MenuHelper.SaveMenu(value); return MenuHelper.SaveMenu(value);
} }
/// <summary> /// <summary>
/// /// 删除菜单调用
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
[HttpDelete] [HttpDelete]
@ -68,26 +68,15 @@ namespace Bootstrap.Admin.Controllers.Api
return ret; return ret;
} }
/// <summary> /// <summary>
/// /// 角色管理页面分配菜单时调用
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id">角色ID</param>
/// <param name="value"></param> /// <param name="value">菜单ID集合</param>
/// <returns></returns> /// <returns></returns>
[HttpPut("{id}")] [HttpPut("{id}")]
public bool Put(int id, [FromBody]JObject value) public bool Put(int id, [FromBody]IEnumerable<int> value)
{ {
var ret = false; return MenuHelper.SaveMenusByRoleId(id, value);
dynamic json = value;
string menuIds = json.menuIds.ToString();
switch ((string)json.type)
{
case "role":
ret = MenuHelper.SaveMenusByRoleId(id, menuIds);
break;
default:
break;
}
return ret;
} }
} }
} }

View File

@ -87,17 +87,10 @@
}, },
'#btnSubmitMenu': function (row) { '#btnSubmitMenu': function (row) {
var roleId = row.Id; var roleId = row.Id;
var type = $btnSubmitMenu.data('type'); var menuIds = $nestMenuInput.find('input:checkbox:checked').map(function (index, element) {
switch (type) { return $(element).val();
case "menu": }).toArray();
var menuIds = $nestMenuInput.find('input:checkbox:checked').map(function (index, element) { $.bc({ id: roleId, url: Menu.url, method: "put", data: menuIds, modal: '#dialogMenu', title: Menu.title });
return $(element).val();
}).toArray().join(',');
break;
default:
break;
}
$.bc({ id: roleId, url: Menu.url, method: "put", data: { type: "role", menuIds: menuIds }, modal: '#dialogMenu', title: Menu.title });
} }
} }
}, },

View File

@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess
/// <param name="menuIds"></param> /// <param name="menuIds"></param>
/// <param name="dictIds"></param> /// <param name="dictIds"></param>
/// <param name="cacheKey"></param> /// <param name="cacheKey"></param>
internal static void ClearCache(string roleIds = null, string userIds = null, string groupIds = null, string menuIds = null, string dictIds = null, string cacheKey = null) internal static void ClearCache(string roleIds = null, string userIds = null, string groupIds = null, IEnumerable<int> menuIds = null, string dictIds = null, string cacheKey = null)
{ {
var cacheKeys = new List<string>(); var cacheKeys = new List<string>();
var corsKeys = new List<string>(); var corsKeys = new List<string>();
@ -62,7 +62,7 @@ namespace Bootstrap.DataAccess
} }
if (menuIds != null) if (menuIds != null)
{ {
menuIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id => menuIds.ToList().ForEach(id =>
{ {
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id));
}); });

View File

@ -32,7 +32,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count(); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == value.Count();
} }
CacheCleanUtility.ClearCache(menuIds: ids); CacheCleanUtility.ClearCache(menuIds: value);
return ret; return ret;
} }
/// <summary> /// <summary>
@ -64,7 +64,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
} }
CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? string.Empty : p.Id.ToString()); CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
return ret; return ret;
} }
@ -103,13 +103,13 @@ namespace Bootstrap.DataAccess
/// <param name="id"></param> /// <param name="id"></param>
/// <param name="menuIds"></param> /// <param name="menuIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveMenusByRoleId(int id, string menuIds) public static bool SaveMenusByRoleId(int id, IEnumerable<int> menuIds)
{ {
bool ret = false; bool ret = false;
DataTable dt = new DataTable(); DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("NavigationID", typeof(int)); dt.Columns.Add("NavigationID", typeof(int));
if (!string.IsNullOrEmpty(menuIds)) menuIds.Split(',').ToList().ForEach(menuId => dt.Rows.Add(id, Convert.ToInt32(menuId))); menuIds.ToList().ForEach(menuId => dt.Rows.Add(id, menuId));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{ {
try try

View File

@ -223,7 +223,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
} }
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: id.ToString()); CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<int>() { id });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)