diff --git a/apps/vue/types/abp.type.d.ts b/apps/vue/types/abp.type.d.ts index 8843fb470..b8dcafa66 100644 --- a/apps/vue/types/abp.type.d.ts +++ b/apps/vue/types/abp.type.d.ts @@ -74,7 +74,8 @@ declare interface FullAuditedEntityDto extends AuditedEntityDto extends FullAuditedEntityDto { +declare interface FullAuditedEntityWithUserDto + extends FullAuditedEntityDto { creator: TUserDto; lastModifier: TUserDto; deleter: TUserDto; 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 index 441eaef11..c5b4d9ca0 100644 --- 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 @@ -32,7 +32,7 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe if (!importModel.Contains(modelTypeName)) { - importModel += modelTypeName + ", "; + importModel += modelTypeName + ","; } } } @@ -49,7 +49,7 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe .Replace(">", ""); } - returnType = ReplaceTypeSimple(returnType); + returnType = returnType.ReplaceTypeSimple(); returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; if (!importModel.Contains(returnType)) { @@ -66,6 +66,7 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe { var url = action.Value.Url; var isFormatUrl = false; + var formatUrlIndex = 0; apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); @@ -83,13 +84,36 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe } // 需要格式化url - if (url.Contains('{') && url.Contains(paramter.Name)) + var formatUrlPath = string.Concat("{", paramter.Name, "}"); + if (url.Contains(formatUrlPath)) { - var formatUrl = MiddleString(url, "{", "}"); - url = url.Replace(formatUrl, ""); - url = "'" + url + "'" + " + " + paramter.Name; + formatUrlIndex = url.IndexOf(formatUrlPath) + formatUrlPath.Length; + // 'api/platform/packages/{id}/blob/{Name}' => `api/platform/packages/${id}/blob/{input.name}` + url = url.Replace(formatUrlPath, $"${formatUrlPath}"); isFormatUrl = true; } + + if (formatUrlIndex >= 0 && formatUrlIndex + formatUrlPath.Length <= url.Length) + { + var formatUrl = url[(formatUrlIndex + formatUrlPath.Length)..].MiddleString("{", "}"); + if (!formatUrl.IsNullOrWhiteSpace()) + { + if (appModel.Types.TryGetValue(paramter.Type, out var paramType)) + { + var formatParamInUrl = paramType.Properties + .FirstOrDefault(p => formatUrl.Contains(p.Name)); + + if (formatParamInUrl != null) + { + // 'api/platform/packages/xxx/blob/{Name}' => `api/platform/packages/xxx/blob/${input.name}` + url = url.Replace( + formatUrl, + string.Concat("${", paramter.Name, ".", formatParamInUrl.Name.ToCamelCase(), "}")); + isFormatUrl = true; + } + } + } + } } apiScriptBuilder.AppendLine(") => {"); @@ -120,10 +144,15 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe apiRetuanName = apiRetuanName.ToLower(); } - if (!url.StartsWith("'") && !url.EndsWith("'")) + if (isFormatUrl && !url.StartsWith("`") && !url.EndsWith("`")) + { + url = "`" + url + "`"; + } + else { url = "'" + url + "'"; } + apiScriptBuilder.AppendFormat(" return axios.request<{0}>(", apiRetuanName); apiScriptBuilder.AppendLine("{"); apiScriptBuilder.AppendFormat(" method: '{0}',", action.Value.HttpMethod); @@ -131,53 +160,71 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe apiScriptBuilder.AppendFormat(" url: {0},", url); apiScriptBuilder.AppendLine(""); - if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + var inPathParams = action.Value.Parameters + .Where(p => TypeScriptModelGenerator.DataInParamSources.Contains(p.BindingSourceId)) + .DistinctBy(p => p.NameOnMethod); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + var inFormParams = action.Value.Parameters + .Where(p => TypeScriptModelGenerator.DataInFormSources.Contains(p.BindingSourceId)) + .DistinctBy(p => p.NameOnMethod); + + if (!isFormatUrl && inPathParams.Any()) { - if (!isFormatUrl && action.Value.ParametersOnMethod.Any()) + if (inPathParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" params: {0},", inPathParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else { apiScriptBuilder.AppendLine(" params: {"); - - foreach (var paramter in action.Value.ParametersOnMethod) + foreach (var paramter in inPathParams) { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendFormat(" {0}: {1}.{2},", paramter.Name, paramter.NameOnMethod, 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()) + if (inBodyParams.Any()) + { + if (inBodyParams.Count() == 1) { - apiScriptBuilder.AppendLine(" params: {"); - foreach (var paramter in inPathParams) + 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(" },"); } + } - if (inBodyParams.Any() && - inBodyParams.Count() == 1) + if (inFormParams.Any()) + { + if (inFormParams.Count() == 1) { - apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); + apiScriptBuilder.AppendFormat(" data: {0},", inFormParams.First().NameOnMethod); apiScriptBuilder.AppendLine(""); } else { apiScriptBuilder.AppendLine(" data: {"); - foreach (var paramter in inBodyParams) + foreach (var paramter in inFormParams) { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendFormat(" {0}: {1}.{2},", paramter.Name, paramter.NameOnMethod, paramter.Name); apiScriptBuilder.AppendLine(""); } - apiScriptBuilder.AppendLine(" },"); } + apiScriptBuilder.AppendLine(" headers: {"); + apiScriptBuilder.AppendLine(" 'Content-type': 'multipart/form-data'"); + apiScriptBuilder.AppendLine(" },"); } apiScriptBuilder.AppendLine(" });"); @@ -186,46 +233,4 @@ public class AxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransientDe 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/TypeScriptModelGenerator.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs index be7833787..cbd870cf3 100644 --- a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/LINGYUN/Abp/Cli/ServiceProxying/TypeScript/TypeScriptModelGenerator.cs @@ -6,14 +6,14 @@ using System.Linq; using System.Text; using Volo.Abp.DependencyInjection; using Volo.Abp.Http.Modeling; -using Volo.Abp.ObjectExtending; namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDependency { internal readonly static string[] AbpBaseTypes = new string[] { - typeof(ExtensibleObject).FullName, + "Volo.Abp.Content.IRemoteStreamContent", + "Volo.Abp.Content.RemoteStreamContent", "Volo.Abp.Application.Dtos.AuditedEntityDto", "Volo.Abp.Application.Dtos.AuditedEntityWithUserDto", "Volo.Abp.Application.Dtos.CreationAuditedEntityDto", @@ -33,12 +33,23 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep "Volo.Abp.Application.Dtos.PagedAndSortedResultRequestDto", "Volo.Abp.Application.Dtos.PagedResultDto", "Volo.Abp.Application.Dtos.PagedResultRequestDto", + "Volo.Abp.ObjectExtending.ExtensibleObject", }; internal readonly static string[] DataInParamMethods = new string[] { "GET", "DELETE" }; + internal readonly static string[] DataInParamSources = new string[] + { + "Path", + "ModelBinding" + }; + internal readonly static string[] DataInFormSources = new string[] + { + "Form", + "FormFile" + }; public ILogger Logger { protected get; set; } @@ -59,7 +70,8 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep { foreach (var paramter in action.Value.Parameters) { - if (appModel.Types.TryGetValue(paramter.Type, out var modelType)) + if (!AbpBaseTypes.Contains(paramter.TypeSimple) && + appModel.Types.TryGetValue(paramter.Type, out var modelType)) { var modelTypeName = paramter.Type[(paramter.Type.LastIndexOf('.') + 1)..]; @@ -125,7 +137,7 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep .Replace(">", ""); } - returnType = ReplaceTypeSimple(returnType); + returnType = returnType.ReplaceTypeSimple(); if (appModel.Types.TryGetValue(returnType, out var returnBaseType)) { @@ -190,7 +202,7 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep if (!model.BaseType.IsNullOrWhiteSpace()) { var baseType = ReplaceAbpBaseType(model.BaseType); - baseType = ReplaceTypeSimple(baseType); + baseType = baseType.ReplaceTypeSimple(); modelBuilder.AppendFormat("extends {0} ", baseType[(baseType.LastIndexOf('.') + 1)..]); } @@ -200,13 +212,13 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep { modelBuilder.AppendFormat(" {0}", model.Properties[index].Name.ToCamelCase()); var propCharacter = model.Properties[index].IsRequired ? ": " : "?: "; - var propTypeName = ReplaceTypeSimple(model.Properties[index].TypeSimple); + var propTypeName = model.Properties[index].TypeSimple.ReplaceTypeSimple(); if (propTypeName.LastIndexOf('.') >= 0) { propTypeName = propTypeName[(propTypeName.LastIndexOf('.') + 1)..]; } - modelBuilder.AppendFormat("{0}{1};", propCharacter, ReplaceTypeSimple(propTypeName)); + modelBuilder.AppendFormat("{0}{1};", propCharacter, propTypeName.ReplaceTypeSimple()); modelBuilder.AppendLine(""); } @@ -236,7 +248,7 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep { var types = new List(); - var propertityType = ReplaceTypeSimple(model.TypeSimple); + var propertityType = model.TypeSimple.ReplaceTypeSimple(); if (!AbpBaseTypes.Contains(propertityType) && apiModel.Types.TryGetValue(propertityType, out var baseType)) @@ -249,28 +261,6 @@ public class TypeScriptModelGenerator : ITypeScriptModelGenerator, ITransientDep return types; } - 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; - } - protected virtual string ReplaceAbpBaseType(string typeSimple) { var abpBaseType = AbpBaseTypes.FirstOrDefault(t => t.StartsWith(typeSimple)); 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 index d7fed2325..f8ec8e8bc 100644 --- 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 @@ -19,7 +19,7 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans { var apiScriptBuilder = new StringBuilder(); - apiScriptBuilder.AppendLine("import http from '../http'"); + apiScriptBuilder.AppendLine("import http from '../../http'"); var importModel = ""; @@ -33,7 +33,7 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans if (!importModel.Contains(modelTypeName)) { - importModel += modelTypeName + ", "; + importModel += modelTypeName + ","; } } } @@ -50,7 +50,7 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans .Replace(">", ""); } - returnType = ReplaceTypeSimple(returnType); + returnType = returnType.ReplaceTypeSimple(); returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; if (!importModel.Contains(returnType)) { @@ -67,6 +67,7 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans { var url = action.Value.Url; var isFormatUrl = false; + var formatUrlIndex = 0; apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); @@ -84,13 +85,36 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans } // 需要格式化url - if (url.Contains('{') && url.Contains(paramter.Name)) + var formatUrlPath = string.Concat("{", paramter.Name, "}"); + if (url.Contains(formatUrlPath)) { - var formatUrl = MiddleString(url, "{", "}"); - url = url.Replace(formatUrl, ""); - url = "'" + url + "'" + " + " + paramter.Name; + formatUrlIndex = url.IndexOf(formatUrlPath) + formatUrlPath.Length; + // 'api/platform/packages/{id}/blob/{Name}' => `api/platform/packages/${id}/blob/{input.name}` + url = url.Replace(formatUrlPath, $"${formatUrlPath}"); isFormatUrl = true; } + + if (formatUrlIndex >= 0 && formatUrlIndex + formatUrlPath.Length <= url.Length) + { + var formatUrl = url[(formatUrlIndex + formatUrlPath.Length)..].MiddleString("{", "}"); + if (!formatUrl.IsNullOrWhiteSpace()) + { + if (appModel.Types.TryGetValue(paramter.Type, out var paramType)) + { + var formatParamInUrl = paramType.Properties + .FirstOrDefault(p => formatUrl.Contains(p.Name)); + + if (formatParamInUrl != null) + { + // 'api/platform/packages/xxx/blob/{Name}' => `api/platform/packages/xxx/blob/${input.name}` + url = url.Replace( + formatUrl, + string.Concat("${", paramter.Name, ".", formatParamInUrl.Name.ToCamelCase(), "}")); + isFormatUrl = true; + } + } + } + } } var apiRetuanName = action.Value.ReturnValue.TypeSimple; @@ -119,41 +143,40 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans apiRetuanName = apiRetuanName.ToLower(); } - apiScriptBuilder.AppendFormat("): Promise<{0}> ", apiRetuanName); + apiScriptBuilder.AppendFormat("): Promise<{0}> ", apiRetuanName.ReplaceTypeSimple()); apiScriptBuilder.AppendLine("=> {"); - if (!url.StartsWith("'") && !url.EndsWith("'")) + if (isFormatUrl && !url.StartsWith("`") && !url.EndsWith("`")) + { + url = "`" + url + "`"; + } + else { 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)) + var inPathParams = action.Value.Parameters + .Where(p => TypeScriptModelGenerator.DataInParamSources.Contains(p.BindingSourceId)) + .DistinctBy(p => p.NameOnMethod); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + var inFormParams = action.Value.Parameters.Where(p => + TypeScriptModelGenerator.DataInFormSources.Contains(p.BindingSourceId)); + + if (!isFormatUrl && inPathParams.Any()) { - if (!isFormatUrl && action.Value.ParametersOnMethod.Any()) + if (inPathParams.Count() == 1) { - apiScriptBuilder.AppendLine(" params: {"); - - foreach (var paramter in action.Value.ParametersOnMethod) - { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); - apiScriptBuilder.AppendLine(""); - } - - apiScriptBuilder.AppendLine(" },"); + apiScriptBuilder.AppendFormat(" params: {0},", inPathParams.First().NameOnMethod); + 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()) + else { apiScriptBuilder.AppendLine(" params: {"); foreach (var paramter in inPathParams) @@ -163,9 +186,11 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans } apiScriptBuilder.AppendLine(" },"); } + } - if (inBodyParams.Any() && - inBodyParams.Count() == 1) + if (inBodyParams.Any()) + { + if (inBodyParams.Count() == 1) { apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); apiScriptBuilder.AppendLine(""); @@ -182,52 +207,19 @@ public class UniAppAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans } } + if (inFormParams.Any()) + { + apiScriptBuilder.AppendFormat(" data: {0},", inFormParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + apiScriptBuilder.AppendLine(" headers: {"); + apiScriptBuilder.AppendLine(" 'Content-type': 'multipart/form-data'"); + 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 index db20d7f1c..695ab61c9 100644 --- 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 @@ -33,7 +33,7 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie if (!importModel.Contains(modelTypeName)) { - importModel += modelTypeName + ", "; + importModel += modelTypeName + ","; } } } @@ -50,7 +50,7 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie .Replace(">", ""); } - returnType = ReplaceTypeSimple(returnType); + returnType = returnType.ReplaceTypeSimple(); returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; if (!importModel.Contains(returnType)) { @@ -66,6 +66,8 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie foreach (var action in actionModel.Actions) { var url = action.Value.Url; + var isFormatUrl = false; + var formatUrlIndex = 0; apiScriptBuilder.AppendFormat("export const {0} = (", action.Value.UniqueName); @@ -83,11 +85,35 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie } // 需要格式化url - if (url.Contains('{') && url.Contains(paramter.Name)) + var formatUrlPath = string.Concat("{", paramter.Name, "}"); + if (url.Contains(formatUrlPath)) { - var formatUrl = MiddleString(url, "{", "}"); - url = url.Replace(formatUrl, ""); - url = "'" + url + "'" + " + " + paramter.Name; + formatUrlIndex = url.IndexOf(formatUrlPath) + formatUrlPath.Length; + // 'api/platform/packages/{id}/blob/{Name}' => `api/platform/packages/${id}/blob/{input.name}` + url = url.Replace(formatUrlPath, $"${formatUrlPath}"); + isFormatUrl = true; + } + + if (formatUrlIndex >= 0 && formatUrlIndex + formatUrlPath.Length <= url.Length) + { + var formatUrl = url[(formatUrlIndex + formatUrlPath.Length)..].MiddleString("{", "}"); + if (!formatUrl.IsNullOrWhiteSpace()) + { + if (appModel.Types.TryGetValue(paramter.Type, out var paramType)) + { + var formatParamInUrl = paramType.Properties + .FirstOrDefault(p => formatUrl.Contains(p.Name)); + + if (formatParamInUrl != null) + { + // 'api/platform/packages/xxx/blob/{Name}' => `api/platform/packages/xxx/blob/${input.name}` + url = url.Replace( + formatUrl, + string.Concat("${", paramter.Name, ".", formatParamInUrl.Name.ToCamelCase(), "}")); + isFormatUrl = true; + } + } + } } } @@ -119,10 +145,16 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie apiRetuanName = apiRetuanName.ToLower(); } - if (!url.StartsWith("'") && !url.EndsWith("'")) + if (isFormatUrl && !url.StartsWith("`") && !url.EndsWith("`")) + { + url = "`" + url + "`"; + } + else { url = "'" + url + "'"; } + + apiScriptBuilder.AppendFormat(" return defHttp.request<{0}>(", apiRetuanName); apiScriptBuilder.AppendLine("{"); apiScriptBuilder.AppendFormat(" method: '{0}',", action.Value.HttpMethod); @@ -130,39 +162,36 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie apiScriptBuilder.AppendFormat(" url: {0},", url); apiScriptBuilder.AppendLine(""); - if (TypeScriptModelGenerator.DataInParamMethods.Contains(action.Value.HttpMethod)) + var inPathParams = action.Value.Parameters + .Where(p => TypeScriptModelGenerator.DataInParamSources.Contains(p.BindingSourceId)) + .DistinctBy(p => p.NameOnMethod); + var inBodyParams = action.Value.Parameters.Where(p => p.BindingSourceId == "Body"); + var inFormParams = action.Value.Parameters + .Where(p => TypeScriptModelGenerator.DataInFormSources.Contains(p.BindingSourceId)) + .DistinctBy(p => p.NameOnMethod); + + if (!isFormatUrl && inPathParams.Any()) { - if (action.Value.ParametersOnMethod.Any()) + if (inPathParams.Count() == 1) { - apiScriptBuilder.AppendLine(" params: {"); - - foreach (var paramter in action.Value.ParametersOnMethod) - { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); - apiScriptBuilder.AppendLine(""); - } - - apiScriptBuilder.AppendLine(" },"); + apiScriptBuilder.AppendFormat(" params: {0},", inPathParams.First().NameOnMethod); + 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()) + else { apiScriptBuilder.AppendLine(" params: {"); foreach (var paramter in inPathParams) { - apiScriptBuilder.AppendFormat(" {0}: {1},", paramter.Name, paramter.Name); + apiScriptBuilder.AppendFormat(" {0}: {1}.{2},", paramter.Name, paramter.NameOnMethod, paramter.Name); apiScriptBuilder.AppendLine(""); } apiScriptBuilder.AppendLine(" },"); } + } - if (inBodyParams.Any() && - inBodyParams.Count() == 1) + if (inBodyParams.Any()) + { + if (inBodyParams.Count() == 1) { apiScriptBuilder.AppendFormat(" data: {0},", inBodyParams.First().NameOnMethod); apiScriptBuilder.AppendLine(""); @@ -179,15 +208,25 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie } } - apiScriptBuilder.Append(" }"); - - if (action.Value.AllowAnonymous == true) + if (inFormParams.Any()) { - apiScriptBuilder.AppendLine(",{"); - // 匿名方法无需token - apiScriptBuilder.AppendLine(" withToken: false,"); - - apiScriptBuilder.Append(" }"); + if (inFormParams.Count() == 1) + { + apiScriptBuilder.AppendFormat(" data: {0},", inFormParams.First().NameOnMethod); + apiScriptBuilder.AppendLine(""); + } + else + { + apiScriptBuilder.AppendLine(" data: {"); + foreach (var paramter in inFormParams) + { + apiScriptBuilder.AppendFormat(" {0}: {1}.{2},", paramter.Name, paramter.NameOnMethod, paramter.Name); + apiScriptBuilder.AppendLine(""); + } + } + apiScriptBuilder.AppendLine(" headers: {"); + apiScriptBuilder.AppendLine(" 'Content-type': 'multipart/form-data'"); + apiScriptBuilder.AppendLine(" },"); } apiScriptBuilder.AppendLine(");"); @@ -196,46 +235,4 @@ public class VbenAxiosHttpApiScriptGenerator : IHttpApiScriptGenerator, ITransie 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 index 89cba0f78..86df36956 100644 --- 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 @@ -32,7 +32,7 @@ public class VbenDynamicHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans if (!importModel.Contains(modelTypeName)) { - importModel += modelTypeName + ", "; + importModel += modelTypeName + ","; } } } @@ -49,7 +49,7 @@ public class VbenDynamicHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans .Replace(">", ""); } - returnType = ReplaceTypeSimple(returnType); + returnType = returnType.ReplaceTypeSimple(); returnType = returnType[(returnType.LastIndexOf('.') + 1)..]; if (!importModel.Contains(returnType)) { @@ -87,6 +87,7 @@ public class VbenDynamicHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans apiScriptBuilder.AppendLine(") => {"); + var apiRequestName = "request"; var apiRetuanName = action.Value.ReturnValue.TypeSimple; @@ -185,26 +186,4 @@ public class VbenDynamicHttpApiScriptGenerator : IHttpApiScriptGenerator, ITrans 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 e6b0acc7c..a58b5a7c4 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 -asp uni-app-axios -u http://127.0.0.1:30010 -m abp -o D:\\Projects\\Development\\type-script" + "commandLineArgs": "generate-proxy -t ts -asp uni-app-axios -u http://127.0.0.1:30025 -m Platform -o D:\\Projects\\Development\\type-script" } } } \ No newline at end of file diff --git a/aspnet-core/modules/cli/LINGYUN.Abp.Cli/System/StringExtensions.cs b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/System/StringExtensions.cs new file mode 100644 index 000000000..bf4ba9b75 --- /dev/null +++ b/aspnet-core/modules/cli/LINGYUN.Abp.Cli/System/StringExtensions.cs @@ -0,0 +1,47 @@ +namespace System; + +internal static class StringExtensions +{ + public static string ReplaceTypeSimple(this string typeSimple) + { + typeSimple = typeSimple + .Replace("?", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("", "") + .Replace("IRemoteStreamContent", "Blob") + .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(this 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; + } +}