2018-10-19 23:09:52 +08:00
|
|
|
|
using Bootstrap.Security;
|
2018-10-28 23:35:23 +08:00
|
|
|
|
using Bootstrap.Security.DataAccess;
|
2018-10-28 15:03:59 +08:00
|
|
|
|
using Longbow.Cache;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using Longbow.Data;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-28 20:50:01 +08:00
|
|
|
|
using System.Linq;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class MenuHelper
|
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-28 15:03:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 12:08:57 +08:00
|
|
|
|
/// <param name="p"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool Save(BootstrapMenu p)
|
2018-10-31 12:08:57 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Menu>().Save(p);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool Delete(IEnumerable<string> value)
|
2018-10-31 12:08:57 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Menu>().Delete(value);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(menuIds: value);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// 通过用户名获得所有菜单
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
public static IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-28 15:03:59 +08:00
|
|
|
|
/// <param name="roleId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<object> RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbContextManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 12:08:57 +08:00
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <param name="menuIds"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <returns></returns>
|
2018-10-31 12:08:57 +08:00
|
|
|
|
public static bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
|
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Menu>().SaveMenusByRoleId(roleId, menuIds);
|
2018-10-31 12:08:57 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List<string>() { roleId });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// <param name="activeUrl"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <returns></returns>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl)
|
|
|
|
|
{
|
|
|
|
|
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0);
|
2019-01-11 23:20:28 +08:00
|
|
|
|
if (appId != "0") menus = menus.Where(m => m.Application == appId);
|
2019-02-21 13:16:18 +08:00
|
|
|
|
return DbHelper.CascadeMenus(menus, activeUrl);
|
2018-10-28 20:50:01 +08:00
|
|
|
|
}
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// 通过当前用户名获得后台菜单,层次化后集合
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
/// <param name="userName">当前登陆的用户名</param>
|
|
|
|
|
/// <param name="activeUrl">当前访问菜单</param>
|
|
|
|
|
/// <param name="connName">连接字符串名称,默认为ba</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<BootstrapMenu> RetrieveSystemMenus(string userName, string activeUrl = null)
|
|
|
|
|
{
|
|
|
|
|
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0);
|
2019-02-21 13:16:18 +08:00
|
|
|
|
return DbHelper.CascadeMenus(menus, activeUrl);
|
2018-10-28 20:50:01 +08:00
|
|
|
|
}
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过当前用户名获得所有菜单,层次化后集合
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-31 16:50:55 +08:00
|
|
|
|
public static IEnumerable<object> RetrieveMenus(string userName)
|
2018-10-28 20:50:01 +08:00
|
|
|
|
{
|
|
|
|
|
var menus = RetrieveAllMenus(userName);
|
2019-01-11 23:20:28 +08:00
|
|
|
|
return DbHelper.CascadeMenus(menus);
|
2018-10-28 20:50:01 +08:00
|
|
|
|
}
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// 通过用户获得所有菜单
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// </summary>
|
2018-10-28 20:50:01 +08:00
|
|
|
|
/// <param name="userName"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
private static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create<Menu>().RetrieveAllMenus(userName), RetrieveMenusAll);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|