refactor: 增加 PageBase 基类

This commit is contained in:
Argo Zhang 2020-01-09 12:35:38 +08:00
parent 9169775718
commit 1e3fabc46f
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
5 changed files with 137 additions and 75 deletions

View File

@ -1,4 +1,6 @@
<AuthorizateComponent Key="saveDisplayName">
@inherits ProfilesBase
<AuthorizateComponent Key="saveDisplayName">
<div class="card">
<div class="card-header">基本资料</div>
<div class="card-body">
@ -13,7 +15,7 @@
<label class="control-label" for="userName">登录名称</label>
<input type="text" class="form-control ignore" value="@Model?.UserName" readonly />
</div>
<LgbInputText ColumnClass="col-sm-6 col-md-auto" @bind-Value="@User.DisplayName" placeholder="不可为空16字以内" maxlength="16">
<LgbInputText ColumnClass="col-sm-6 col-md-auto" @bind-Value="@DisplayName" placeholder="不可为空16字以内" maxlength="16">
<RequiredValidator />
<StringLengthValidator Length="16" />
</LgbInputText>
@ -114,57 +116,3 @@
<img class="card-img d-none" src="@Model?.Icon.ToBlazorLink()" />
</div>
</div>
@code {
/// <summary>
///
/// </summary>
[CascadingParameter(Name = "Default")]
public DefaultLayout? RootLayout { get; protected set; }
protected ProfilesModel? Model { get; set; }
protected bool IsDemo { get; set; } = false;
protected BootstrapUser User { get; set; } = new BootstrapUser();
protected PasswordModel Password { get; set; } = new PasswordModel();
protected override void OnInitialized()
{
Model = new ProfilesModel(RootLayout?.UserName);
var user = DataAccess.UserHelper.RetrieveUserByUserName(Model?.UserName);
if (user != null) User = user;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override bool ShouldRender() => false;
private void SaveDisplayName(EditContext context)
{
if (!string.IsNullOrEmpty(User.UserName) && Bootstrap.DataAccess.UserHelper.SaveDisplayName(User.UserName, User.DisplayName))
{
RootLayout?.OnDisplayNameChanged(User.DisplayName);
}
}
private void SavePassword(EditContext context)
{
Bootstrap.DataAccess.UserHelper.ChangePassword(User.UserName, Password.Password, Password.NewPassword);
}
protected class PasswordModel
{
[DisplayName("原密码")]
public string Password { get; set; } = "";
[DisplayName("新密码")]
public string NewPassword { get; set; } = "";
[DisplayName("确认密码")]
public string ConfirmPassword { get; set; } = "";
}
}

View File

@ -1,35 +1,35 @@
using Bootstrap.Security;
using Microsoft.AspNetCore.Components;
using Bootstrap.Admin.Components;
using Bootstrap.Security;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Components
namespace Bootstrap.Pages.Admin.Components
{
/// <summary>
/// 字典表维护组件
/// </summary>
public class DictsBase : ComponentBase
public class DictsBase : PageBase
{
/// <summary>
///
/// 获得/设置 BootstrapDict 实例
/// </summary>
protected BootstrapDict QueryModel { get; set; } = new BootstrapDict() { Define = -1 };
/// <summary>
///
/// 获得/设置 字典类别集合
/// </summary>
protected List<SelectedItem> DefineItems { get; set; } = new List<SelectedItem>(new SelectedItem[] { new SelectedItem() { Text = "系统使用", Value = "0" }, new SelectedItem() { Text = "自定义", Value = "1" } });
/// <summary>
///
/// 获得/设置 查询条件集合
/// </summary>
protected List<SelectedItem> QueryDefine { get; set; } = new List<SelectedItem>(new SelectedItem[] { new SelectedItem() { Text = "全部", Value = "-1", Active = true }, new SelectedItem() { Text = "系统使用", Value = "0" }, new SelectedItem() { Text = "自定义", Value = "1" } });
/// <summary>
///
/// 查询方法
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageItems"></param>
/// <param name="pageIndex">页码</param>
/// <param name="pageItems">每页显示数据条目数量</param>
protected QueryData<BootstrapDict> Query(int pageIndex, int pageItems)
{
var data = DataAccess.DictHelper.RetrieveDicts();
@ -42,7 +42,7 @@ namespace Bootstrap.Admin.Components
}
/// <summary>
///
/// 新建方法
/// </summary>
/// <returns></returns>
protected BootstrapDict Add()
@ -51,19 +51,13 @@ namespace Bootstrap.Admin.Components
}
/// <summary>
///
/// 保存方法
/// </summary>
protected bool Save(BootstrapDict dict) => DataAccess.DictHelper.Save(dict);
/// <summary>
///
/// 删除方法
/// </summary>
protected bool Delete(IEnumerable<BootstrapDict> items) => DataAccess.DictHelper.Delete(items.Select(item => item.Id ?? ""));
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override bool ShouldRender() => false;
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Components;
namespace Bootstrap.Pages.Admin.Components
{
/// <summary>
/// 页面组件基类
/// </summary>
public abstract class PageBase : ComponentBase
{
/// <summary>
/// 是否重新绘制组件方法
/// </summary>
/// <returns></returns>
protected override bool ShouldRender() => false;
}
}

View File

@ -0,0 +1,103 @@
using System.ComponentModel;
using Bootstrap.Admin.Models;
using Bootstrap.Admin.Shared;
using Bootstrap.Security;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
namespace Bootstrap.Pages.Admin.Components
{
/// <summary>
/// 个人中心组件类
/// </summary>
public class ProfilesBase : PageBase
{
/// <summary>
/// 获得/设置 RootLayout 实例
/// </summary>
[CascadingParameter(Name = "Default")]
public DefaultLayout? RootLayout { get; protected set; }
/// <summary>
/// 获得/设置 ProfilesModel 实例
/// </summary>
protected ProfilesModel? Model { get; set; }
/// <summary>
/// 获得/设置 是否为演示系统
/// </summary>
protected bool IsDemo { get; set; } = false;
/// <summary>
/// 获得/设置 BootstrapUser 实例
/// </summary>
protected BootstrapUser User { get; set; } = new BootstrapUser();
/// <summary>
/// 获得/设置 PasswModel 实例
/// </summary>
protected PasswordModel Password { get; set; } = new PasswordModel();
/// <summary>
/// 获得/设置 当前用户显示名称
/// </summary>
protected string DisplayName { get; set; } = "";
/// <summary>
/// 组件初始化方法
/// </summary>
protected override void OnInitialized()
{
Model = new ProfilesModel(RootLayout?.UserName);
var user = DataAccess.UserHelper.RetrieveUserByUserName(Model?.UserName);
if (user != null) User = user;
// 直接绑定 User.DisplayName 导致未保存时 UI 的显示名称也会变化
DisplayName = User.DisplayName;
}
/// <summary>
/// 保存显示名称方法
/// </summary>
protected void SaveDisplayName(EditContext context)
{
if (!string.IsNullOrEmpty(User.UserName) && Bootstrap.DataAccess.UserHelper.SaveDisplayName(User.UserName, DisplayName))
{
User.DisplayName = DisplayName;
RootLayout?.OnDisplayNameChanged(DisplayName);
}
}
/// <summary>
/// 保存密码方法
/// </summary>
protected void SavePassword(EditContext context)
{
Bootstrap.DataAccess.UserHelper.ChangePassword(User.UserName, Password.Password, Password.NewPassword);
}
/// <summary>
/// 密码保存实体类
/// </summary>
protected class PasswordModel
{
/// <summary>
/// 获得/设置 原密码
/// </summary>
[DisplayName("原密码")]
public string Password { get; set; } = "";
/// <summary>
/// 获得/设置 新密码
/// </summary>
[DisplayName("新密码")]
public string NewPassword { get; set; } = "";
/// <summary>
/// 获得/设置 确认密码
/// </summary>
[DisplayName("确认密码")]
public string ConfirmPassword { get; set; } = "";
}
}
}

View File

@ -4,6 +4,7 @@
@using Bootstrap.Admin.Models
@using Bootstrap.Admin.Pages
@using Bootstrap.Admin.Shared
@using Bootstrap.Pages.Admin.Components
@using Bootstrap.Security
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization