CodeGenerator/Templates/Request.cshtml
owenchen 61714e0a91 ow
2026-05-12 10:43:31 +08:00

169 lines
11 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@{
ModelData modelData = Model;
NamespaceObject namespaceObject = modelData.NamespaceObject;
SysTableSchemaViewEntity[] sysTableSchemas = modelData.sysTableSchemas;
ClassInfo classInfo= modelData.ClassInfos.FirstOrDefault();
//ClassInfo classInfo= modelData.ClassInfos.Where(x=>x.ClassName=="SecurityUserService").FirstOrDefault();
}
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Request;
using CloudBuilder.Core.Service;
using Microsoft.Extensions.DependencyInjection;
@{
if(classInfo.Usings!=null && classInfo.Usings.Length>0)
{
for (var i = 0; i < classInfo.Usings.Length; i++)
{
if(classInfo.Usings[i]=="CloudBuilder.Core.Service") continue;
<text>using @classInfo.Usings[i];
</text>
}
}
}
namespace @namespaceObject.Namespace
{
public class @{@classInfo.ClassName.Replace("Service", "")}Request : IRequest
{
private readonly IServiceRequest serviceRequest;
public @{@classInfo.ClassName.Replace("Service", "")}Request(IServiceRequest serviceRequest)
{
this.serviceRequest = serviceRequest;
}
@{
if(classInfo.ClassMethodInfos!=null && classInfo.ClassMethodInfos.Length>0 && classInfo.ClassMethodInfos[0].Parameters!=null)
{
for (var j= 0; j< classInfo.ClassMethodInfos.Length; j++)
{
string methodReturnName="void ";
if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void")
{
methodReturnName=" "+classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool")+" ";
}
//没有参数的情况=======================================
if(classInfo.ClassMethodInfos[j].Parameters.Length==0)
{
//文本开始=================================================================================================================
<text> public @{ @methodReturnName @classInfo.ClassMethodInfos[j].MethodName }()
{
@{
//[HttpGet]一定要有返回值,否则使用[HttpPost]
if(classInfo.ClassMethodInfos[j].HttpName=="[HttpGet]"){
<text>return Task.Run(() => serviceRequest.GetAsync<@{@classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool")}>("/@{@classInfo.ClassName.Replace("Service", "")}/@{@classInfo.ClassMethodInfos[j].MethodName}")).GetAwaiter().GetResult();</text>
}
else{
//[HttpPost]可以有无返回值
if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void"){<text>return </text>}<text> Task.Run(() => serviceRequest.@{if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void"){<text>PostAsJsonAsync<@{@classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool")}> </text>}else{<text>PostAsync</text>}}("/@{@classInfo.ClassName.Replace("Service", "")}/@{@classInfo.ClassMethodInfos[j].MethodName}")).GetAwaiter().GetResult();</text>
}
}
}
</text>
//文本结束=================================================================================================================
}
//只有1个参数的情况============================================
if(classInfo.ClassMethodInfos[j].Parameters.Length>0)
{
//超过1个参数的情况如果是get,不能用组合对象如果是post要重新组合一个类========================================
if(classInfo.ClassMethodInfos[j].Parameters.Length>1 && classInfo.ClassMethodInfos[j].HttpName!="[HttpGet]")
{
<text>
public class @{@classInfo.ClassMethodInfos[j].MethodName}CompositionData@{@classInfo.ClassMethodInfos[j].Parameters.Length}
{
</text>
for (var ik = 0; ik < classInfo.ClassMethodInfos[j].Parameters.Length; ik++){
string parameterTypeName=classInfo.ClassMethodInfos[j].Parameters[ik].ParameterTypeName;
if(classInfo.ClassMethodInfos[j].Parameters[ik].IsValueType)
{
if(classInfo.ClassMethodInfos[j].Parameters[ik].ParameterTypeName=="String")
parameterTypeName="string";
if(classInfo.ClassMethodInfos[j].Parameters[ik].ParameterTypeName=="Boolean")
parameterTypeName="bool";
}
<text> public @parameterTypeName @classInfo.ClassMethodInfos[j].Parameters[ik].ParameterName { get; set; }
</text>
}
<text>
}
</text>
}
string parametersName=@classInfo.ClassMethodInfos[j].Parameters[0].ParameterTypeName.Replace("String", "string").Replace("Boolean", "bool")+" "+@classInfo.ClassMethodInfos[j].Parameters[0].ParameterName;
string content=string.Empty;
//如果是[HttpGet],一定是值参数,统一使用组合参数
if(classInfo.ClassMethodInfos[j].Parameters.Length>0 && classInfo.ClassMethodInfos[j].HttpName=="[HttpGet]")
{
content += "var compositionData = new Dictionary<string, string>();\t\r";
for (var iac = 0; iac < classInfo.ClassMethodInfos[j].Parameters.Length; iac++)
{
content += "compositionData.Add(\"" + @classInfo.ClassMethodInfos[j].Parameters[iac].ParameterName + "\", " + @classInfo.ClassMethodInfos[j].Parameters[iac].ParameterName + ".ToString());\t\r";
}
content += "return Task.Run(() => serviceRequest.GetWithQueryAsync<"+@classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool")+">(\"/"+@classInfo.ClassName.Replace("Service", "")+"/"+@classInfo.ClassMethodInfos[j].MethodName+"\", compositionData)).GetAwaiter().GetResult();";
}
else if(classInfo.ClassMethodInfos[j].Parameters.Length>0 && classInfo.ClassMethodInfos[j].HttpName!="[HttpGet]")
{
string returnValue=string.Empty;
if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void")returnValue="return";
if(classInfo.ClassMethodInfos[j].Parameters.Length>1)
{
string CompositionData=classInfo.ClassMethodInfos[j].MethodName+"CompositionData"+classInfo.ClassMethodInfos[j].Parameters.Length;
content += "var compositionData = new "+CompositionData+"{\t\r";
for (var ikp = 0; ikp < classInfo.ClassMethodInfos[j].Parameters.Length; ikp++)
{
if(ikp>0) content += ",\t\r";
content += @classInfo.ClassMethodInfos[j].Parameters[ikp].ParameterName+" = "+@classInfo.ClassMethodInfos[j].Parameters[ikp].ParameterName;
}
content += " };\t\r";
string rv=classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool").Replace("Void", "dynamic");
content += returnValue+" Task.Run(() => serviceRequest.PostAsJsonAsync<"+CompositionData+", "+rv+">(\"/"+@classInfo.ClassName.Replace("Service", "")+"/"+@classInfo.ClassMethodInfos[j].MethodName+"\", compositionData)).GetAwaiter().GetResult();";
}
else
{
string post="PostAsync";
if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void")post="PostAsJsonAsync"+string.Format("<{1},{0}>",classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool"),@classInfo.ClassMethodInfos[j].Parameters[0].ParameterTypeName.Replace("String", "string").Replace("Boolean", "bool"));
if(!classInfo.ClassMethodInfos[j].Parameters[0].IsValueType && classInfo.ClassMethodInfos[j].MethodReturnName=="Void")post="PostAsJsonAsync"+string.Format("<{0},dynamic>",@classInfo.ClassMethodInfos[j].Parameters[0].ParameterTypeName.Replace("String", "string").Replace("Boolean", "bool"));
if(classInfo.ClassMethodInfos[j].Parameters[0].IsValueType)
{
if(classInfo.ClassMethodInfos[j].MethodReturnName!="Void"){post="PostAsJsonAsync"+string.Format("<{0}>",classInfo.ClassMethodInfos[j].MethodReturnName.Replace("String", "string").Replace("Boolean", "bool"));}
content += returnValue+" Task.Run(() => serviceRequest."+post+"(\"/"+@classInfo.ClassName.Replace("Service", "")+"/"+@classInfo.ClassMethodInfos[j].MethodName+"?"+@classInfo.ClassMethodInfos[j].Parameters[0].ParameterName+"=\" + "+@classInfo.ClassMethodInfos[j].Parameters[0].ParameterName+".ToString())).GetAwaiter().GetResult();";
}
else{
content += returnValue+" Task.Run(() => serviceRequest."+post+"(\"/"+@classInfo.ClassName.Replace("Service", "")+"/"+@classInfo.ClassMethodInfos[j].MethodName+"\" , "+@classInfo.ClassMethodInfos[j].Parameters[0].ParameterName+")).GetAwaiter().GetResult();";
}
}
}
parametersName=string.Empty;
for (var ikb = 0; ikb < classInfo.ClassMethodInfos[j].Parameters.Length; ikb++){
if(ikb>0)parametersName+=", ";
parametersName+=@classInfo.ClassMethodInfos[j].Parameters[ikb].ParameterTypeName.Replace("String", "string").Replace("Boolean", "bool")+" "+@classInfo.ClassMethodInfos[j].Parameters[ikb].ParameterName;
}
//文本开始================================================
<text> public @{@methodReturnName @classInfo.ClassMethodInfos[j].MethodName}(@{@parametersName})</text><text>
{
@{@content.ToString()}
}
</text>
//文本结束===================================================
}
}
}
//下面}为代码段结束=================================================================================================================
}
}
}