diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/AbpCliModule.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/AbpCliModule.cs index efb6b5e44..2d8f1bea4 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/AbpCliModule.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/AbpCliModule.cs @@ -29,6 +29,14 @@ namespace LINGYUN.Abp.Cli options.Generators[TypeScriptServiceProxyGenerator.Name] = typeof(TypeScriptServiceProxyGenerator); options.Generators[CSharpServiceProxyGenerator.Name] = typeof(CSharpServiceProxyGenerator); }); + + Configure(options => + { + options.ScriptGenerators[AxiosHttpApiScriptGenerator.Name] = new AxiosHttpApiScriptGenerator(); + options.ScriptGenerators[VbenAxiosHttpApiScriptGenerator.Name] = new VbenAxiosHttpApiScriptGenerator(); + options.ScriptGenerators[VbenDynamicHttpApiScriptGenerator.Name] = new VbenDynamicHttpApiScriptGenerator(); + options.ScriptGenerators[UniAppAxiosHttpApiScriptGenerator.Name] = new UniAppAxiosHttpApiScriptGenerator(); + }); } } } diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/Commands/GenerateProxyCommand.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/Commands/GenerateProxyCommand.cs index a09b83358..e2e8ddcb3 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/Commands/GenerateProxyCommand.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/Commands/GenerateProxyCommand.cs @@ -1,4 +1,5 @@ using LINGYUN.Abp.Cli.ServiceProxying; +using LINGYUN.Abp.Cli.ServiceProxying.TypeScript; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System; @@ -54,6 +55,8 @@ public class GenerateProxyCommand : IConsoleCommand, ITransientDependency private VoloGenerateProxyArgs BuildArgs(CommandLineArgs commandLineArgs) { var provider = commandLineArgs.Options.GetOrNull(Options.Provider.Short, Options.Provider.Long); + var apiScriptProxy = commandLineArgs.Options.GetOrNull(Options.ApiScriptProxy.Short, Options.ApiScriptProxy.Long) ?? + VbenDynamicHttpApiScriptGenerator.Name; var url = commandLineArgs.Options.GetOrNull(Options.Url.Short, Options.Url.Long); var target = commandLineArgs.Options.GetOrNull(Options.Target.Long); var module = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.Module.Long) ?? "app"; @@ -63,7 +66,19 @@ public class GenerateProxyCommand : IConsoleCommand, ITransientDependency var workDirectory = commandLineArgs.Options.GetOrNull(Options.WorkDirectory.Short, Options.WorkDirectory.Long) ?? Directory.GetCurrentDirectory(); var folder = commandLineArgs.Options.GetOrNull(Options.Folder.Long); - return new GenerateProxyArgs(CommandName, workDirectory, module, url, output, target, apiName, source, folder, provider, commandLineArgs.Options); + return new GenerateProxyArgs( + CommandName, + workDirectory, + module, + url, + output, + target, + apiName, + source, + folder, + provider, + apiScriptProxy, + commandLineArgs.Options); } public string GetUsageInfo() @@ -83,7 +98,8 @@ public class GenerateProxyCommand : IConsoleCommand, ITransientDependency sb.AppendLine("-t|--type The name of generate type (csharp, ts)."); sb.AppendLine(" csharp"); sb.AppendLine(" --folder (default: 'ClientProxies') Folder name to place generated CSharp code in."); - sb.AppendLine(" ts"); + sb.AppendLine(" ts"); + sb.AppendLine(" -asp|--api-script-proxy The generated api proxy type(axios, vben-axios, vben-dynamic). default: vben-dynamic."); sb.AppendLine(" -o|--output TypeScript file path or folder to place generated code in."); sb.AppendLine("-p|--provider The client proxy provider(http, dapr)."); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); @@ -95,7 +111,8 @@ public class GenerateProxyCommand : IConsoleCommand, ITransientDependency sb.AppendLine(" labp generate-proxy -p dapr"); sb.AppendLine(" labp generate-proxy -m identity -o Pages/Identity/client-proxies.js -url https://localhost:44302/"); sb.AppendLine(" labp generate-proxy --folder MyProxies/InnerFolder -url https://localhost:44302/"); - sb.AppendLine(" labp generate-proxy -t ts -m identity -o api/identity -url https://localhost:44302/ "); + sb.AppendLine(" labp generate-proxy -t ts -m identity -o api/identity -url https://localhost:44302/"); + sb.AppendLine(" labp generate-proxy -t ts -asp vben-dynamic -m identity -o api/identity -url https://localhost:44302/"); return sb.ToString(); } @@ -119,6 +136,12 @@ public class GenerateProxyCommand : IConsoleCommand, ITransientDependency public const string Long = "provider"; } + public static class ApiScriptProxy + { + public const string Short = "asp"; + public const string Long = "api-script-proxy"; + } + public static class Module { public const string Short = "m"; diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs index 8be4f1660..688d77ea0 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs @@ -7,6 +7,8 @@ public class GenerateProxyArgs : Volo.Abp.Cli.ServiceProxying.GenerateProxyArgs { public string Provider { get; } + public string ApiScriptProxy { get; } + public GenerateProxyArgs( [NotNull] string commandName, [NotNull] string workDirectory, @@ -18,9 +20,11 @@ public class GenerateProxyArgs : Volo.Abp.Cli.ServiceProxying.GenerateProxyArgs string source, string folder, string provider, + string apiScriptProxy, Dictionary extraProperties = null) : base(commandName, workDirectory, module, url, output, target, apiName, source, folder, extraProperties) { + ApiScriptProxy = apiScriptProxy; Provider = provider; } } diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/AxiosHttpApiScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/AxiosHttpApiScriptGenerator.cs new file mode 100644 index 000000000..441eaef11 --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/AxiosHttpApiScriptGenerator.cs @@ -0,0 +1,231 @@ +using System; +using System.Linq; +using System.Text; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Modeling; + +namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; + +// axios原生代理生成 +public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDependency +{ + public const string Name = "axios"; + + public string CreateScript( + ApplicationApiDescriptionModel appModel, + ModuleApiDescriptionModel apiModel, + ControllerApiDescriptionModel actionModel) + { + var apiScriptBuilder = new StringBuilder(); + + apiScriptBuilder.AppendLine("import axios from 'axios';"); + + var importModel = ""; + + foreach (var action in actionModel.Actions) + { + foreach (var paramter in action.Value.ParametersOnMethod) + { + if (appModel.Types.TryGetValue(paramter.Type, out var _)) + { + var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; + + if (!importModel.Contains(modelTypeName)) + { + importModel += modelTypeName + ", "; + } + } + } + + var returnType = action.Value.ReturnValue.TypeSimple; + if (!returnType.StartsWith("System")) + { + var abpBaseType = TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(basType => returnType.StartsWith(basType)); + if (!abpBaseType.IsNullOrWhiteSpace()) + { + returnType = returnType + .Replace(abpBaseType, "") + .Replace("<", "") + .Replace(">", ""); + } + + returnType = ReplaceTypeSimple(returnType); + returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; + if (!importModel.Contains(returnType)) + { + importModel += returnType + ","; + } + } + } + importModel = importModel.RemovePostFix(","); + + apiScriptBuilder.AppendLine("import { " + importModel + " } from './model';"); + apiScriptBuilder.AppendLine(""); + + foreach (var action in actionModel.Actions) + { + var url = action.Value.Url; + var isFormatUrl = false; + + apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); + + for (var index = 0; index < action.Value.ParametersOnMethod.Count; index++) + { + var paramter = action.Value.ParametersOnMethod[index]; + var apiParamCharacter = paramter.IsOptional ? "?:" : ":"; + var apiParamName = paramter.TypeSimple; + apiParamName = apiParamName[(apiParamName.LastIndexOf('.') + 1)..]; + apiScriptBuilder.AppendFormat("{0}{1} {2}", paramter.Name, apiParamCharacter, apiParamName); + + if (index < action.Value.ParametersOnMethod.Count - 1) + { + apiScriptBuilder.Append(", "); + } + + // 需要格式化url + if (url.Contains('{') && url.Contains(paramter.Name)) + { + var formatUrl = MiddleString(url, "{", "}"); + url = url.Replace(formatUrl, ""); + url = "'" + url + "'" + " + " + paramter.Name; + isFormatUrl = true; + } + } + + apiScriptBuilder.AppendLine(") => {"); + + var apiRetuanName = action.Value.ReturnValue.TypeSimple; + + if (apiRetuanName.Contains("ListResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"ListResultDto<{apiRetuanName}>"; + } + else if (apiRetuanName.Contains("PagedResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"PagedResultDto<{apiRetuanName}>"; + } + else + { + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + } + + if (action.Value.ReturnValue.TypeSimple.Contains("System.")) + { + apiRetuanName = apiRetuanName.ToLower(); + } + + if (!url.StartsWith("'") && !url.EndsWith("'")) + { + url = "'" + url + "'"; + } + apiScriptBuilder.AppendFormat(" return axios.request<{0}>(", apiRetuanName); + apiScriptBuilder.AppendLine("{"); + apiScriptBuilder.AppendFormat(" method: '{0}',", action.Value.HttpMethod); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat(" url: {0},", url); + apiScriptBuilder.AppendLine(""); + + if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + { + if (!isFormatUrl && action.Value.ParametersOnMethod.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + + foreach (var paramter in action.Value.ParametersOnMethod) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + + apiScriptBuilder.AppendLine(" },"); + } + } + else + { + var inPathParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Path"); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + + if (!isFormatUrl && inPathParams.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + foreach (var paramter in inPathParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + + if (inBodyParams.Any() && + inBodyParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else + { + apiScriptBuilder.AppendLine(" data: {"); + foreach (var paramter in inBodyParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + } + + apiScriptBuilder.AppendLine(" });"); + apiScriptBuilder.AppendLine("};"); + } + + return apiScriptBuilder.ToString(); + } + + protected virtual string ReplaceTypeSimple(string typeSimple) + { + typeSimple = typeSimple + .Replace("?", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("{string:string}", "Dictionary") + .Replace("{number:string}", "Dictionary") + .Replace("{string:number}", "Dictionary") + .Replace("{string:object}", "Dictionary"); + + if (typeSimple.StartsWith("[") && typeSimple.EndsWith("]")) + { + typeSimple = typeSimple.ReplaceFirst("[", "").RemovePostFix("]", ""); + typeSimple = typeSimple.Replace(typeSimple, $"{typeSimple}[]"); + } + + return typeSimple; + } + + public static string MiddleString(string sourse, string startstr, string endstr) + { + var result = string.Empty; + int startindex, endindex; + startindex = sourse.IndexOf(startstr); + if (startindex == -1) + { + return result; + } + var tmpstr = sourse.Substring(startindex + startstr.Length - 1); + endindex = tmpstr.IndexOf(endstr); + if (endindex == -1) + { + return result; + } + result = tmpstr.Remove(endindex + 1); + + return result; + } +} diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptProxyGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/IHttpApiScriptGenerator.cs similarity index 57% rename from aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptProxyGenerator.cs rename to aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/IHttpApiScriptGenerator.cs index 83f36f61d..58c576d7c 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptProxyGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/IHttpApiScriptGenerator.cs @@ -2,14 +2,10 @@ namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; -public interface ITypeScriptProxyGenerator +public interface IHttpApiScriptGenerator { - string CreateModelScript( - ApplicationApiDescriptionModel appModel, - ControllerApiDescriptionModel actionModel); - string CreateScript( - ApplicationApiDescriptionModel appModel, + ApplicationApiDescriptionModel appModel, ModuleApiDescriptionModel apiModel, ControllerApiDescriptionModel actionModel); } diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptModelGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptModelGenerator.cs new file mode 100644 index 000000000..e1093c742 --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/ITypeScriptModelGenerator.cs @@ -0,0 +1,10 @@ +using Volo.Abp.Http.Modeling; + +namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; + +public interface ITypeScriptModelGenerator +{ + string CreateScript( + ApplicationApiDescriptionModel appModel, + ControllerApiDescriptionModel actionModel); +} diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptProxyGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs similarity index 57% rename from aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptProxyGenerator.cs rename to aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs index 318ab40ef..be7833787 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptProxyGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs @@ -9,9 +9,9 @@ using Volo.Abp.Http.Modeling; using Volo.Abp.ObjectExtending; namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; -public class TypeScriptProxyGenerator : ITypeScriptProxyGenerator, ITransientDependency +public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDependency { - protected static string[] AbpBaseTypes = new string[] + internal readonly static string[] AbpBaseTypes = new string[] { typeof(ExtensibleObject).FullName, "Volo.Abp.Application.Dtos.AuditedEntityDto", @@ -34,19 +34,22 @@ public class TypeScriptProxyGenerator : ITypeScriptProxyGenerator, ITransientDep "Volo.Abp.Application.Dtos.PagedResultDto", "Volo.Abp.Application.Dtos.PagedResultRequestDto", }; - protected static string[] DataInParamMethods = new string[] + internal readonly static string[] DataInParamMethods = new string[] { "GET", "DELETE" }; - public ILogger Logger { protected get; set; } - public TypeScriptProxyGenerator() + public ILogger Logger { protected get; set; } + + public TypeScriptModelGenerator() { - Logger = NullLogger.Instance; + Logger = NullLogger.Instance; } - public string CreateModelScript(ApplicationApiDescriptionModel appModel, ControllerApiDescriptionModel actionModel) + public string CreateScript( + ApplicationApiDescriptionModel appModel, + ControllerApiDescriptionModel actionModel) { var modelScriptBuilder = new StringBuilder(); var modelBaseTypes = new List(); @@ -163,171 +166,6 @@ public class TypeScriptProxyGenerator : ITypeScriptProxyGenerator, ITransientDep return modelScriptBuilder.ToString(); } - public string CreateScript( - ApplicationApiDescriptionModel appModel, - ModuleApiDescriptionModel apiModel, - ControllerApiDescriptionModel actionModel) - { - var apiScriptBuilder = new StringBuilder(); - - apiScriptBuilder.AppendLine("import { defAbpHttp } from '/@/utils/http/abp';"); - - var importModel = ""; - - foreach (var action in actionModel.Actions) - { - foreach (var paramter in action.Value.ParametersOnMethod) - { - if (appModel.Types.TryGetValue(paramter.Type, out var _)) - { - var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; - - if (!importModel.Contains(modelTypeName)) - { - importModel += modelTypeName + ", "; - } - } - } - - var returnType = action.Value.ReturnValue.TypeSimple; - if (!returnType.StartsWith("System")) - { - var abpBaseType = AbpBaseTypes.FirstOrDefault(basType => returnType.StartsWith(basType)); - if (!abpBaseType.IsNullOrWhiteSpace()) - { - returnType = returnType - .Replace(abpBaseType, "") - .Replace("<", "") - .Replace(">", ""); - } - - returnType = ReplaceTypeSimple(returnType); - returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; - if (!importModel.Contains(returnType)) - { - importModel += returnType + ","; - } - } - } - importModel = importModel.RemovePostFix(","); - - apiScriptBuilder.AppendLine("import { " + importModel + " } from './model';"); - apiScriptBuilder.AppendLine(""); - apiScriptBuilder.AppendFormat("const remoteServiceName = '{0}';", apiModel.RemoteServiceName); - apiScriptBuilder.AppendLine(""); - apiScriptBuilder.AppendFormat("const controllerName = '{0}';", actionModel.ControllerName); - apiScriptBuilder.AppendLine(""); - apiScriptBuilder.AppendLine(""); - - foreach (var action in actionModel.Actions) - { - apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); - - for (var index = 0; index < action.Value.ParametersOnMethod.Count; index++) - { - var paramter = action.Value.ParametersOnMethod[index]; - var apiParamCharacter = paramter.IsOptional ? "?:" : ":"; - var apiParamName = paramter.TypeSimple; - apiParamName = apiParamName[(apiParamName.LastIndexOf('.') + 1)..]; - apiScriptBuilder.AppendFormat("{0}{1} {2}", paramter.Name, apiParamCharacter, apiParamName); - - if (index < action.Value.ParametersOnMethod.Count - 1) - { - apiScriptBuilder.Append(", "); - } - } - - apiScriptBuilder.AppendLine(") => {"); - - var apiRequestName = "request"; - var apiRetuanName = action.Value.ReturnValue.TypeSimple; - - if (apiRetuanName.Contains("ListResultDto")) - { - apiRequestName = "listRequest"; - apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; - apiRetuanName = apiRetuanName[..^1]; - } - else if (apiRetuanName.Contains("PagedResultDto")) - { - apiRequestName = "pagedRequest"; - apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; - apiRetuanName = apiRetuanName[..^1]; - } - - apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; - - if (action.Value.ReturnValue.TypeSimple.Contains("System.")) - { - apiRetuanName = apiRetuanName.ToLower(); - } - - apiScriptBuilder.AppendFormat(" return defAbpHttp.{0}<{1}>(", apiRequestName, apiRetuanName); - apiScriptBuilder.AppendLine("{"); - apiScriptBuilder.AppendLine(" service: remoteServiceName,"); - apiScriptBuilder.AppendLine(" controller: controllerName,"); - apiScriptBuilder.AppendFormat(" action: '{0}',", action.Value.Name); - apiScriptBuilder.AppendLine(""); - apiScriptBuilder.AppendFormat(" uniqueName: '{0}',", action.Value.UniqueName); - apiScriptBuilder.AppendLine(""); - - if (DataInParamMethods.Contains(action.Value.HttpMethod)) - { - if (action.Value.ParametersOnMethod.Any()) - { - apiScriptBuilder.AppendLine(" params: {"); - - foreach (var paramter in action.Value.ParametersOnMethod) - { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); - apiScriptBuilder.AppendLine(""); - } - - apiScriptBuilder.AppendLine(" },"); - } - } - else - { - var inPathParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Path"); - var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); - - if (inPathParams.Any()) - { - apiScriptBuilder.AppendLine(" params: {"); - foreach (var paramter in inPathParams) - { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); - apiScriptBuilder.AppendLine(""); - } - apiScriptBuilder.AppendLine(" },"); - } - - if (inBodyParams.Any() && - inBodyParams.Count() == 1) - { - apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); - apiScriptBuilder.AppendLine(""); - } - else - { - apiScriptBuilder.AppendLine(" data: {"); - foreach (var paramter in inBodyParams) - { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); - apiScriptBuilder.AppendLine(""); - } - apiScriptBuilder.AppendLine(" },"); - } - } - - apiScriptBuilder.AppendLine(" });"); - - apiScriptBuilder.AppendLine("};"); - } - - return apiScriptBuilder.ToString(); - } - protected virtual string CreateModel( string modelName, TypeApiDescriptionModel model) diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptServiceProxyGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptServiceProxyGenerator.cs index c751ccf97..9aebe281a 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptServiceProxyGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptServiceProxyGenerator.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.IO; using System.Linq; @@ -17,15 +18,18 @@ public class TypeScriptServiceProxyGenerator : ServiceProxyGeneratorBase typeScriptServiceProxyOptions) : base(cliHttpClientFactory, jsonSerializer) { - _typeScriptProxyGenerator = typeScriptProxyGenerator; + _typeScriptModelGenerator = typeScriptModelGenerator; + _typeScriptServiceProxyOptions = typeScriptServiceProxyOptions.Value; } public async override Task GenerateProxyAsync(Volo.Abp.Cli.ServiceProxying.GenerateProxyArgs args) @@ -41,8 +45,8 @@ public class TypeScriptServiceProxyGenerator : ServiceProxyGeneratorBase ScriptGenerators { get; } + public TypeScriptServiceProxyOptions() + { + ScriptGenerators = new Dictionary(); + } +} diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/UniAppAxiosHttpApiScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/UniAppAxiosHttpApiScriptGenerator.cs new file mode 100644 index 000000000..d7fed2325 --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/UniAppAxiosHttpApiScriptGenerator.cs @@ -0,0 +1,233 @@ +using System; +using System.Linq; +using System.Text; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Modeling; + +namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; + +// 自用,请勿使用 +// 适用于uni-app的axios +public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDependency +{ + public const string Name = "uni-app-axios"; + + public string CreateScript( + ApplicationApiDescriptionModel appModel, + ModuleApiDescriptionModel apiModel, + ControllerApiDescriptionModel actionModel) + { + var apiScriptBuilder = new StringBuilder(); + + apiScriptBuilder.AppendLine("import http from '../http'"); + + var importModel = ""; + + foreach (var action in actionModel.Actions) + { + foreach (var paramter in action.Value.ParametersOnMethod) + { + if (appModel.Types.TryGetValue(paramter.Type, out var _)) + { + var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; + + if (!importModel.Contains(modelTypeName)) + { + importModel += modelTypeName + ", "; + } + } + } + + var returnType = action.Value.ReturnValue.TypeSimple; + if (!returnType.StartsWith("System")) + { + var abpBaseType = TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(basType => returnType.StartsWith(basType)); + if (!abpBaseType.IsNullOrWhiteSpace()) + { + returnType = returnType + .Replace(abpBaseType, "") + .Replace("<", "") + .Replace(">", ""); + } + + returnType = ReplaceTypeSimple(returnType); + returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; + if (!importModel.Contains(returnType)) + { + importModel += returnType + ","; + } + } + } + importModel = importModel.RemovePostFix(","); + + apiScriptBuilder.AppendLine("import { " + importModel + " } from './model'"); + apiScriptBuilder.AppendLine(""); + + foreach (var action in actionModel.Actions) + { + var url = action.Value.Url; + var isFormatUrl = false; + + apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); + + for (var index = 0; index < action.Value.ParametersOnMethod.Count; index++) + { + var paramter = action.Value.ParametersOnMethod[index]; + var apiParamCharacter = paramter.IsOptional ? "?:" : ":"; + var apiParamName = paramter.TypeSimple; + apiParamName = apiParamName[(apiParamName.LastIndexOf('.') + 1)..]; + apiScriptBuilder.AppendFormat("{0}{1} {2}", paramter.Name, apiParamCharacter, apiParamName); + + if (index < action.Value.ParametersOnMethod.Count - 1) + { + apiScriptBuilder.Append(", "); + } + + // 需要格式化url + if (url.Contains('{') && url.Contains(paramter.Name)) + { + var formatUrl = MiddleString(url, "{", "}"); + url = url.Replace(formatUrl, ""); + url = "'" + url + "'" + " + " + paramter.Name; + isFormatUrl = true; + } + } + + var apiRetuanName = action.Value.ReturnValue.TypeSimple; + + if (apiRetuanName.Contains("ListResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"ListResultDto<{apiRetuanName}>"; + } + else if (apiRetuanName.Contains("PagedResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"PagedResultDto<{apiRetuanName}>"; + } + else + { + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + } + + if (action.Value.ReturnValue.TypeSimple.Contains("System.")) + { + apiRetuanName = apiRetuanName.ToLower(); + } + + apiScriptBuilder.AppendFormat("): Promise<{0}> ", apiRetuanName); + + apiScriptBuilder.AppendLine("=> {"); + + if (!url.StartsWith("'") && !url.EndsWith("'")) + { + url = "'" + url + "'"; + } + apiScriptBuilder.AppendLine(" return http.request({"); + apiScriptBuilder.AppendFormat(" method: '{0}',", action.Value.HttpMethod); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat(" url: {0},", url); + apiScriptBuilder.AppendLine(""); + + if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + { + if (!isFormatUrl && action.Value.ParametersOnMethod.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + + foreach (var paramter in action.Value.ParametersOnMethod) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + + apiScriptBuilder.AppendLine(" },"); + } + } + else + { + var inPathParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Path"); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + + if (!isFormatUrl && inPathParams.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + foreach (var paramter in inPathParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + + if (inBodyParams.Any() && + inBodyParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else + { + apiScriptBuilder.AppendLine(" data: {"); + foreach (var paramter in inBodyParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + } + + apiScriptBuilder.AppendLine(" })"); + apiScriptBuilder.AppendLine("}"); + } + + return apiScriptBuilder.ToString(); + } + + protected virtual string ReplaceTypeSimple(string typeSimple) + { + typeSimple = typeSimple + .Replace("?", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("{string:string}", "Dictionary") + .Replace("{number:string}", "Dictionary") + .Replace("{string:number}", "Dictionary") + .Replace("{string:object}", "Dictionary"); + + if (typeSimple.StartsWith("[") && typeSimple.EndsWith("]")) + { + typeSimple = typeSimple.ReplaceFirst("[", "").RemovePostFix("]", ""); + typeSimple = typeSimple.Replace(typeSimple, $"{typeSimple}[]"); + } + + return typeSimple; + } + + public static string MiddleString(string sourse, string startstr, string endstr) + { + var result = string.Empty; + int startindex, endindex; + startindex = sourse.IndexOf(startstr); + if (startindex == -1) + { + return result; + } + var tmpstr = sourse.Substring(startindex + startstr.Length - 1); + endindex = tmpstr.IndexOf(endstr); + if (endindex == -1) + { + return result; + } + result = tmpstr.Remove(endindex + 1); + + return result; + } +} diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenAxiosHttpApiScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenAxiosHttpApiScriptGenerator.cs new file mode 100644 index 000000000..db20d7f1c --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenAxiosHttpApiScriptGenerator.cs @@ -0,0 +1,241 @@ +using Serilog; +using System; +using System.Linq; +using System.Text; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Modeling; + +namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; + +// 用于vben原生axios代理生成 +public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDependency +{ + public const string Name = "vben-axios"; + + public string CreateScript( + ApplicationApiDescriptionModel appModel, + ModuleApiDescriptionModel apiModel, + ControllerApiDescriptionModel actionModel) + { + var apiScriptBuilder = new StringBuilder(); + + apiScriptBuilder.AppendLine("import { defHttp } from '/@/utils/http/axios';"); + + var importModel = ""; + + foreach (var action in actionModel.Actions) + { + foreach (var paramter in action.Value.ParametersOnMethod) + { + if (appModel.Types.TryGetValue(paramter.Type, out var _)) + { + var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; + + if (!importModel.Contains(modelTypeName)) + { + importModel += modelTypeName + ", "; + } + } + } + + var returnType = action.Value.ReturnValue.TypeSimple; + if (!returnType.StartsWith("System")) + { + var abpBaseType = TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(basType => returnType.StartsWith(basType)); + if (!abpBaseType.IsNullOrWhiteSpace()) + { + returnType = returnType + .Replace(abpBaseType, "") + .Replace("<", "") + .Replace(">", ""); + } + + returnType = ReplaceTypeSimple(returnType); + returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; + if (!importModel.Contains(returnType)) + { + importModel += returnType + ","; + } + } + } + importModel = importModel.RemovePostFix(","); + + apiScriptBuilder.AppendLine("import { " + importModel + " } from './model';"); + apiScriptBuilder.AppendLine(""); + + foreach (var action in actionModel.Actions) + { + var url = action.Value.Url; + + apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); + + for (var index = 0; index < action.Value.ParametersOnMethod.Count; index++) + { + var paramter = action.Value.ParametersOnMethod[index]; + var apiParamCharacter = paramter.IsOptional ? "?:" : ":"; + var apiParamName = paramter.TypeSimple; + apiParamName = apiParamName[(apiParamName.LastIndexOf('.') + 1)..]; + apiScriptBuilder.AppendFormat("{0}{1} {2}", paramter.Name, apiParamCharacter, apiParamName); + + if (index < action.Value.ParametersOnMethod.Count - 1) + { + apiScriptBuilder.Append(", "); + } + + // 需要格式化url + if (url.Contains('{') && url.Contains(paramter.Name)) + { + var formatUrl = MiddleString(url, "{", "}"); + url = url.Replace(formatUrl, ""); + url = "'" + url + "'" + " + " + paramter.Name; + } + } + + apiScriptBuilder.AppendLine(") => {"); + + var apiRetuanName = action.Value.ReturnValue.TypeSimple; + + if (apiRetuanName.Contains("ListResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"ListResultDto<{apiRetuanName}>"; + } + else if (apiRetuanName.Contains("PagedResultDto")) + { + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + apiRetuanName = $"PagedResultDto<{apiRetuanName}>"; + } + else + { + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + } + + if (action.Value.ReturnValue.TypeSimple.Contains("System.")) + { + apiRetuanName = apiRetuanName.ToLower(); + } + + if (!url.StartsWith("'") && !url.EndsWith("'")) + { + url = "'" + url + "'"; + } + apiScriptBuilder.AppendFormat(" return defHttp.request<{0}>(", apiRetuanName); + apiScriptBuilder.AppendLine("{"); + apiScriptBuilder.AppendFormat(" method: '{0}',", action.Value.HttpMethod); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat(" url: {0},", url); + apiScriptBuilder.AppendLine(""); + + if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + { + if (action.Value.ParametersOnMethod.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + + foreach (var paramter in action.Value.ParametersOnMethod) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + + apiScriptBuilder.AppendLine(" },"); + } + } + else + { + var inPathParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Path"); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + + if (inPathParams.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + foreach (var paramter in inPathParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + + if (inBodyParams.Any() && + inBodyParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else + { + apiScriptBuilder.AppendLine(" data: {"); + foreach (var paramter in inBodyParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + } + + apiScriptBuilder.Append(" }"); + + if (action.Value.AllowAnonymous == true) + { + apiScriptBuilder.AppendLine(",{"); + // 匿名方法无需token + apiScriptBuilder.AppendLine(" withToken: false,"); + + apiScriptBuilder.Append(" }"); + } + + apiScriptBuilder.AppendLine(");"); + apiScriptBuilder.AppendLine("};"); + } + + return apiScriptBuilder.ToString(); + } + + protected virtual string ReplaceTypeSimple(string typeSimple) + { + typeSimple = typeSimple + .Replace("?", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("{string:string}", "Dictionary") + .Replace("{number:string}", "Dictionary") + .Replace("{string:number}", "Dictionary") + .Replace("{string:object}", "Dictionary"); + + if (typeSimple.StartsWith("[") && typeSimple.EndsWith("]")) + { + typeSimple = typeSimple.ReplaceFirst("[", "").RemovePostFix("]", ""); + typeSimple = typeSimple.Replace(typeSimple, $"{typeSimple}[]"); + } + + return typeSimple; + } + + public static string MiddleString(string sourse, string startstr, string endstr) + { + var result = string.Empty; + int startindex, endindex; + startindex = sourse.IndexOf(startstr); + if (startindex == -1) + { + return result; + } + var tmpstr = sourse.Substring(startindex + startstr.Length - 1); + endindex = tmpstr.IndexOf(endstr); + if (endindex == -1) + { + return result; + } + result = tmpstr.Remove(endindex + 1); + + return result; + } +} diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenDynamicHttpApiScriptGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenDynamicHttpApiScriptGenerator.cs new file mode 100644 index 000000000..89cba0f78 --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/VbenDynamicHttpApiScriptGenerator.cs @@ -0,0 +1,210 @@ +using System; +using System.Linq; +using System.Text; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Modeling; + +namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; + +// 用于vben动态axios代理生成 +public class VbenDynamicHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDependency +{ + public const string Name = "vben-dynamic"; + + public string CreateScript( + ApplicationApiDescriptionModel appModel, + ModuleApiDescriptionModel apiModel, + ControllerApiDescriptionModel actionModel) + { + var apiScriptBuilder = new StringBuilder(); + + apiScriptBuilder.AppendLine("import { defAbpHttp } from '/@/utils/http/abp';"); + + var importModel = ""; + + foreach (var action in actionModel.Actions) + { + foreach (var paramter in action.Value.ParametersOnMethod) + { + if (appModel.Types.TryGetValue(paramter.Type, out var _)) + { + var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; + + if (!importModel.Contains(modelTypeName)) + { + importModel += modelTypeName + ", "; + } + } + } + + var returnType = action.Value.ReturnValue.TypeSimple; + if (!returnType.StartsWith("System")) + { + var abpBaseType = TypeScriptModelGenerator.AbpBaseTypes.FirstOrDefault(basType => returnType.StartsWith(basType)); + if (!abpBaseType.IsNullOrWhiteSpace()) + { + returnType = returnType + .Replace(abpBaseType, "") + .Replace("<", "") + .Replace(">", ""); + } + + returnType = ReplaceTypeSimple(returnType); + returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; + if (!importModel.Contains(returnType)) + { + importModel += returnType + ","; + } + } + } + importModel = importModel.RemovePostFix(","); + + apiScriptBuilder.AppendLine("import { " + importModel + " } from './model';"); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat("const remoteServiceName = '{0}';", apiModel.RemoteServiceName); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat("const controllerName = '{0}';", actionModel.ControllerName); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendLine(""); + + foreach (var action in actionModel.Actions) + { + apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); + + for (var index = 0; index < action.Value.ParametersOnMethod.Count; index++) + { + var paramter = action.Value.ParametersOnMethod[index]; + var apiParamCharacter = paramter.IsOptional ? "?:" : ":"; + var apiParamName = paramter.TypeSimple; + apiParamName = apiParamName[(apiParamName.LastIndexOf('.') + 1)..]; + apiScriptBuilder.AppendFormat("{0}{1} {2}", paramter.Name, apiParamCharacter, apiParamName); + + if (index < action.Value.ParametersOnMethod.Count - 1) + { + apiScriptBuilder.Append(", "); + } + } + + apiScriptBuilder.AppendLine(") => {"); + + var apiRequestName = "request"; + var apiRetuanName = action.Value.ReturnValue.TypeSimple; + + if (apiRetuanName.Contains("ListResultDto")) + { + apiRequestName = "listRequest"; + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + } + else if (apiRetuanName.Contains("PagedResultDto")) + { + apiRequestName = "pagedRequest"; + apiRetuanName = apiRetuanName[(apiRetuanName.IndexOf("<") + 1)..]; + apiRetuanName = apiRetuanName[..^1]; + } + + apiRetuanName = apiRetuanName[(apiRetuanName.LastIndexOf('.') + 1)..]; + + if (action.Value.ReturnValue.TypeSimple.Contains("System.")) + { + apiRetuanName = apiRetuanName.ToLower(); + } + + apiScriptBuilder.AppendFormat(" return defAbpHttp.{0}<{1}>(", apiRequestName, apiRetuanName); + apiScriptBuilder.AppendLine("{"); + apiScriptBuilder.AppendLine(" service: remoteServiceName,"); + apiScriptBuilder.AppendLine(" controller: controllerName,"); + apiScriptBuilder.AppendFormat(" action: '{0}',", action.Value.Name); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendFormat(" uniqueName: '{0}',", action.Value.UniqueName); + apiScriptBuilder.AppendLine(""); + + if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + { + if (action.Value.ParametersOnMethod.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + + foreach (var paramter in action.Value.ParametersOnMethod) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + + apiScriptBuilder.AppendLine(" },"); + } + } + else + { + var inPathParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Path"); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + + if (inPathParams.Any()) + { + apiScriptBuilder.AppendLine(" params: {"); + foreach (var paramter in inPathParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + + if (inBodyParams.Any() && + inBodyParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else + { + apiScriptBuilder.AppendLine(" data: {"); + foreach (var paramter in inBodyParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + apiScriptBuilder.AppendLine(" },"); + } + } + + apiScriptBuilder.Append(" }"); + + if (action.Value.AllowAnonymous == true) + { + apiScriptBuilder.AppendLine(",{"); + // 匿名方法无需token + apiScriptBuilder.AppendLine(" withToken: false,"); + + apiScriptBuilder.Append(" }"); + } + + apiScriptBuilder.AppendLine(");"); + apiScriptBuilder.AppendLine("};"); + } + + return apiScriptBuilder.ToString(); + } + + protected virtual string ReplaceTypeSimple(string typeSimple) + { + typeSimple = typeSimple + .Replace("?", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("{string:string}", "Dictionary") + .Replace("{number:string}", "Dictionary") + .Replace("{string:number}", "Dictionary") + .Replace("{string:object}", "Dictionary"); + + if (typeSimple.StartsWith("[") && typeSimple.EndsWith("]")) + { + typeSimple = typeSimple.ReplaceFirst("[", "").RemovePostFix("]", ""); + typeSimple = typeSimple.Replace(typeSimple, $"{typeSimple}[]"); + } + + return typeSimple; + } +} 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 69db1f1e0..e6b0acc7c 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-proxy -t ts -u http://127.0.0.1:30045 -m webhooks-management -o D:\\Projects\\Development\\type-script" + "commandLineArgs": "generate-proxy -t ts -asp uni-app-axios -u http://127.0.0.1:30010 -m abp -o D:\\Projects\\Development\\type-script" } } } \ No newline at end of file