feat: 完成短信登录功能

This commit is contained in:
Argo-Lenovo 2022-01-01 14:32:42 +08:00
parent ee28d845f2
commit b72899b658
6 changed files with 29 additions and 13 deletions

View File

@ -134,7 +134,7 @@ class UserService : BaseDatabase, IUser
/// <param name="appId"></param>
/// <param name="roles"></param>
/// <returns></returns>
public bool TryCreateUserByPhone(string phone, string appId, ICollection<string> roles)
public bool TryCreateUserByPhone(string phone, string code, string appId, ICollection<string> roles)
{
var ret = false;
try
@ -142,6 +142,7 @@ class UserService : BaseDatabase, IUser
var user = GetAll().FirstOrDefault(user => user.UserName == phone);
if (user == null)
{
var salt = LgbCryptography.GenerateSalt();
Database.BeginTransaction();
user = new User()
{
@ -151,6 +152,8 @@ class UserService : BaseDatabase, IUser
UserName = phone,
Icon = "default.jpg",
Description = "手机用户",
PassSalt = salt,
Password = LgbCryptography.ComputeHash(code, salt),
App = appId
};
Database.Save(user);

View File

@ -79,5 +79,5 @@ public interface IUser
/// <param name="appId"></param>
/// <param name="roles"></param>
/// <returns></returns>
bool TryCreateUserByPhone(string phone, string appId, ICollection<string> roles);
bool TryCreateUserByPhone(string phone, string code, string appId, ICollection<string> roles);
}

View File

@ -98,23 +98,32 @@ namespace Bootstrap.Admin.Controllers
/// <param name="loginService"></param>
/// <param name="phone"></param>
/// <param name="code"></param>
/// <param name="remember">Remember.</param>
/// <returns></returns>
[HttpPost()]
public async Task<IActionResult> Mobile(string phone, string code,
public async Task<IActionResult> Mobile(string phone, string code, [FromQuery] string? remember,
[FromServices] ISMSProvider provider,
[FromServices] ILogin loginService,
[FromServices] IUser userService,
[FromServices] IDict dictService,
[FromServices] BootstrapAppContext context)
{
if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin();
var auth = provider.Validate(phone, code);
var persistent = remember == "true";
var period = 0;
if (persistent)
{
// Cookie 持久化
period = dictService.GetCookieExpiresPeriod();
}
await loginService.Log(phone, auth);
if (auth)
{
userService.TryCreateUserByPhone(phone, context.AppId, provider.Options.Roles);
userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles);
}
return auth ? await SignInAsync(phone, false, 0, MobileSchema) : RedirectLogin();
return auth ? await SignInAsync(phone, persistent, period, MobileSchema) : RedirectLogin();
}
#endregion

View File

@ -35,12 +35,14 @@ public partial class Login
void OnClickSwitchButton()
{
PostUrl = UseMobileLogin ? "/Account/Mobile" : "/Account/Login";
var rem = RememberPassword ? "true" : "false";
PostUrl = UseMobileLogin ? $"/Account/Mobile?remember={rem}" : $"/Account/Login?remember={rem}";
}
Task OnRememberPassword(bool remember)
{
PostUrl = "/Account/Login?remember=true";
var rem = remember ? "true" : "false";
PostUrl = UseMobileLogin ? $"/Account/Mobile?remember={rem}" : $"/Account/Login?remember={rem}";
return Task.CompletedTask;
}

View File

@ -6,7 +6,7 @@
UseTabSet="true" TabDefaultUrl="/Admin/Index">
<Header>
<span class="ms-3 flex-fill">Bootstrap of Blazor</span>
<Logout ImageUrl="/images/Argo.png" DisplayName="@DisplayName" UserName="Admin">
<Logout ImageUrl="/images/Argo.png" DisplayName="@DisplayName" UserName="@UserName">
<LinkTemplate>
<a href="/Admin/Profiles"><i class="fa fa-suitcase"></i>个人中心</a>
<a href="/Admin/Index"><i class="fa fa-cog"></i>设置</a>

View File

@ -47,6 +47,8 @@ namespace BootstrapAdmin.Web.Shared
private string? DisplayName { get; set; }
private string? UserName { get; set; }
/// <summary>
/// OnInitializedAsync 方法
/// </summary>
@ -54,15 +56,15 @@ namespace BootstrapAdmin.Web.Shared
protected override async Task OnInitializedAsync()
{
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var userName = state.User.Identity?.Name;
UserName = state.User.Identity?.Name;
if (!string.IsNullOrEmpty(userName))
if (!string.IsNullOrEmpty(UserName))
{
DisplayName = UsersService.GetDisplayName(userName);
Context.UserName = userName;
DisplayName = UsersService.GetDisplayName(UserName);
Context.UserName = UserName;
Context.DisplayName = DisplayName;
MenuItems = NavigationsService.GetAllMenus(userName).ToAdminMenus();
MenuItems = NavigationsService.GetAllMenus(UserName).ToAdminMenus();
}
Title = DictsService.GetWebTitle();