feat: 增加返回首页菜单逻辑

This commit is contained in:
Argo-Tianyi 2022-01-10 02:46:27 +08:00
parent 11984e20e7
commit 19a2123cee
9 changed files with 72 additions and 31 deletions

View File

@ -159,14 +159,12 @@ class DictService : IDict
/// </summary> /// </summary>
/// <param name="appId"></param> /// <param name="appId"></param>
/// <returns></returns> /// <returns></returns>
public string? GetHomeUrlByAppId(string? appId) public string? GetHomeUrlByAppId(string? appId = null)
{ {
string? url = null; string? url = null;
if (!string.IsNullOrEmpty(appId)) var dicts = GetAll();
{ appId ??= "BA";
var dicts = GetAll(); url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
}
return url; return url;
} }
} }

View File

@ -38,6 +38,13 @@ class UserService : IUser
return FreeSql.Ado.Query<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName) r on ra.RoleId = r.ID union select Code from Dicts where Category = @Category and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName)", new { UserName = userName, Category = "应用程序", RoleName = "Administrators" }).ToList(); return FreeSql.Ado.Query<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName) r on ra.RoleId = r.ID union select Code from Dicts where Category = @Category and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName)", new { UserName = userName, Category = "应用程序", RoleName = "Administrators" }).ToList();
} }
/// <summary>
/// 通过用户名获得指定的前台 AppId
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetAppIdByUserName(string userName) => FreeSql.Select<User>().Where(s => s.UserName == userName).ToOne(s => s.App);
public string? GetDisplayName(string? userName) public string? GetDisplayName(string? userName)
{ {
return FreeSql.Select<User>().Where(s => s.UserName == userName).ToOne(s => s.DisplayName); return FreeSql.Select<User>().Where(s => s.UserName == userName).ToOne(s => s.DisplayName);

View File

@ -186,14 +186,12 @@ class DictService : BaseDatabase, IDict
/// </summary> /// </summary>
/// <param name="appId"></param> /// <param name="appId"></param>
/// <returns></returns> /// <returns></returns>
public string? GetHomeUrlByAppId(string? appId) public string? GetHomeUrlByAppId(string? appId = null)
{ {
string? url = null; string? url = null;
if (!string.IsNullOrEmpty(appId)) appId ??= "BA";
{ var dicts = GetAll();
var dicts = GetAll(); url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
}
return url; return url;
} }
} }

View File

@ -53,6 +53,13 @@ class UserService : BaseDatabase, IUser
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public List<string> GetApps(string userName) => Database.Fetch<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators"); public List<string> GetApps(string userName) => Database.Fetch<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0) r on ra.RoleId = r.ID union select Code from Dicts where Category = @1 and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @0 and r.RoleName = @2 union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join {Database.Provider.EscapeSqlIdentifier("Groups")} g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @0 and r.RoleName = @2)", userName, "应用程序", "Administrators");
/// <summary>
/// 通过用户名获得指定的前台 AppId
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetAppIdByUserName(string userName) => Database.FirstOrDefault<User>("Where UserName = @0", userName)?.App;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -148,5 +148,5 @@ public interface IDict
/// </summary> /// </summary>
/// <param name="appId"></param> /// <param name="appId"></param>
/// <returns></returns> /// <returns></returns>
string? GetHomeUrlByAppId(string? appId); string? GetHomeUrlByAppId(string? appId = null);
} }

View File

@ -28,6 +28,13 @@ public interface IUser
/// <returns></returns> /// <returns></returns>
List<string> GetApps(string userName); List<string> GetApps(string userName);
/// <summary>
/// 通过用户名获得指定的前台 AppId
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
string? GetAppIdByUserName(string userName);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -1,10 +1,14 @@
using Microsoft.AspNetCore.Authorization; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Services;
using Microsoft.AspNetCore.Authorization;
namespace BootstrapAdmin.Web.Pages.Home; namespace BootstrapAdmin.Web.Pages.Home;
/// <summary> /// <summary>
/// /// 返回前台页面
/// </summary> /// </summary>
[Route("/")]
[Route("/Index")]
[Route("/Home/Index")] [Route("/Home/Index")]
[Authorize] [Authorize]
public class Index : ComponentBase public class Index : ComponentBase
@ -13,6 +17,34 @@ public class Index : ComponentBase
[NotNull] [NotNull]
private NavigationManager? Navigation { get; set; } private NavigationManager? Navigation { get; set; }
[Inject]
[NotNull]
private BootstrapAppContext? Context { get; set; }
[Inject]
[NotNull]
private IDict? DictsService { get; set; }
[Inject]
[NotNull]
private IUser? UsersService { get; set; }
[NotNull]
private string? Url { get; set; }
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
var appId = UsersService.GetAppIdByUserName(Context.UserName);
Url = DictsService.GetHomeUrlByAppId(appId) ?? "Admin/Index";
#if !DEBUG
Navigation.NavigateTo(Url, true);
#endif
}
#if DEBUG #if DEBUG
/// <summary> /// <summary>
/// ///
@ -20,15 +52,7 @@ public class Index : ComponentBase
/// <param name="firstRender"></param> /// <param name="firstRender"></param>
protected override void OnAfterRender(bool firstRender) protected override void OnAfterRender(bool firstRender)
{ {
Navigation.NavigateTo($"/Admin/Index", true); Navigation.NavigateTo(Url, true);
}
#else
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
Navigation.NavigateTo($"/Admin/Index");
} }
#endif #endif
} }

View File

@ -3,29 +3,29 @@
namespace BootstrapAdmin.Web.Services namespace BootstrapAdmin.Web.Services
{ {
/// <summary> /// <summary>
/// /// AppContext 实体类
/// </summary> /// </summary>
public class BootstrapAppContext public class BootstrapAppContext
{ {
/// <summary> /// <summary>
/// /// 获得/设置 当前网站 AppId
/// </summary> /// </summary>
public string AppId { get; } public string AppId { get; }
/// <summary> /// <summary>
/// /// 获得/设置 当前登录账号
/// </summary> /// </summary>
[NotNull] [NotNull]
public string? UserName { get; set; } public string? UserName { get; set; }
/// <summary> /// <summary>
/// /// 获得/设置 当前用户显示名称
/// </summary> /// </summary>
[NotNull] [NotNull]
public string? DisplayName { get; internal set; } public string? DisplayName { get; internal set; }
/// <summary> /// <summary>
/// /// 构造函数
/// </summary> /// </summary>
/// <param name="configuration"></param> /// <param name="configuration"></param>
public BootstrapAppContext(IConfiguration configuration) public BootstrapAppContext(IConfiguration configuration)

View File

@ -20,11 +20,11 @@ namespace BootstrapAdmin.Web.Shared
[Inject] [Inject]
[NotNull] [NotNull]
private IDict? DictsService { get; set; } private AuthenticationStateProvider? AuthenticationStateProvider { get; set; }
[Inject] [Inject]
[NotNull] [NotNull]
private AuthenticationStateProvider? AuthenticationStateProvider { get; set; } private IDict? DictsService { get; set; }
[Inject] [Inject]
[NotNull] [NotNull]