BootstrapAdmin/Bootstrap.DataAccess/Helper/MenuHelper.cs

134 lines
5.1 KiB
C#
Raw Normal View History

using Bootstrap.Security;
using Bootstrap.Security.DataAccess;
using Longbow.Cache;
2018-10-19 23:09:52 +08:00
using Longbow.Data;
using Microsoft.AspNetCore.Http;
using System;
2018-10-19 23:09:52 +08:00
using System.Collections.Generic;
using System.Linq;
2018-10-19 23:09:52 +08:00
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
public static class MenuHelper
{
/// <summary>
///
/// </summary>
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="p"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
public static bool Save(BootstrapMenu p)
{
// 不允许保存系统菜单与前台演示系统的默认菜单
if (DictHelper.RetrieveSystemModel() && (p.Category == "0" || p.Application == "2")) return true;
2019-06-01 16:57:33 +08:00
if (p.Id == string.Empty) p.Id = null;
var ret = DbContextManager.Create<Menu>().Save(p);
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>
public static bool Delete(IEnumerable<string> value)
{
if (DictHelper.RetrieveSystemModel())
{
// 不允许删除系统菜单与前台演示系统的默认菜单
2019-06-01 16:57:33 +08:00
var systemMenus = RetrieveAllMenus("Admin").Where(m => m.Category == "0" || m.Application == "2");
value = value.Where(v => !systemMenus.Any(m => m.Id == v));
if (!value.Any()) return true;
}
var ret = DbContextManager.Create<Menu>().Delete(value);
if (ret) CacheCleanUtility.ClearCache(menuIds: value);
return ret;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过用户名获得所有菜单
2018-10-19 23:09:52 +08:00
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName);
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
public static IEnumerable<object> RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbContextManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="menuIds"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
public static bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
{
var ret = DbContextManager.Create<Menu>().SaveMenusByRoleId(roleId, menuIds);
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="appId"></param>
/// <param name="userName"></param>
/// <param name="activeUrl"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl)
{
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0);
if (appId != "0") menus = menus.Where(m => m.Application == appId);
return DbHelper.CascadeMenus(menus, activeUrl);
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过当前用户名获得后台菜单,层次化后集合
/// </summary>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</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);
return DbHelper.CascadeMenus(menus, activeUrl);
}
/// <summary>
/// 通过当前用户名获得所有菜单,层次化后集合
2018-10-19 23:09:52 +08:00
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenus(string userName)
{
var menus = RetrieveAllMenus(userName);
return DbHelper.CascadeMenus(menus);
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过用户获得所有菜单
2018-10-19 23:09:52 +08:00
/// </summary>
/// <param name="userName"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
public 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
}
}