Skip to content

Commit

Permalink
refactor: 使用 net8 项目模板 (#18)
Browse files Browse the repository at this point in the history
* refactor: 更改控制器名称为 LoginController

* refactor: 增加 AccountLogout 路由

* doc: 增加文档注释

* chore: 更新脚本路径

* refactor: 更新登录逻辑

* refactor: 精简代码

* chore: 更新登录跳转脚本

* chore: 增加弹窗支持

* chore: 增加健康检查支持

* refactor: 增强 AdminTable 参数

* refactor: 禁用扩展按钮

* revert: 撤销更改

* refactor: 使用原生 Table 实现功能
  • Loading branch information
ArgoZhang authored Feb 25, 2025
1 parent 6ffab71 commit 9d7cfa0
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="BootstrapBlazor.FontAwesome" Version="9.0.2" />
<PackageReference Include="BootstrapBlazor.Middleware" Version="9.0.0" />
<PackageReference Include="Longbow.HealthChecks" Version="6.0.0" />
<PackageReference Include="Longbow.HealthChecks" Version="9.0.0" />
<PackageReference Include="Longbow.Tasks" Version="9.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div class="wrap white">
@inherits BootstrapModuleComponentBase

<div class="wrap white">
<div class="container">
<form method="post" class="form-signin" action="@PostUrl" @ref="LoginForm">
<h2 class="form-signin-heading">@Title</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public partial class AdminLogin : IDisposable
/// </summary>
protected string? PostUrl { get; set; }

private DotNetObjectReference<AdminLogin>? Interop { get; set; }

/// <summary>
///
/// </summary>
Expand All @@ -75,10 +73,6 @@ public partial class AdminLogin : IDisposable
[NotNull]
protected ILogin? LoginService { get; set; }

[Inject]
[NotNull]
private IJSRuntime? JSRuntime { get; set; }

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ namespace BootstrapAdmin.Web.Components.Components;
/// <summary>
/// AdminLoginGitee 组件
/// </summary>
[JSModuleAutoLoader("./Components/Components/AdminLoginGitee.razor.js", AutoInvokeInit = false, AutoInvokeDispose = false)]
public partial class AdminLoginGitee
{
[Inject, NotNull]
private IUser? UserService { get; set; }

[Inject, NotNull]
private NavigationManager? NavigationManager { get; set; }

private LoginModel Model { get; set; } = new();

private Task OnLogin(EditContext context)
private async Task OnLogin(EditContext context)
{
var auth = UserService.Authenticate(Model.UserName, Model.Password);
if (auth)
{
NavigationManager.NavigateTo("Account/LoginTo", true);
await InvokeVoidAsync("login", "Login/Login", Model.UserName, Model.RememberMe);
}
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { execute } from "../../_content/BootstrapBlazor/modules/ajax.js"

export async function login(url, userName, rememberMe) {
var password = document.querySelector('input[type="password"]').value;
await execute({
url: url,
method: 'POST',
toJson: false,
data: {
userName,
password,
rememberMe
}
});
location.href = "/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BootstrapAdmin.Web.Components.Components;
/// <summary>
/// FAIconList 组件
/// </summary>
[JSModuleAutoLoader("./Components/FAIconList.razor.js", JSObjectReference = true)]
[JSModuleAutoLoader("./Components/Components/FAIconList.razor.js", JSObjectReference = true)]
public partial class FAIconList
{
private string? ClassString => CssBuilder.Default("icon-list")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
@inherits LayoutComponentBase

<CascadingValue Value="ReloadMenu" IsFixed="true">
<Layout SideWidth="0" IsPage="true" IsFullSide="true" IsFixedHeader="true"
ShowFooter="true" ShowGotoTop="true" ShowCollapseBar="true" Menus="@MenuItems"
OnAuthorizing="@OnAuthorizing"
UseTabSet="true" TabDefaultUrl="/Admin/Index" OnUpdateAsync="OnUpdateAsync">
<Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span>
<Logout ImageUrl="@Icon" DisplayName="@Context.DisplayName" UserName="@UserName">
<LinkTemplate>
<a href="/Admin/Profiles"><i class="fa-solid fa-suitcase"></i>个人中心</a>
<a href="/Admin/Index"><i class="fa-solid fa-cog"></i>设置</a>
<a href="/Admin/Notifications"><i class="fa-solid fa-bell"></i>通知<span id="logoutNoti" class="badge badge-pill badge-success"></span></a>
<LogoutLink />
</LinkTemplate>
</Logout>
<div class="layout-drawer"><i class="fa-solid fa-gears"></i></div>
</Header>
<Side>
<div class="layout-banner">
<img class="layout-logo" src="/images/logo.png" />
<div class="layout-title">
<span>@Title</span>
<BootstrapBlazorRoot>
<CascadingValue Value="ReloadMenu" IsFixed="true">
<Layout SideWidth="0" IsPage="true" IsFullSide="true" IsFixedHeader="true"
ShowFooter="true" ShowGotoTop="true" ShowCollapseBar="true" Menus="@MenuItems"
OnAuthorizing="@OnAuthorizing"
UseTabSet="true" TabDefaultUrl="/Admin/Index" OnUpdateAsync="OnUpdateAsync">
<Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span>
<Logout ImageUrl="@Icon" DisplayName="@Context.DisplayName" UserName="@UserName">
<LinkTemplate>
<a href="/Admin/Profiles"><i class="fa-solid fa-suitcase"></i>个人中心</a>
<a href="/Admin/Index"><i class="fa-solid fa-cog"></i>设置</a>
<a href="/Admin/Notifications"><i class="fa-solid fa-bell"></i>通知<span id="logoutNoti" class="badge badge-pill badge-success"></span></a>
<LogoutLink />
</LinkTemplate>
</Logout>
<div class="layout-drawer"><i class="fa-solid fa-gears"></i></div>
</Header>
<Side>
<div class="layout-banner">
<img class="layout-logo" src="/images/logo.png" />
<div class="layout-title">
<span>@Title</span>
</div>
</div>
</div>
</Side>
<Main>
@Body
</Main>
<Footer>
<div class="flex-fill text-center text-dark">@Footer</div>
</Footer>
<NotAuthorized>
<Redirect />
</NotAuthorized>
</Layout>
</CascadingValue>
</Side>
<Main>
@Body
</Main>
<Footer>
<div class="flex-fill text-center text-dark">@Footer</div>
</Footer>
<NotAuthorized>
<Redirect />
</NotAuthorized>
</Layout>
</CascadingValue>
</BootstrapBlazorRoot>

@if (Lock)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace BootstrapAdmin.Web.Components.Pages.Account;

/// <summary>
///
/// Login 组件
/// </summary>
[Layout(typeof(LoginLayout))]
[Route("/Account/Login")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BootstrapAdmin.Web.Components.Layout;

namespace BootstrapAdmin.Web.Components.Pages.Account;

/// <summary>
/// Login 组件
/// </summary>
[Layout(typeof(LoginLayout))]
[Route("/Account/Logout")]
public class Logout : ComponentBase
{
[Inject, NotNull]
private NavigationManager? NavigationManager { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="firstRender"></param>
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);

NavigationManager.NavigateTo("Login/Logout", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
</div>
</HeaderTemplate>
<BodyTemplate>
<AdminTable TItem="HealthCheckReportItem" IsFixedHeader="false" @ref="HealthTable" IsMultipleSelect="false"
ShowToolbar="false" ShowDefaultButtons="false" ShowEmpty="false" ShowSearch="false" ShowLoading="true" OnQueryAsync="OnQueryAsync">
<Table TItem="HealthCheckReportItem" IsFixedHeader="false" @ref="HealthTable" IsMultipleSelect="false"
IsStriped="true" IsBordered="true" ExtendButtonColumnWidth="100"
ShowToolbar="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
ShowEmpty="false" ShowSearch="false" ShowLoading="true" OnQueryAsync="OnQueryAsync">
<TableColumns>
<TableColumn @bind-Field="@context.Name" Lookup="@GetNameLookup()" Text="检查项"></TableColumn>
<TableColumn @bind-Field="@context.Description" Text="描述"></TableColumn>
Expand All @@ -27,6 +29,6 @@
<RowButtonTemplate>
<TableCellButton Color="Color.Primary" Icon="fa-solid fa-info-circle" Text="明细" OnClick="@(() => OnRowButtonClick(context))" />
</RowButtonTemplate>
</AdminTable>
</Table>
</BodyTemplate>
</Card>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class Healths
private HealthStatus Status { get; set; }

[NotNull]
private AdminTable<HealthCheckReportItem>? HealthTable { get; set; }
private Table<HealthCheckReportItem>? HealthTable { get; set; }

[Inject]
[NotNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Website: https://admin.blazor.zone

using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Models;
using BootstrapAdmin.Web.Services;
using BootstrapAdmin.Web.Services.SMS;
using BootstrapAdmin.Web.Utils;
Expand All @@ -19,36 +20,37 @@ namespace BootstrapAdmin.Web.Controllers;
/// Account controller.
/// </summary>
[AllowAnonymous]
public class AccountController : Controller
public class LoginController : Controller
{
private const string MobileSchema = "Mobile";

#region UserLogin
/// <summary>
/// Login the specified userName, password and remember.
/// </summary>
/// <returns>The login.</returns>
/// <param name="userName">User name.</param>
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
/// <param name="model"></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,
[IgnoreAntiforgeryToken]
public async Task<RedirectResult> Login([FromBody] LoginModel model,
[FromQuery] string? returnUrl, [FromQuery] string? appId,
[FromServices] BootstrapAppContext context,
[FromServices] IUser userService,
[FromServices] IDict dictService)
{
var userName = model.UserName;
var password = model.Password;
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
return RedirectLogin();
}

var auth = userService.Authenticate(userName, password);
var persistent = remember == "true";
var persistent = model.RememberMe;
var period = 0;
if (persistent)
{
Expand All @@ -58,10 +60,12 @@ public async Task<IActionResult> Login(string userName, string password, [FromQu

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);
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)
private async Task<RedirectResult> SignInAsync(string userName, string returnUrl, bool persistent, int period = 0, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
{
var identity = new ClaimsIdentity(authenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
Expand Down Expand Up @@ -94,9 +98,7 @@ private RedirectResult RedirectLogin(string? returnUrl = null)
}
return Redirect(url);
}
#endregion

#region Logout
/// <summary>
/// Logout this instance.
/// </summary>
Expand All @@ -113,7 +115,6 @@ public async Task<IActionResult> Logout([FromQuery] string returnUrl, [FromQuery
["ReturnUrl"] = returnUrl
}));
}
#endregion

#region Mobile Login
/// <summary>
Expand Down

0 comments on commit 9d7cfa0

Please sign in to comment.