feat: 增加登录帮助类用于获取默认导航页面
This commit is contained in:
parent
f18229d58c
commit
36aa19dd6e
|
@ -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
|
|||
/// <param name="remember">Remember.</param>
|
||||
/// <param name="returnUrl"></param>
|
||||
/// <param name="appId"></param>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="userService"></param>
|
||||
/// <param name="dictService"></param>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> 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<IActionResult> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过 appId 获取设置的首页
|
||||
/// </summary>
|
||||
/// <param name="dictService"></param>
|
||||
/// <param name="appId"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace BootstrapAdmin.Web.Pages.Admin;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[AllowAnonymous]
|
||||
public partial class Index
|
||||
{
|
||||
|
||||
}
|
|
@ -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
|
|||
/// </summary>
|
||||
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);
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
using BootstrapAdmin.Web.Core;
|
||||
|
||||
namespace BootstrapAdmin.Web.Utils;
|
||||
|
||||
/// <summary>
|
||||
/// 登录获取默认首页帮助类
|
||||
/// </summary>
|
||||
public static class LoginHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="returnUrl"></param>
|
||||
/// <param name="appId"></param>
|
||||
/// <param name="defaultAppId"></param>
|
||||
/// <param name="userService"></param>
|
||||
/// <param name="dictService"></param>
|
||||
/// <returns></returns>
|
||||
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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue