feat: 后台应用通过当前请求拼接前台地址

This commit is contained in:
Argo-Tianyi 2022-01-25 00:08:29 +08:00
parent 1a896cf994
commit 10e5b1d26c
7 changed files with 48 additions and 28 deletions

View File

@ -1,29 +1,32 @@
using BootstrapAdmin.Web.Services;
namespace BootstrapAdmin.Web
namespace BootstrapAdmin.Web;
/// <summary>
///
/// </summary>
public partial class App
{
/// <summary>
///
/// </summary>
public partial class App
public string? Title { get; set; }
[Inject]
[NotNull]
private BootstrapAppContext? AppContext { get; set; }
[Inject]
[NotNull]
private NavigationManager? NavigationManager { get; set; }
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
/// <summary>
///
/// </summary>
public string? Title { get; set; }
base.OnInitialized();
[Inject]
[NotNull]
private BootstrapAppContext? AppContext { get; set; }
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
//Title = DictHelper.RetrieveWebTitle(AppContext.AppId);
}
AppContext.BaseUri = NavigationManager.ToAbsoluteUri(NavigationManager.BaseUri);
}
}

View File

@ -1,5 +1,4 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Services;
using Microsoft.JSInterop;
namespace BootstrapAdmin.Web.Components;
@ -75,7 +74,6 @@ public partial class AdminLogin : IDisposable
base.OnInitialized();
Title = DictsService.GetWebTitle();
PostUrl = QueryHelper.AddQueryString("/Account/Login", new Dictionary<string, string?>
{
["ReturnUrl"] = ReturnUrl,

View File

@ -52,7 +52,9 @@ namespace BootstrapAdmin.Web.Controllers
period = dictService.GetCookieExpiresPeriod();
}
return auth ? await SignInAsync(userName, LoginHelper.GetDefaultUrl(userName, returnUrl, appId, context.AppId, userService, dictService), persistent, period) : RedirectLogin(returnUrl);
context.UserName = userName;
context.BaseUri = new Uri($"{Request.Scheme}://{Request.Host}/");
return auth ? await SignInAsync(userName, LoginHelper.GetDefaultUrl(context, returnUrl, 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)
@ -139,7 +141,10 @@ namespace BootstrapAdmin.Web.Controllers
{
userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles);
}
return auth ? await SignInAsync(phone, LoginHelper.GetDefaultUrl(phone, returnUrl, appId, context.AppId, userService, dictService), persistent, period, MobileSchema) : RedirectLogin(returnUrl);
context.UserName = phone;
context.BaseUri = new Uri(Request.Path.Value!);
return auth ? await SignInAsync(phone, LoginHelper.GetDefaultUrl(context, returnUrl, appId, userService, dictService), persistent, period, MobileSchema) : RedirectLogin(returnUrl);
}
#endregion

View File

@ -39,7 +39,7 @@ public class Index : ComponentBase
protected override void OnInitialized()
{
// 查看是否自定义前台
Url = LoginHelper.GetDefaultUrl(Context.UserName, null, null, Context.AppId, UsersService, DictsService);
Url = LoginHelper.GetDefaultUrl(Context, null, null, UsersService, DictsService);
#if !DEBUG
Navigation.NavigateTo(Url, true);

View File

@ -24,6 +24,12 @@ namespace BootstrapAdmin.Web.Services
[NotNull]
public string? DisplayName { get; internal set; }
/// <summary>
/// 获得/设置 应用程序基础地址 如 http://localhost:5210
/// </summary>
[NotNull]
public Uri? BaseUri { get; set; }
/// <summary>
/// 构造函数
/// </summary>

View File

@ -1,4 +1,5 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Services;
namespace BootstrapAdmin.Web.Utils;
@ -10,18 +11,21 @@ public static class LoginHelper
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="context"></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)
public static string GetDefaultUrl(BootstrapAppContext context, string? returnUrl, string? appId, IUser userService, IDict dictService)
{
if (string.IsNullOrEmpty(returnUrl))
{
// 查找 User 设置的默认应用
var userName = context.UserName;
var defaultAppId = context.AppId;
var schema = context.BaseUri.Scheme;
var host = context.BaseUri.Host;
appId ??= userService.GetAppIdByUserName(userName) ?? defaultAppId;
if (appId == defaultAppId && dictService.GetEnableDefaultApp())
@ -32,7 +36,11 @@ public static class LoginHelper
if (!string.IsNullOrEmpty(appId))
{
returnUrl = dictService.GetHomeUrlByAppId(appId);
var appUrl = dictService.GetHomeUrlByAppId(appId);
if (!string.IsNullOrEmpty(appUrl))
{
returnUrl = string.Format(appUrl, schema, host);
}
}
}