13 changed files with 1006 additions and 188 deletions
@ -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("<System.String>", "<string>") |
|||
.Replace("<System.Guid>", "<string>") |
|||
.Replace("<System.Int32>", "<number>") |
|||
.Replace("<System.Int64>", "<number>") |
|||
.Replace("{string:string}", "Dictionary<string, string>") |
|||
.Replace("{number:string}", "Dictionary<number, string>") |
|||
.Replace("{string:number}", "Dictionary<string, number>") |
|||
.Replace("{string:object}", "Dictionary<string, any>"); |
|||
|
|||
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; |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using Volo.Abp.Http.Modeling; |
|||
|
|||
namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; |
|||
|
|||
public interface ITypeScriptModelGenerator |
|||
{ |
|||
string CreateScript( |
|||
ApplicationApiDescriptionModel appModel, |
|||
ControllerApiDescriptionModel actionModel); |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace LINGYUN.Abp.Cli.ServiceProxying.TypeScript; |
|||
|
|||
public class TypeScriptServiceProxyOptions |
|||
{ |
|||
public IDictionary<string, IHttpApiScriptGenerator> ScriptGenerators { get; } |
|||
public TypeScriptServiceProxyOptions() |
|||
{ |
|||
ScriptGenerators = new Dictionary<string, IHttpApiScriptGenerator>(); |
|||
} |
|||
} |
|||
@ -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("<System.String>", "<string>") |
|||
.Replace("<System.Guid>", "<string>") |
|||
.Replace("<System.Int32>", "<number>") |
|||
.Replace("<System.Int64>", "<number>") |
|||
.Replace("{string:string}", "Dictionary<string, string>") |
|||
.Replace("{number:string}", "Dictionary<number, string>") |
|||
.Replace("{string:number}", "Dictionary<string, number>") |
|||
.Replace("{string:object}", "Dictionary<string, any>"); |
|||
|
|||
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; |
|||
} |
|||
} |
|||
@ -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("<System.String>", "<string>") |
|||
.Replace("<System.Guid>", "<string>") |
|||
.Replace("<System.Int32>", "<number>") |
|||
.Replace("<System.Int64>", "<number>") |
|||
.Replace("{string:string}", "Dictionary<string, string>") |
|||
.Replace("{number:string}", "Dictionary<number, string>") |
|||
.Replace("{string:number}", "Dictionary<string, number>") |
|||
.Replace("{string:object}", "Dictionary<string, any>"); |
|||
|
|||
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; |
|||
} |
|||
} |
|||
@ -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("<System.String>", "<string>") |
|||
.Replace("<System.Guid>", "<string>") |
|||
.Replace("<System.Int32>", "<number>") |
|||
.Replace("<System.Int64>", "<number>") |
|||
.Replace("{string:string}", "Dictionary<string, string>") |
|||
.Replace("{number:string}", "Dictionary<number, string>") |
|||
.Replace("{string:number}", "Dictionary<string, number>") |
|||
.Replace("{string:object}", "Dictionary<string, any>"); |
|||
|
|||
if (typeSimple.StartsWith("[") && typeSimple.EndsWith("]")) |
|||
{ |
|||
typeSimple = typeSimple.ReplaceFirst("[", "").RemovePostFix("]", ""); |
|||
typeSimple = typeSimple.Replace(typeSimple, $"{typeSimple}[]"); |
|||
} |
|||
|
|||
return typeSimple; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue