From 1e3fabc46f66c456cf42eb96c09fb866ceed9ec8 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 9 Jan 2020 12:35:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20PageBase=20?= =?UTF-8?q?=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pages/Admin/Profiles.razor | 60 +--------- .../{ => Pages}/Components/DictsBase.cs | 32 +++--- .../Pages/Components/PageBase.cs | 16 +++ .../Pages/Components/ProfilesBase.cs | 103 ++++++++++++++++++ src/admin/Bootstrap.Admin/_Imports.razor | 1 + 5 files changed, 137 insertions(+), 75 deletions(-) rename src/admin/Bootstrap.Admin/{ => Pages}/Components/DictsBase.cs (80%) create mode 100644 src/admin/Bootstrap.Admin/Pages/Components/PageBase.cs create mode 100644 src/admin/Bootstrap.Admin/Pages/Components/ProfilesBase.cs diff --git a/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor b/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor index 3b2354e3..83dbd1f7 100644 --- a/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor +++ b/src/admin/Bootstrap.Admin/Pages/Admin/Profiles.razor @@ -1,4 +1,6 @@ - +@inherits ProfilesBase + +
基本资料
@@ -13,7 +15,7 @@
- + @@ -114,57 +116,3 @@
- -@code { - /// - /// - /// - [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; - } - - /// - /// - /// - /// - 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; } = ""; - } -} diff --git a/src/admin/Bootstrap.Admin/Components/DictsBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/DictsBase.cs similarity index 80% rename from src/admin/Bootstrap.Admin/Components/DictsBase.cs rename to src/admin/Bootstrap.Admin/Pages/Components/DictsBase.cs index 0b98fa7b..bff062cd 100644 --- a/src/admin/Bootstrap.Admin/Components/DictsBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/DictsBase.cs @@ -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 { /// /// 字典表维护组件 /// - public class DictsBase : ComponentBase + public class DictsBase : PageBase { /// - /// + /// 获得/设置 BootstrapDict 实例 /// protected BootstrapDict QueryModel { get; set; } = new BootstrapDict() { Define = -1 }; /// - /// + /// 获得/设置 字典类别集合 /// protected List DefineItems { get; set; } = new List(new SelectedItem[] { new SelectedItem() { Text = "系统使用", Value = "0" }, new SelectedItem() { Text = "自定义", Value = "1" } }); /// - /// + /// 获得/设置 查询条件集合 /// protected List QueryDefine { get; set; } = new List(new SelectedItem[] { new SelectedItem() { Text = "全部", Value = "-1", Active = true }, new SelectedItem() { Text = "系统使用", Value = "0" }, new SelectedItem() { Text = "自定义", Value = "1" } }); /// - /// + /// 查询方法 /// - /// - /// + /// 页码 + /// 每页显示数据条目数量 protected QueryData Query(int pageIndex, int pageItems) { var data = DataAccess.DictHelper.RetrieveDicts(); @@ -42,7 +42,7 @@ namespace Bootstrap.Admin.Components } /// - /// + /// 新建方法 /// /// protected BootstrapDict Add() @@ -51,19 +51,13 @@ namespace Bootstrap.Admin.Components } /// - /// + /// 保存方法 /// protected bool Save(BootstrapDict dict) => DataAccess.DictHelper.Save(dict); /// - /// + /// 删除方法 /// protected bool Delete(IEnumerable items) => DataAccess.DictHelper.Delete(items.Select(item => item.Id ?? "")); - - /// - /// - /// - /// - protected override bool ShouldRender() => false; } } diff --git a/src/admin/Bootstrap.Admin/Pages/Components/PageBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/PageBase.cs new file mode 100644 index 00000000..55f0c788 --- /dev/null +++ b/src/admin/Bootstrap.Admin/Pages/Components/PageBase.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Components; + +namespace Bootstrap.Pages.Admin.Components +{ + /// + /// 页面组件基类 + /// + public abstract class PageBase : ComponentBase + { + /// + /// 是否重新绘制组件方法 + /// + /// + protected override bool ShouldRender() => false; + } +} diff --git a/src/admin/Bootstrap.Admin/Pages/Components/ProfilesBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/ProfilesBase.cs new file mode 100644 index 00000000..d1929aff --- /dev/null +++ b/src/admin/Bootstrap.Admin/Pages/Components/ProfilesBase.cs @@ -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 +{ + /// + /// 个人中心组件类 + /// + public class ProfilesBase : PageBase + { + /// + /// 获得/设置 RootLayout 实例 + /// + [CascadingParameter(Name = "Default")] + public DefaultLayout? RootLayout { get; protected set; } + + /// + /// 获得/设置 ProfilesModel 实例 + /// + protected ProfilesModel? Model { get; set; } + + /// + /// 获得/设置 是否为演示系统 + /// + protected bool IsDemo { get; set; } = false; + + /// + /// 获得/设置 BootstrapUser 实例 + /// + protected BootstrapUser User { get; set; } = new BootstrapUser(); + + /// + /// 获得/设置 PasswModel 实例 + /// + protected PasswordModel Password { get; set; } = new PasswordModel(); + + /// + /// 获得/设置 当前用户显示名称 + /// + protected string DisplayName { get; set; } = ""; + + /// + /// 组件初始化方法 + /// + 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; + } + + /// + /// 保存显示名称方法 + /// + protected void SaveDisplayName(EditContext context) + { + if (!string.IsNullOrEmpty(User.UserName) && Bootstrap.DataAccess.UserHelper.SaveDisplayName(User.UserName, DisplayName)) + { + User.DisplayName = DisplayName; + RootLayout?.OnDisplayNameChanged(DisplayName); + } + } + + /// + /// 保存密码方法 + /// + protected 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; } = ""; + } + } +} diff --git a/src/admin/Bootstrap.Admin/_Imports.razor b/src/admin/Bootstrap.Admin/_Imports.razor index 5ef33262..d083dab6 100644 --- a/src/admin/Bootstrap.Admin/_Imports.razor +++ b/src/admin/Bootstrap.Admin/_Imports.razor @@ -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