From bca5a512cf8dd1eb0debb55152dadc69ab28a69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 29 Mar 2023 22:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=A7=86=E5=9B=BE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8BViewKinds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/ReadOnlyEntityController.cs | 39 +++++----- NewLife.Cube/NewLife.Cube.csproj | 1 + .../Areas/Admin/Controllers/UserController.cs | 71 +++++++------------ .../Common/ReadOnlyEntityController.cs | 26 +++---- NewLife.CubeNC/NewLife.CubeNC.csproj | 2 + NewLife.CubeNC/ViewModels/FieldCollection.cs | 30 ++++---- NewLife.CubeNC/ViewModels/ViewKinds.cs | 27 +++++++ 7 files changed, 103 insertions(+), 93 deletions(-) create mode 100644 NewLife.CubeNC/ViewModels/ViewKinds.cs diff --git a/NewLife.Cube/Common/ReadOnlyEntityController.cs b/NewLife.Cube/Common/ReadOnlyEntityController.cs index 5d95a8b3..c7b1d870 100644 --- a/NewLife.Cube/Common/ReadOnlyEntityController.cs +++ b/NewLife.Cube/Common/ReadOnlyEntityController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using NewLife.Common; using NewLife.Cube.Common; using NewLife.Cube.Entity; +using NewLife.Cube.ViewModels; using NewLife.Data; using NewLife.Log; using NewLife.Web; @@ -410,7 +411,7 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : var list = SearchData(p); // Json输出 - if (IsJsonRequest) return Json(0, null, OnFilter(list.Cast(), "ListFields"), new { pager = p, stat = p.State }); + if (IsJsonRequest) return Json(0, null, OnFilter(list.Cast(), ViewKinds.List), new { pager = p, stat = p.State }); return Json(0, null, list, new { pager = p, stat = p.State }); } @@ -430,7 +431,7 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : Valid(entity, DataObjectMethodType.Select, false); // Json输出 - if (IsJsonRequest) return Json(0, null, OnFilter(entity, "Detail")); + if (IsJsonRequest) return Json(0, null, OnFilter(entity, ViewKinds.Detail)); return Json(0, null, entity); } @@ -1203,7 +1204,7 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : #region 列表字段和表单字段 private static FieldCollection _ListFields; /// 列表字段过滤 - protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, "List"); + protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, ViewKinds.List); //private static FieldCollection _FormFields; ///// 表单字段过滤 @@ -1212,31 +1213,30 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : private static FieldCollection _AddFormFields; /// 表单字段过滤 - protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, "AddForm"); + protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, ViewKinds.AddForm); private static FieldCollection _EditFormFields; /// 表单字段过滤 - protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, "EditForm"); + protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, ViewKinds.EditForm); private static FieldCollection _DetailFields; /// 表单字段过滤 - protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, "Detail"); + protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, ViewKinds.Detail); /// 获取字段信息。支持用户重载并根据上下文定制界面 /// 字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List - /// + /// /// - protected virtual FieldCollection OnGetFields(String kind, TEntity entity) + protected virtual FieldCollection OnGetFields(ViewKinds kind, IModel model) { var fields = kind switch { - "Detail" => DetailFields, - "EditForm" => EditFormFields, - "AddForm" => AddFormFields, - "List" => ListFields, - _ => ListFields + ViewKinds.List => ListFields, + ViewKinds.Detail => DetailFields, + ViewKinds.AddForm => AddFormFields, + ViewKinds.EditForm => EditFormFields, + _ => ListFields, }; - return fields.Clone(); } @@ -1244,11 +1244,10 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : /// 获取字段 /// /// 字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List - /// Name和ColumnName的值的格式。0-小驼峰,1-小写,2-保持默认。默认0 /// [AllowAnonymous] [HttpGet] - public virtual ActionResult GetFields(String kind, FormatType formatType = FormatType.CamelCase) + public virtual ActionResult GetFields(ViewKinds kind) { var fields = OnGetFields(kind, null); @@ -1263,12 +1262,12 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : /// /// /// - protected virtual IDictionary OnFilter(IModel model, String kind) + protected virtual IDictionary OnFilter(IModel model, ViewKinds kind) { if (model == null) return null; var dic = new Dictionary(); - var fields = OnGetFields(kind, null); + var fields = OnGetFields(kind, model); if (fields != null) { var names = Factory.FieldNames; @@ -1288,13 +1287,13 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : /// /// /// - protected virtual IEnumerable> OnFilter(IEnumerable models, String kind) + protected virtual IEnumerable> OnFilter(IEnumerable models, ViewKinds kind) { if (models == null) yield break; foreach (var item in models) { - yield return OnFilter(item, null); + yield return OnFilter(item, kind); } } #endregion diff --git a/NewLife.Cube/NewLife.Cube.csproj b/NewLife.Cube/NewLife.Cube.csproj index ea57ffbb..a4ced980 100644 --- a/NewLife.Cube/NewLife.Cube.csproj +++ b/NewLife.Cube/NewLife.Cube.csproj @@ -74,6 +74,7 @@ + diff --git a/NewLife.CubeNC/Areas/Admin/Controllers/UserController.cs b/NewLife.CubeNC/Areas/Admin/Controllers/UserController.cs index 00eb1f2c..b2441177 100644 --- a/NewLife.CubeNC/Areas/Admin/Controllers/UserController.cs +++ b/NewLife.CubeNC/Areas/Admin/Controllers/UserController.cs @@ -7,6 +7,8 @@ using NewLife.Common; using NewLife.Cube.Areas.Admin.Models; using NewLife.Cube.Entity; using NewLife.Cube.Services; +using NewLife.Cube.ViewModels; +using NewLife.Data; using NewLife.Log; using NewLife.Reflection; using NewLife.Web; @@ -88,53 +90,34 @@ public class UserController : EntityController } } - protected override FieldCollection OnGetFields(String kind, User entity) + protected override FieldCollection OnGetFields(ViewKinds kind, IModel model) { - switch (kind.ToLower()) - { - case "addform": - case "editform": - { - var CurrUser = ManageProvider.User;//理论上肯定大于0 - var RoleData = Role.FindAllWithCache().Where(w => w.IsSystem == false).OrderByDescending(e => e.Sort).ToDictionary(e => e.ID, e => e.Name); - if (CurrUser != null) - { - if (CurrUser.Role.IsSystem) - { - RoleData = Role.FindAllWithCache().OrderByDescending(e => e.Sort).ToDictionary(e => e.ID, e => e.Name); - } - } - if (kind.ToLower() == "addform") - { - var AddRoleIDField = AddFormFields.GetField("RoleID"); - if (AddRoleIDField != null) - { - AddRoleIDField.DataSource = entity => RoleData; - } - var AddRoleIDsField = AddFormFields.GetField("RoleIds"); - if (AddRoleIDsField != null) - { - AddRoleIDsField.DataSource = entity => RoleData; - } - } - if (kind.ToLower() == "editform") - { - var EditRoleIDField = EditFormFields.GetField("RoleID"); - if (EditRoleIDField != null) - { - EditRoleIDField.DataSource = entity => RoleData; - } - var EditRoleIDsField = EditFormFields.GetField("RoleIds"); - if (EditRoleIDsField != null) - { - EditRoleIDsField.DataSource = entity => RoleData; - } - } + var fields = base.OnGetFields(kind, model); + if (fields == null) return fields; - break; - } + var user = ManageProvider.User;//理论上肯定大于0 + var roles = Role.FindAllWithCache().Where(w => w.IsSystem == false).OrderByDescending(e => e.Sort).ToDictionary(e => e.ID, e => e.Name); + if (user != null) + { + if (user.Role.IsSystem) + { + roles = Role.FindAllWithCache().OrderByDescending(e => e.Sort).ToDictionary(e => e.ID, e => e.Name); + } } - return base.OnGetFields(kind, entity); + + switch (kind) + { + case ViewKinds.AddForm: + case ViewKinds.EditForm: + var df = fields.GetField("RoleID"); + if (df != null) df.DataSource = entity => roles; + + var df2 = fields.GetField("RoleIds"); + if (df2 != null) df2.DataSource = entity => roles; + break; + } + + return fields; } /// diff --git a/NewLife.CubeNC/Common/ReadOnlyEntityController.cs b/NewLife.CubeNC/Common/ReadOnlyEntityController.cs index da837646..8807bb6c 100644 --- a/NewLife.CubeNC/Common/ReadOnlyEntityController.cs +++ b/NewLife.CubeNC/Common/ReadOnlyEntityController.cs @@ -13,6 +13,7 @@ using NewLife.Cube.Common; using NewLife.Cube.Entity; using NewLife.Cube.Extensions; using NewLife.Cube.ViewModels; +using NewLife.Data; using NewLife.IO; using NewLife.Log; using NewLife.Reflection; @@ -1240,7 +1241,7 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : #region 列表字段和表单字段 private static FieldCollection _ListFields; /// 列表字段过滤 - protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, "List"); + protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, ViewKinds.List); //private static FieldCollection _FormFields; ///// 表单字段过滤 @@ -1249,31 +1250,30 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : private static FieldCollection _AddFormFields; /// 表单字段过滤 - protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, "AddForm"); + protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, ViewKinds.AddForm); private static FieldCollection _EditFormFields; /// 表单字段过滤 - protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, "EditForm"); + protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, ViewKinds.EditForm); private static FieldCollection _DetailFields; /// 表单字段过滤 - protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, "Detail"); + protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, ViewKinds.Detail); /// 获取字段信息。支持用户重载并根据上下文定制界面 /// 字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List - /// + /// /// - protected virtual FieldCollection OnGetFields(String kind, TEntity entity) + protected virtual FieldCollection OnGetFields(ViewKinds kind, IModel model) { var fields = kind switch { - "Detail" => DetailFields, - "EditForm" => EditFormFields, - "AddForm" => AddFormFields, - "List" => ListFields, - _ => ListFields + ViewKinds.List => ListFields, + ViewKinds.Detail => DetailFields, + ViewKinds.AddForm => AddFormFields, + ViewKinds.EditForm => EditFormFields, + _ => ListFields, }; - return fields.Clone(); } @@ -1284,7 +1284,7 @@ public class ReadOnlyEntityController : ControllerBaseX where TEntity : /// Name和ColumnName的值的格式。0-小驼峰,1-小写,2-保持默认。默认0 /// [AllowAnonymous] - public virtual ActionResult GetFields(String kind, FormatType formatType = FormatType.CamelCase) + public virtual ActionResult GetFields(ViewKinds kind) { var fields = OnGetFields(kind, null); diff --git a/NewLife.CubeNC/NewLife.CubeNC.csproj b/NewLife.CubeNC/NewLife.CubeNC.csproj index 54985620..112c6d41 100644 --- a/NewLife.CubeNC/NewLife.CubeNC.csproj +++ b/NewLife.CubeNC/NewLife.CubeNC.csproj @@ -130,6 +130,8 @@ + + diff --git a/NewLife.CubeNC/ViewModels/FieldCollection.cs b/NewLife.CubeNC/ViewModels/FieldCollection.cs index f43502d7..f973d029 100644 --- a/NewLife.CubeNC/ViewModels/FieldCollection.cs +++ b/NewLife.CubeNC/ViewModels/FieldCollection.cs @@ -16,7 +16,7 @@ public class FieldCollection : List { #region 属性 /// 类型 - public String Kind { get; set; } + public ViewKinds Kind { get; set; } /// 工厂 public IEntityFactory Factory { get; set; } @@ -31,12 +31,12 @@ public class FieldCollection : List #region 构造 /// 实例化一个字段集合 /// - public FieldCollection(String kind) => Kind = kind; + public FieldCollection(ViewKinds kind) => Kind = kind; /// 使用工厂实例化一个字段集合 /// /// - public FieldCollection(IEntityFactory factory, String kind) + public FieldCollection(IEntityFactory factory, ViewKinds kind) { Kind = kind; Factory = factory; @@ -51,21 +51,22 @@ public class FieldCollection : List switch (kind) { - case "AddForm": + case ViewKinds.List: + SetRelation(false); + break; + case ViewKinds.Detail: + SetRelation(true); + break; + case ViewKinds.AddForm: SetRelation(true); //RemoveCreateField(); RemoveUpdateField(); break; - case "EditForm": + case ViewKinds.EditForm: SetRelation(true); break; - case "Detail": - SetRelation(true); + case ViewKinds.Search: break; - case "Form": - SetRelation(true); - break; - case "List": default: SetRelation(false); break; @@ -82,11 +83,8 @@ public class FieldCollection : List { DataField df = Kind switch { - "AddForm" => new FormField(), - "EditForm" => new FormField(), - "Detail" => new FormField(), - "Form" => new FormField(), - "List" => new ListField(), + ViewKinds.List => new ListField(), + ViewKinds.Detail or ViewKinds.AddForm or ViewKinds.EditForm => new FormField(), _ => throw new NotImplementedException(), }; //df.Sort = Count + 1; diff --git a/NewLife.CubeNC/ViewModels/ViewKinds.cs b/NewLife.CubeNC/ViewModels/ViewKinds.cs new file mode 100644 index 00000000..6e2c879a --- /dev/null +++ b/NewLife.CubeNC/ViewModels/ViewKinds.cs @@ -0,0 +1,27 @@ +using System.ComponentModel; + +namespace NewLife.Cube.ViewModels; + +/// 视图类型。如List/Detail/AddForm/EditForm/Search等 +public enum ViewKinds +{ + /// 列表 + [Description("列表")] + List = 1, + + /// 详情 + [Description("详情")] + Detail = 2, + + /// 添加表单 + [Description("添加表单")] + AddForm = 3, + + /// 编辑表单 + [Description("编辑表单")] + EditForm = 4, + + /// 搜索 + [Description("搜索")] + Search = 5, +} \ No newline at end of file