chore: 增加菜单服务

This commit is contained in:
Argo-Tianyi 2021-12-11 00:52:43 +08:00
parent 0675e5de8c
commit d9da2119a6
15 changed files with 132 additions and 56 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -7,9 +7,20 @@
</PropertyGroup>
<ItemGroup>
<!--<PackageReference Include="BootstrapBlazor" Version="6.1.1-beta01" />-->
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\BootstrapBlazor\src\BootstrapBlazor\BootstrapBlazor.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="BootstrapBlazor.Components" />
<Using Include="Microsoft.AspNetCore.Components" />
<Using Include="System.Diagnostics.CodeAnalysis" />
</ItemGroup>
</Project>

View File

@ -4,16 +4,16 @@
<name>BootstrapAdmin.DataAccess</name>
</assembly>
<members>
<member name="T:Microsoft.Extensions.DependencyInjection.ServicesExtensions">
<member name="T:BootstrapAdmin.DataAccess.Extensions.MenuItemExtensions">
<summary>
</summary>
</member>
<member name="M:Microsoft.Extensions.DependencyInjection.ServicesExtensions.AddDataAccessServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<member name="M:BootstrapAdmin.DataAccess.Extensions.MenuItemExtensions.Parse(BootstrapAdmin.DataAccess.Models.Menu)">
<summary>
</summary>
<param name="services"></param>
<param name="menu"></param>
<returns></returns>
</member>
<member name="T:BootstrapAdmin.DataAccess.Models.Dict">
@ -126,7 +126,7 @@
</summary>
</member>
<member name="M:BootstrapAdmin.DataAccess.Services.IMenu.GetMenus">
<member name="M:BootstrapAdmin.DataAccess.Services.IMenu.GetAdminMenusByUser(System.String)">
<summary>
</summary>
@ -143,12 +143,37 @@
</summary>
<param name="db"></param>
</member>
<member name="M:BootstrapAdmin.DataAccess.Services.MenuService.GetMenus">
<member name="M:BootstrapAdmin.DataAccess.Services.MenuService.GetAdminMenusByUser(System.String)">
<summary>
</summary>
<returns></returns>
<exception cref="T:System.NotImplementedException"></exception>
</member>
<member name="M:BootstrapAdmin.DataAccess.Services.MenuService.CascadeMenus(System.Collections.Generic.IEnumerable{BootstrapAdmin.DataAccess.Models.Menu})">
<summary>
获得带层次关系的菜单集合
</summary>
<param name="menus">未层次化菜单集合</param>
<returns>带层次化的菜单集合</returns>
</member>
<member name="M:BootstrapAdmin.DataAccess.Services.MenuService.RetrieveAllMenus(System.String)">
<summary>
获得指定用户名可访问的所有菜单集合
</summary>
<param name="userName">当前用户名</param>
<returns>未层次化的菜单集合</returns>
</member>
<member name="T:Microsoft.Extensions.DependencyInjection.ServicesExtensions">
<summary>
</summary>
</member>
<member name="M:Microsoft.Extensions.DependencyInjection.ServicesExtensions.AddDataAccessServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary>
</summary>
<param name="services"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Components.Routing;
namespace BootstrapAdmin.DataAccess.Extensions
{
/// <summary>
///
/// </summary>
public static class MenuItemExtensions
{
/// <summary>
///
/// </summary>
/// <param name="menu"></param>
/// <returns></returns>
public static MenuItem Parse(this Models.Menu menu) => new()
{
Text = menu.Name,
Url = menu.Url.Replace("~", ""),
Icon = menu.Icon,
Match = NavLinkMatch.All,
Target = menu.Target,
Id = menu.Id,
ParentId = menu.ParentId
};
}
}

View File

@ -1,5 +1,8 @@
using BootstrapAdmin.DataAccess.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using PetaPoco;
using PetaPoco.Providers;
namespace Microsoft.Extensions.DependencyInjection
{
@ -16,6 +19,13 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddDataAccessServices(this IServiceCollection services)
{
services.TryAddSingleton<IMenu, MenuService>();
services.TryAddSingleton<IDatabase>(provider =>
{
//TODO: 后期改造成自定适配
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
return new Database<SQLiteDatabaseProvider>(connString);
});
return services;
}
}

View File

@ -78,6 +78,6 @@ namespace BootstrapAdmin.DataAccess.Models
/// <summary>
/// 获得/设置 当前菜单项的所有子菜单集合
/// </summary>
public IEnumerable<Menu> Menus { get; set; } = new List<Menu>();
public List<Menu> Menus { get; } = new List<Menu>();
}
}

View File

@ -11,6 +11,6 @@ namespace BootstrapAdmin.DataAccess.Services
///
/// </summary>
/// <returns></returns>
IEnumerable<Menu> GetMenus();
IEnumerable<MenuItem> GetAdminMenusByUser(string userName);
}
}

View File

@ -1,4 +1,4 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.DataAccess.Extensions;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.Services
@ -20,10 +20,45 @@ namespace BootstrapAdmin.DataAccess.Services
///
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public IEnumerable<Menu> GetMenus()
public IEnumerable<MenuItem> GetAdminMenusByUser(string userName)
{
throw new NotImplementedException();
var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0);
return CascadeMenus(menus);
}
/// <summary>
/// 获得带层次关系的菜单集合
/// </summary>
/// <param name="menus">未层次化菜单集合</param>
/// <returns>带层次化的菜单集合</returns>
public static IEnumerable<MenuItem> CascadeMenus(IEnumerable<Models.Menu> menus)
{
var root = menus.Where(m => m.ParentId == "0")
.OrderBy(m => m.Category).ThenBy(m => m.Application).ThenBy(m => m.Order)
.Select(m => m.Parse())
.ToList();
CascadeMenus(menus, root);
return root;
}
private static void CascadeMenus(IEnumerable<Models.Menu> navs, List<MenuItem> level)
{
level.ForEach(m =>
{
m.Items = navs.Where(sub => sub.ParentId == m.Id).OrderBy(sub => sub.Order).Select(sub => sub.Parse()).ToList();
CascadeMenus(navs, m.Items.ToList());
});
}
/// <summary>
/// 获得指定用户名可访问的所有菜单集合
/// </summary>
/// <param name="userName">当前用户名</param>
/// <returns>未层次化的菜单集合</returns>
private List<Models.Menu> RetrieveAllMenus(string userName)
{
var order = _db.Provider.EscapeSqlIdentifier("Order");
return _db.Fetch<Models.Menu>($"select n.ID, n.ParentId, n.Name, n.{order}, n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application, d.Name as CategoryName, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = 0 left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID", new { UserName = userName, Category = "菜单", RoleName = "Administrators" });
}
}
}

View File

@ -5,10 +5,6 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="6.1.1-beta01" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BootstrapAdmin.DataAccess\BootstrapAdmin.DataAccess.csproj" />
</ItemGroup>

View File

@ -161,11 +161,6 @@
</summary>
<param name="configuration"></param>
</member>
<member name="T:BootstrapAdmin.Web.Services.IBootstrapMenu">
<summary>
</summary>
</member>
<member name="T:BootstrapAdmin.Web.Shared.MainLayout">
<summary>
MainLayout 布局类
@ -197,7 +192,7 @@
PetaPoco ORM 的 IDataService 接口实现
</summary>
</member>
<member name="M:BootstrapBlazor.DataAcces.PetaPoco.DefaultDataService`1.#ctor(Microsoft.Extensions.Configuration.IConfiguration)">
<member name="M:BootstrapBlazor.DataAcces.PetaPoco.DefaultDataService`1.#ctor(PetaPoco.IDatabase)">
<summary>
构造函数
</summary>

View File

@ -1,4 +1,4 @@
@page "/admin/dicts"
@page "/Admin/Dicts"
<BlazorTable TItem="Dict" IsPagination="false" TableSearchModel="DictsSearchModel">
<ColumnsTemplete>

View File

@ -1,3 +1 @@
@attribute [Authorize]
@using BootstrapAdmin.DataAccess.Models

View File

@ -8,7 +8,7 @@
}
},
"profiles": {
"Bootstrap.Admin.Blazor": {
"BootstrapAdmin.Web": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,

View File

@ -19,12 +19,7 @@ namespace BootstrapBlazor.DataAcces.PetaPoco
/// <summary>
/// 构造函数
/// </summary>
public DefaultDataService(IConfiguration configuration)
{
//TODO: 后期改造成自定适配
var connString = configuration.GetConnectionString("bb");
_db = new Database<SQLiteDatabaseProvider>(connString);
}
public DefaultDataService(IDatabase db) => _db = db;
/// <summary>
/// 删除方法

View File

@ -1,9 +0,0 @@
namespace BootstrapAdmin.Web.Services
{
/// <summary>
///
/// </summary>
public interface IBootstrapMenu
{
}
}

View File

@ -1,4 +1,6 @@
namespace BootstrapAdmin.Web.Shared
using BootstrapAdmin.DataAccess.Services;
namespace BootstrapAdmin.Web.Shared
{
/// <summary>
/// MainLayout 布局类
@ -7,6 +9,10 @@
{
private IEnumerable<MenuItem>? MenuItems { get; set; }
[Inject]
[NotNull]
private IMenu? MenuService { get; set; }
/// <summary>
///
/// </summary>
@ -14,19 +20,7 @@
{
base.OnInitialized();
// TODO: 暂时写死 Admin 账号
//MenuItems = DataAccess.MenuHelper.RetrieveSystemMenus("Admin").Select(s => new MenuItem()
//{
// Url = s.Url.Replace("~", ""),
// Text = s.Name,
// Icon = s.Icon,
// Items = s.Menus.Select(x => new MenuItem
// {
// Url = x.Url.Replace("~", ""),
// Text = x.Name,
// Icon = x.Icon
// })
//});
MenuItems = MenuService.GetAdminMenusByUser("Admin");
}
}
}