From b5034bcafbb1338f580401e480ea1984c906c713 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 4 Feb 2023 11:30:06 +0800 Subject: [PATCH] fix(cli): fix missing field parameters --- .../Cli/UI/Vben/VbenModelScriptGenerator.cs | 425 ++++++++++++++---- .../Cli/UI/Vben/VbenViewScriptGenerator.cs | 2 + .../Properties/launchSettings.json | 2 +- 3 files changed, 349 insertions(+), 80 deletions(-) diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenModelScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenModelScriptGenerator.cs index a058a4881..c1b982262 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenModelScriptGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenModelScriptGenerator.cs @@ -1,15 +1,43 @@ -using System; +using LINGYUN.Abp.Cli.ServiceProxying.TypeScript; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Volo.Abp.Auditing; +using Volo.Abp.Data; using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities; using Volo.Abp.Http.Modeling; +using Volo.Abp.MultiTenancy; using Volo.Abp.TextTemplating; namespace LINGYUN.Abp.Cli.UI.Vben; public class VbenModelScriptGenerator : IVbenModelScriptGenerator, ISingletonDependency { + internal readonly static string[] DisableInputModelFields = new [] + { + nameof(IEntity.Id), + nameof(IMultiTenant.TenantId), + nameof(IHasConcurrencyStamp.ConcurrencyStamp), + nameof(IFullAuditedObject.CreationTime), + nameof(IFullAuditedObject.CreatorId), + nameof(IFullAuditedObject.LastModificationTime), + nameof(IFullAuditedObject.LastModifierId), + }; + internal readonly static string[] RemoveInputModelFields = new[] + { + nameof(IFullAuditedObject.DeletionTime), + nameof(IFullAuditedObject.IsDeleted), + nameof(IFullAuditedObject.DeleterId), + nameof(IHasExtraProperties.ExtraProperties), + "SkipCount", + "MaxResultCount", + "Sorting", + "Items", + "TotalCount", + }; + private readonly ITemplateRenderer _templateRenderer; public VbenModelScriptGenerator( @@ -32,7 +60,8 @@ public class VbenModelScriptGenerator : IVbenModelScriptGenerator, ISingletonDep .FirstOrDefault(); var listResultAction = controllerModel.Actions - .Where(action => action.Value.ReturnValue.TypeSimple.Contains("ListResultDto")) + .Where(action => action.Value.ReturnValue.TypeSimple.Contains("ListResultDto") + || action.Value.ReturnValue.TypeSimple.Contains("PagedResultDto")) .Select(action => action.Value) .FirstOrDefault(); @@ -49,69 +78,246 @@ public class VbenModelScriptGenerator : IVbenModelScriptGenerator, ISingletonDep if (listResultAction != null) { - foreach (var inputParamter in listResultAction.Parameters) + searchModels.AddRange(GetSearchComponents(appModel, listResultAction.Parameters)); + + foreach (var searchParamter in listResultAction.Parameters) { - if (appModel.Types.TryGetValue(inputParamter.Type, out var inputModelType)) + var abpBaseType = GetAbpBaseType(appModel, searchParamter.TypeSimple); + if (abpBaseType != null) { - var span = inputModelType.Properties.Length > 3 ? 8 - : inputModelType.Properties.Length > 2 ? 12 - : 24; - foreach (var inputModelProp in inputModelType.Properties) - { - var component = new ComponentModel - { - Name = inputModelProp.JsonName ?? inputModelProp.Name.ToCamelCase(), - Component = "Input", - DisplayName = "DisplayName:" + inputModelProp.Name.ToPascalCase(), - Required = inputModelProp.IsRequired, - ColProps = "" + - "{" + - $"span: {span}" + - "}", - }; - - if (appModel.Types.TryGetValue(inputModelProp.Type, out var inputModelPropType) && - inputModelPropType.IsEnum) - { - component.Component = "Select"; - - var optionsStr = Environment.NewLine; - - var options = new object[inputModelPropType.EnumNames.Length]; - for (var index = 0; index < inputModelPropType.EnumNames.Length; index++) - { - optionsStr += "" + - "{" + - $"label: {inputModelPropType.EnumNames[index]}," + - $"value: {inputModelPropType.EnumValues[index]}," + - "}" + - "" + Environment.NewLine; - } - component.ComponentProps = "" + - "{" + - $"options: [{optionsStr}]" + - "}"; - } + searchModels.AddRange(GetSearchComponents(appModel, abpBaseType)); + } - searchModels.Add(component); - } + var inputParamterType = ReplaceAbpBaseType(searchParamter.TypeSimple); + if (appModel.Types.TryGetValue(inputParamterType, out var inputModelType)) + { + searchModels.AddRange(GetSearchComponents(appModel, inputModelType)); } + + searchModels = searchModels.DistinctBy(model => model.Name).ToList(); } } - var createOrUpdateAction = controllerModel.Actions - .Where(action => action.Value.UniqueName.Contains("Create") || action.Value.UniqueName.Contains("Update")) + var createAndUpdateActions = controllerModel.Actions + .Where(action => action.Value.UniqueName.Contains("CreateAsync") || action.Value.UniqueName.Contains("UpdateAsync")) .Select(action => action.Value) - .FirstOrDefault(); + .ToList(); + + foreach (var createAndUpdateAction in createAndUpdateActions) + { + foreach (var createAndUpdateParamter in createAndUpdateAction.Parameters) + { + var abpBaseType = GetAbpBaseType(appModel, createAndUpdateParamter.TypeSimple); + if (abpBaseType != null) + { + inputModels.AddRange(GetComponents(appModel, abpBaseType)); + } + + var inputParamterType = ReplaceAbpBaseType(createAndUpdateParamter.TypeSimple); + if (appModel.Types.TryGetValue(inputParamterType, out var inputModelType)) + { + inputModels.AddRange(GetComponents(appModel, inputModelType)); + } + + inputModels = inputModels.DistinctBy(model => model.Name).ToList(); + } + } + + var modelDataContent = await _templateRenderer.RenderAsync( + "VbenModelData", + new + { + Key = "id", + RemoteService = moduleDefinition.RemoteServiceName, + ExistsSearchModels = searchModels.Any(), + SearchModels = searchModels, + InputModels = inputModels, + }); + + return modelDataContent; + } + + protected virtual List GetSearchComponents(ApplicationApiDescriptionModel appModel, TypeApiDescriptionModel parameter) + { + var components = new List(); + + if (!parameter.BaseType.IsNullOrWhiteSpace()) + { + var abpBaseType = GetAbpBaseType(appModel, parameter.BaseType); + if (abpBaseType != null) + { + components.AddRange(GetSearchComponents(appModel, abpBaseType)); + } + + var baseParamterType = ReplaceAbpBaseType(parameter.BaseType); + if (appModel.Types.TryGetValue(baseParamterType, out var baseModelType)) + { + components.AddRange(GetSearchComponents(appModel, baseModelType)); + } + } + + components.AddRange(GetSearchComponents(appModel, parameter.Properties)); + + return components; + } + + protected virtual List GetSearchComponents(ApplicationApiDescriptionModel appModel, IEnumerable parameters) + { + var components = new List(); + + var span = parameters.Count() > 3 ? 8 + : parameters.Count() > 2 ? 12 + : 24; + + foreach (var inputModelProp in parameters) + { + if (RemoveInputModelFields.Contains(inputModelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + continue; + } + var component = new ComponentModel + { + Name = inputModelProp.JsonName ?? inputModelProp.Name.ToCamelCase(), + Component = "Input", + DisplayName = "DisplayName:" + inputModelProp.Name.ToPascalCase(), + Required = inputModelProp.IsRequired, + ColProps = "" + + "{" + + $"span: {span}" + + "}", + }; + + if (appModel.Types.TryGetValue(inputModelProp.Type, out var inputModelPropType) && + inputModelPropType.IsEnum) + { + component.Component = "Select"; + + var optionsStr = Environment.NewLine; + + var options = new object[inputModelPropType.EnumNames.Length]; + for (var index = 0; index < inputModelPropType.EnumNames.Length; index++) + { + optionsStr += "" + + "{" + + $"label: {inputModelPropType.EnumNames[index]}," + + $"value: {inputModelPropType.EnumValues[index]}," + + "}" + + "" + Environment.NewLine; + } + component.ComponentProps = "" + + "{" + + $"options: [{optionsStr}]" + + "}"; + } + + components.Add(component); + } + + return components; + } + + protected virtual List GetSearchComponents(ApplicationApiDescriptionModel appModel, IEnumerable parameters) + { + var components = new List(); + + var span = parameters.Count() > 3 ? 8 + : parameters.Count() > 2 ? 12 + : 24; + + foreach (var inputModelProp in parameters) + { + if (RemoveInputModelFields.Contains(inputModelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + continue; + } + var component = new ComponentModel + { + Name = inputModelProp.JsonName ?? inputModelProp.Name.ToCamelCase(), + Component = "Input", + DisplayName = "DisplayName:" + inputModelProp.Name.ToPascalCase(), + Required = !inputModelProp.IsOptional, + ColProps = "" + + "{" + + $"span: {span}" + + "}", + }; + + if (appModel.Types.TryGetValue(inputModelProp.Type, out var inputModelPropType) && + inputModelPropType.IsEnum) + { + component.Component = "Select"; - if (createOrUpdateAction != null) + var optionsStr = Environment.NewLine; + + var options = new object[inputModelPropType.EnumNames.Length]; + for (var index = 0; index < inputModelPropType.EnumNames.Length; index++) + { + optionsStr += "" + + "{" + + $"label: {inputModelPropType.EnumNames[index]}," + + $"value: {inputModelPropType.EnumValues[index]}," + + "}" + + "" + Environment.NewLine; + } + component.ComponentProps = "" + + "{" + + $"options: [{optionsStr}]" + + "}"; + } + + components.Add(component); + } + + return components; + } + + protected virtual List GetComponents(ApplicationApiDescriptionModel appModel, TypeApiDescriptionModel parameter) + { + var components = new List(); + + if (!parameter.BaseType.IsNullOrWhiteSpace()) { - foreach (var createOrUpdateParamter in createOrUpdateAction.Parameters) + var abpBaseType = GetAbpBaseType(appModel, parameter.BaseType); + if (abpBaseType != null) { - if (appModel.Types.TryGetValue(createOrUpdateParamter.Type, out var inputModelType)) + components.AddRange(GetComponents(appModel, abpBaseType)); + } + + var baseParamterType = ReplaceAbpBaseType(parameter.BaseType); + if (appModel.Types.TryGetValue(baseParamterType, out var baseModelType)) + { + components.AddRange(GetComponents(appModel, baseModelType)); + } + } + + foreach (var modelProp in parameter.Properties) + { + var inputParamterType = ReplaceAbpBaseType(modelProp.TypeSimple); + if (appModel.Types.TryGetValue(inputParamterType, out var inputModelType)) + { + if (!inputModelType.BaseType.IsNullOrWhiteSpace()) + { + var abpBaseType = GetAbpBaseType(appModel, inputModelType.BaseType); + if (abpBaseType != null) + { + components.AddRange(GetComponents(appModel, abpBaseType)); + } + + var baseParamterType = ReplaceAbpBaseType(inputModelType.BaseType); + if (appModel.Types.TryGetValue(baseParamterType, out var baseModelType)) + { + components.AddRange(GetComponents(appModel, baseModelType)); + } + } + + if (inputModelType.Properties != null) { foreach (var inputModelProp in inputModelType.Properties) { + if (RemoveInputModelFields.Contains(inputModelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + continue; + } var component = new ComponentModel { Name = inputModelProp.JsonName ?? inputModelProp.Name.ToCamelCase(), @@ -125,23 +331,48 @@ public class VbenModelScriptGenerator : IVbenModelScriptGenerator, ISingletonDep ComponentProps = "{}" }; - inputModels.Add(component); + if (DisableInputModelFields.Contains(inputModelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + component.Show = false; + component.Disabled = true; + } + + components.Add(component); } } } - } - - var modelDataContent = await _templateRenderer.RenderAsync( - "VbenModelData", - new + else { - RemoteService = moduleDefinition.RemoteServiceName, - ExistsSearchModels = searchModels.Any(), - SearchModels = searchModels, - InputModels = inputModels, - }); + if (RemoveInputModelFields.Contains(modelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + return components; + } - return modelDataContent; + var component = new ComponentModel + { + Name = modelProp.JsonName ?? modelProp.Name.ToCamelCase(), + Component = "Input", + DisplayName = "DisplayName:" + modelProp.Name.ToPascalCase(), + Required = modelProp.IsRequired, + ColProps = "" + + "{" + + "span: 24" + + "}", + ComponentProps = "{}" + }; + + if (DisableInputModelFields.Contains(modelProp.Name, StringComparer.InvariantCultureIgnoreCase)) + { + component.Show = false; + component.Disabled = true; + component.Width = 1; + } + + components.Add(component); + } + } + + return components; } public async virtual Task CreateTable( @@ -164,37 +395,71 @@ public class VbenModelScriptGenerator : IVbenModelScriptGenerator, ISingletonDep if (listResultAction != null) { - foreach (var inputParamter in listResultAction.Parameters) + var abpBaseType = GetAbpBaseType(appModel, listResultAction.ReturnValue.TypeSimple); + if (abpBaseType != null) { - if (appModel.Types.TryGetValue(inputParamter.Type, out var inputModelType)) - { - var span = inputModelType.Properties.Length > 3 ? 8 - : inputModelType.Properties.Length > 2 ? 12 - : 24; - foreach (var inputModelProp in inputModelType.Properties) - { - var component = new ComponentModel - { - Name = inputModelProp.JsonName ?? inputModelProp.Name.ToCamelCase(), - DisplayName = "DisplayName:" + inputModelProp.Name.ToPascalCase() - }; + ouputModels.AddRange(GetComponents(appModel, abpBaseType)); + } - ouputModels.Add(component); - } - } + var returnValueType = ReplaceAbpBaseType(listResultAction.ReturnValue.TypeSimple); + if (appModel.Types.TryGetValue(returnValueType, out var inputModelType)) + { + ouputModels.AddRange(GetComponents(appModel, inputModelType)); } + + ouputModels = ouputModels.DistinctBy(model => model.Name).ToList(); } var tableDataContent = await _templateRenderer.RenderAsync( "VbenTableData", new { + Key = "id", RemoteService = moduleDefinition.RemoteServiceName, OuputModels = ouputModels, }); return tableDataContent; } + + protected virtual bool IsAbpBaseType(string typeSimple) => TypeScriptModelGenerator.AbpBaseTypes.Any(typeSimple.StartsWith); + + protected virtual string GetAbpBaseType(string typeSimple) => + TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(typeSimple.StartsWith); + + protected virtual TypeApiDescriptionModel GetAbpBaseType(ApplicationApiDescriptionModel appModel, string typeSimple) + { + if (IsAbpBaseType(typeSimple)) + { + var abpBaseType = GetAbpBaseType(typeSimple); + + if (abpBaseType == typeSimple) + { + return null; + } + + return appModel.Types + .Where(type => type.Key.StartsWith(abpBaseType)) + .Select(type => type.Value) + .FirstOrDefault(); + } + + return null; + } + + protected virtual string ReplaceAbpBaseType(string typeSimple) + { + var abpBaseType = TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(basType => typeSimple.StartsWith(basType)); + if (!abpBaseType.IsNullOrWhiteSpace()) + { + typeSimple = typeSimple + .Replace(abpBaseType, "") + .Replace("<", "") + .Replace(">", ""); + } + + return typeSimple; + } } public class ComponentModel @@ -206,6 +471,8 @@ public class ComponentModel public object ComponentProps { get; set; } public string Align { get; set; } = "left"; public int Width { get; set; } = 120; + public bool Show { get; set; } = true; + public bool Disabled { get; set; } = false; public bool Sorter { get; set; } = true; public bool Resizable { get; set; } = true; public bool Required { get; set; } = false; diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenViewScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenViewScriptGenerator.cs index fd5543eff..88797708c 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenViewScriptGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/UI/Vben/VbenViewScriptGenerator.cs @@ -30,6 +30,7 @@ public class VbenViewScriptGenerator : IVbenViewScriptGenerator, ISingletonDepen "VbenModalView", new { + Key = "id", Application = controllerModel.ControllerName, ApiPath = $"/@/api/{moduleDefinition.RemoteServiceName.ToKebabCase()}/{controllerModel.ControllerName.ToKebabCase()}", RemoteService = moduleDefinition.RemoteServiceName, @@ -66,6 +67,7 @@ public class VbenViewScriptGenerator : IVbenViewScriptGenerator, ISingletonDepen "VbenTableView", new { + Key = "id", PagedRequest = pagedResultAction != null, UpdatePermission = updateAction != null && updateAction.AllowAnonymous != null && updateAction.AllowAnonymous != true, UpdatePermissionName = $"{moduleDefinition.RemoteServiceName.ToKebabCase()}.{controllerModel.ControllerName.ToKebabCase()}.Update", diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/Properties/launchSettings.json b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/Properties/launchSettings.json index c3db4819e..c80a9844a 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/Properties/launchSettings.json +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "LINGYUN.Abp.Cli": { "commandName": "Project", - "commandLineArgs": "generate-view -t vben-view -m identity -o D:\\Projects\\Development\\view-script -url http://127.0.0.1:30015/" + "commandLineArgs": "generate-view -t vben-view -m auditing -o D:\\Projects\\Development\\view-script -url http://127.0.0.1:30000/" //"commandLineArgs": "generate-proxy -t ts -asp uni-app-axios -u http://127.0.0.1:30025 -m Platform -o D:\\Projects\\Development\\type-script" } }