From 36aa19dd6e3144fa31c497c9c7f78966cc9ceb12 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 20 Jan 2022 10:52:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=B8=AE=E5=8A=A9=E7=B1=BB=E7=94=A8=E4=BA=8E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=AF=BC=E8=88=AA=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 16 +++----- .../Pages/Admin/Index.razor.cs | 12 ++++++ .../BootstrapAdmin.Web/Pages/Home/Index.cs | 5 ++- .../BootstrapAdmin.Web/Utils/LoginHelper.cs | 41 +++++++++++++++++++ 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Index.razor.cs create mode 100644 src/blazor/admin/BootstrapAdmin.Web/Utils/LoginHelper.cs diff --git a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs index 45c1bbb8..649f426a 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs @@ -1,6 +1,7 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Services; using BootstrapAdmin.Web.Services.SMS; +using BootstrapAdmin.Web.Utils; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; @@ -28,10 +29,12 @@ namespace BootstrapAdmin.Web.Controllers /// Remember. /// /// + /// /// /// [HttpPost] public async Task Login(string userName, string password, [FromQuery] string? remember, [FromQuery] string? returnUrl, [FromQuery] string? appId, + [FromServices] BootstrapAppContext context, [FromServices] IUser userService, [FromServices] IDict dictService) { @@ -48,7 +51,8 @@ namespace BootstrapAdmin.Web.Controllers // Cookie 持久化 period = dictService.GetCookieExpiresPeriod(); } - return auth ? await SignInAsync(userName, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period) : RedirectLogin(returnUrl); + + return auth ? await SignInAsync(userName, LoginHelper.GetDefaultUrl(userName, returnUrl, appId, context.AppId, userService, dictService), persistent, period) : RedirectLogin(returnUrl); } private async Task SignInAsync(string userName, string returnUrl, bool persistent, int period = 0, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme) @@ -70,14 +74,6 @@ namespace BootstrapAdmin.Web.Controllers return Redirect(returnUrl); } - /// - /// 通过 appId 获取设置的首页 - /// - /// - /// - /// - private static string GetAppHomeUrl(IDict dictService, string? appId) => dictService.GetHomeUrlByAppId(appId) ?? "/Admin/Index"; - private IActionResult RedirectLogin(string? returnUrl = null) { var url = returnUrl; @@ -143,7 +139,7 @@ namespace BootstrapAdmin.Web.Controllers { userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles); } - return auth ? await SignInAsync(phone, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period, MobileSchema) : RedirectLogin(returnUrl); + return auth ? await SignInAsync(phone, LoginHelper.GetDefaultUrl(phone, returnUrl, appId, context.AppId, userService, dictService), persistent, period, MobileSchema) : RedirectLogin(returnUrl); } #endregion diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Index.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Index.razor.cs new file mode 100644 index 00000000..26804a95 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Index.razor.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Authorization; + +namespace BootstrapAdmin.Web.Pages.Admin; + +/// +/// +/// +[AllowAnonymous] +public partial class Index +{ + +} diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Home/Index.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Home/Index.cs index 0de79cb0..eb8f144b 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Home/Index.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Home/Index.cs @@ -1,5 +1,6 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Services; +using BootstrapAdmin.Web.Utils; using Microsoft.AspNetCore.Authorization; namespace BootstrapAdmin.Web.Pages.Home; @@ -37,8 +38,8 @@ public class Index : ComponentBase /// protected override void OnInitialized() { - var appId = UsersService.GetAppIdByUserName(Context.UserName); - Url = DictsService.GetHomeUrlByAppId(appId) ?? "/Admin/Index"; + // 查看是否自定义前台 + Url = LoginHelper.GetDefaultUrl(Context.UserName, null, null, Context.AppId, UsersService, DictsService); #if !DEBUG Navigation.NavigateTo(Url, true); diff --git a/src/blazor/admin/BootstrapAdmin.Web/Utils/LoginHelper.cs b/src/blazor/admin/BootstrapAdmin.Web/Utils/LoginHelper.cs new file mode 100644 index 00000000..0b85bd60 --- /dev/null +++ b/src/blazor/admin/BootstrapAdmin.Web/Utils/LoginHelper.cs @@ -0,0 +1,41 @@ +using BootstrapAdmin.Web.Core; + +namespace BootstrapAdmin.Web.Utils; + +/// +/// 登录获取默认首页帮助类 +/// +public static class LoginHelper +{ + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static string GetDefaultUrl(string userName, string? returnUrl, string? appId, string defaultAppId, IUser userService, IDict dictService) + { + if (string.IsNullOrEmpty(returnUrl)) + { + // 查找 User 设置的默认应用 + appId ??= userService.GetAppIdByUserName(userName) ?? defaultAppId; + + if (appId == defaultAppId && dictService.GetEnableDefaultApp()) + { + // 开启默认应用 + appId = dictService.GetApps().FirstOrDefault(d => d.Key != defaultAppId).Key; + } + + if (!string.IsNullOrEmpty(appId)) + { + returnUrl = dictService.GetHomeUrlByAppId(appId); + } + } + + return returnUrl ?? "/Admin/Index"; + } +}