统一视图类型ViewKinds
This commit is contained in:
parent
7c4d544d62
commit
bca5a512cf
|
@ -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<TEntity> : ControllerBaseX where TEntity :
|
|||
var list = SearchData(p);
|
||||
|
||||
// Json输出
|
||||
if (IsJsonRequest) return Json(0, null, OnFilter(list.Cast<IModel>(), "ListFields"), new { pager = p, stat = p.State });
|
||||
if (IsJsonRequest) return Json(0, null, OnFilter(list.Cast<IModel>(), 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<TEntity> : 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<TEntity> : ControllerBaseX where TEntity :
|
|||
#region 列表字段和表单字段
|
||||
private static FieldCollection _ListFields;
|
||||
/// <summary>列表字段过滤</summary>
|
||||
protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, "List");
|
||||
protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, ViewKinds.List);
|
||||
|
||||
//private static FieldCollection _FormFields;
|
||||
///// <summary>表单字段过滤</summary>
|
||||
|
@ -1212,31 +1213,30 @@ public class ReadOnlyEntityController<TEntity> : ControllerBaseX where TEntity :
|
|||
|
||||
private static FieldCollection _AddFormFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, "AddForm");
|
||||
protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, ViewKinds.AddForm);
|
||||
|
||||
private static FieldCollection _EditFormFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, "EditForm");
|
||||
protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, ViewKinds.EditForm);
|
||||
|
||||
private static FieldCollection _DetailFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, "Detail");
|
||||
protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, ViewKinds.Detail);
|
||||
|
||||
/// <summary>获取字段信息。支持用户重载并根据上下文定制界面</summary>
|
||||
/// <param name="kind">字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List</param>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
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<TEntity> : ControllerBaseX where TEntity :
|
|||
/// 获取字段
|
||||
/// </summary>
|
||||
/// <param name="kind">字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List</param>
|
||||
/// <param name="formatType">Name和ColumnName的值的格式。0-小驼峰,1-小写,2-保持默认。默认0</param>
|
||||
/// <returns></returns>
|
||||
[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<TEntity> : ControllerBaseX where TEntity :
|
|||
/// <param name="model"></param>
|
||||
/// <param name="kind"></param>
|
||||
/// <returns></returns>
|
||||
protected virtual IDictionary<String, Object> OnFilter(IModel model, String kind)
|
||||
protected virtual IDictionary<String, Object> OnFilter(IModel model, ViewKinds kind)
|
||||
{
|
||||
if (model == null) return null;
|
||||
|
||||
var dic = new Dictionary<String, Object>();
|
||||
var fields = OnGetFields(kind, null);
|
||||
var fields = OnGetFields(kind, model);
|
||||
if (fields != null)
|
||||
{
|
||||
var names = Factory.FieldNames;
|
||||
|
@ -1288,13 +1287,13 @@ public class ReadOnlyEntityController<TEntity> : ControllerBaseX where TEntity :
|
|||
/// <param name="models"></param>
|
||||
/// <param name="kind"></param>
|
||||
/// <returns></returns>
|
||||
protected virtual IEnumerable<IDictionary<String, Object>> OnFilter(IEnumerable<IModel> models, String kind)
|
||||
protected virtual IEnumerable<IDictionary<String, Object>> OnFilter(IEnumerable<IModel> models, ViewKinds kind)
|
||||
{
|
||||
if (models == null) yield break;
|
||||
|
||||
foreach (var item in models)
|
||||
{
|
||||
yield return OnFilter(item, null);
|
||||
yield return OnFilter(item, kind);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
|
|
@ -74,6 +74,7 @@
|
|||
<Compile Include="..\NewLife.CubeNC\ViewModels\LoginConfigModel.cs" Link="ViewModels\LoginConfigModel.cs" />
|
||||
<Compile Include="..\NewLife.CubeNC\ViewModels\MenuTree.cs" Link="ViewModels\MenuTree.cs" />
|
||||
<Compile Include="..\NewLife.CubeNC\ViewModels\SelectUserModel.cs" Link="ViewModels\SelectUserModel.cs" />
|
||||
<Compile Include="..\NewLife.CubeNC\ViewModels\ViewKinds.cs" Link="ViewModels\ViewKinds.cs" />
|
||||
<Compile Include="..\NewLife.CubeNC\WebMiddleware\TenantMiddleware.cs" Link="WebMiddleware\TenantMiddleware.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -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<User, UserModel>
|
|||
}
|
||||
}
|
||||
|
||||
protected override FieldCollection OnGetFields(String kind, User entity)
|
||||
protected override FieldCollection OnGetFields(ViewKinds kind, IModel model)
|
||||
{
|
||||
switch (kind.ToLower())
|
||||
var fields = base.OnGetFields(kind, model);
|
||||
if (fields == null) return fields;
|
||||
|
||||
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)
|
||||
{
|
||||
case "addform":
|
||||
case "editform":
|
||||
if (user.Role.IsSystem)
|
||||
{
|
||||
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;
|
||||
roles = Role.FindAllWithCache().OrderByDescending(e => e.Sort).ToDictionary(e => e.ID, e => e.Name);
|
||||
}
|
||||
}
|
||||
|
||||
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 base.OnGetFields(kind, entity);
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -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<TEntity> : ControllerBaseX where TEntity :
|
|||
#region 列表字段和表单字段
|
||||
private static FieldCollection _ListFields;
|
||||
/// <summary>列表字段过滤</summary>
|
||||
protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, "List");
|
||||
protected static FieldCollection ListFields => _ListFields ??= new FieldCollection(Factory, ViewKinds.List);
|
||||
|
||||
//private static FieldCollection _FormFields;
|
||||
///// <summary>表单字段过滤</summary>
|
||||
|
@ -1249,31 +1250,30 @@ public class ReadOnlyEntityController<TEntity> : ControllerBaseX where TEntity :
|
|||
|
||||
private static FieldCollection _AddFormFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, "AddForm");
|
||||
protected static FieldCollection AddFormFields => _AddFormFields ??= new FieldCollection(Factory, ViewKinds.AddForm);
|
||||
|
||||
private static FieldCollection _EditFormFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, "EditForm");
|
||||
protected static FieldCollection EditFormFields => _EditFormFields ??= new FieldCollection(Factory, ViewKinds.EditForm);
|
||||
|
||||
private static FieldCollection _DetailFields;
|
||||
/// <summary>表单字段过滤</summary>
|
||||
protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, "Detail");
|
||||
protected static FieldCollection DetailFields => _DetailFields ??= new FieldCollection(Factory, ViewKinds.Detail);
|
||||
|
||||
/// <summary>获取字段信息。支持用户重载并根据上下文定制界面</summary>
|
||||
/// <param name="kind">字段类型:详情-Detail、编辑-EditForm、添加-AddForm、列表-List</param>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
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<TEntity> : ControllerBaseX where TEntity :
|
|||
/// <param name="formatType">Name和ColumnName的值的格式。0-小驼峰,1-小写,2-保持默认。默认0</param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
public virtual ActionResult GetFields(String kind, FormatType formatType = FormatType.CamelCase)
|
||||
public virtual ActionResult GetFields(ViewKinds kind)
|
||||
{
|
||||
var fields = OnGetFields(kind, null);
|
||||
|
||||
|
|
|
@ -130,6 +130,8 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Areas\Cube\Controllers\ModelColumnController.cs" />
|
||||
<Compile Remove="Areas\Cube\Controllers\ModelTableController.cs" />
|
||||
<Compile Remove="Extensions\ItemsExtend.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ public class FieldCollection : List<DataField>
|
|||
{
|
||||
#region 属性
|
||||
/// <summary>类型</summary>
|
||||
public String Kind { get; set; }
|
||||
public ViewKinds Kind { get; set; }
|
||||
|
||||
/// <summary>工厂</summary>
|
||||
public IEntityFactory Factory { get; set; }
|
||||
|
@ -31,12 +31,12 @@ public class FieldCollection : List<DataField>
|
|||
#region 构造
|
||||
/// <summary>实例化一个字段集合</summary>
|
||||
/// <param name="kind"></param>
|
||||
public FieldCollection(String kind) => Kind = kind;
|
||||
public FieldCollection(ViewKinds kind) => Kind = kind;
|
||||
|
||||
/// <summary>使用工厂实例化一个字段集合</summary>
|
||||
/// <param name="factory"></param>
|
||||
/// <param name="kind"></param>
|
||||
public FieldCollection(IEntityFactory factory, String kind)
|
||||
public FieldCollection(IEntityFactory factory, ViewKinds kind)
|
||||
{
|
||||
Kind = kind;
|
||||
Factory = factory;
|
||||
|
@ -51,21 +51,22 @@ public class FieldCollection : List<DataField>
|
|||
|
||||
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>
|
|||
{
|
||||
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;
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
using System.ComponentModel;
|
||||
|
||||
namespace NewLife.Cube.ViewModels;
|
||||
|
||||
/// <summary>视图类型。如List/Detail/AddForm/EditForm/Search等</summary>
|
||||
public enum ViewKinds
|
||||
{
|
||||
/// <summary>列表</summary>
|
||||
[Description("列表")]
|
||||
List = 1,
|
||||
|
||||
/// <summary>详情</summary>
|
||||
[Description("详情")]
|
||||
Detail = 2,
|
||||
|
||||
/// <summary>添加表单</summary>
|
||||
[Description("添加表单")]
|
||||
AddForm = 3,
|
||||
|
||||
/// <summary>编辑表单</summary>
|
||||
[Description("编辑表单")]
|
||||
EditForm = 4,
|
||||
|
||||
/// <summary>搜索</summary>
|
||||
[Description("搜索")]
|
||||
Search = 5,
|
||||
}
|
Loading…
Reference in New Issue