From c49c14c7719db53f6e801a1d41ad9a1ca8ea7749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=93=E6=9C=88?= <353374337@qq.com> Date: Fri, 19 Oct 2018 00:36:32 +0800 Subject: [PATCH] =?UTF-8?q?Ajax/JavaScript=E8=A7=A3=E6=9E=90=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeShowWeb/CodeShow.aspx.cs | 18 +--- CodeShowWeb/Index.aspx | 2 +- CodeShowWeb/Index.aspx.cs | 31 +++--- CoderEngine/AjaxCodeWrite.cs | 182 +++++++++++++++++++++++---------- CoderEngine/CoderEngine.csproj | 2 +- CoderEngine/JavaCodeWrite.cs | 3 - UseEngineConsolo/Program.cs | 26 +++-- 7 files changed, 165 insertions(+), 99 deletions(-) diff --git a/CodeShowWeb/CodeShow.aspx.cs b/CodeShowWeb/CodeShow.aspx.cs index 39b0bb9..d242060 100644 --- a/CodeShowWeb/CodeShow.aspx.cs +++ b/CodeShowWeb/CodeShow.aspx.cs @@ -21,11 +21,11 @@ namespace CodeShowWeb string CodeLang = GetLang(); if (CodeLang == "Java") { - return GetJavaCode(); + return new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); } else if (CodeLang == "swift") { - return GetSwiftCode(); + return new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); } return "代码语言尚未支持"; @@ -56,18 +56,6 @@ namespace CodeShowWeb return "代码语言尚未支持"; } - - - public string GetJavaCode() - { - string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); - return JavaCode; - } - - public string GetSwiftCode() - { - string SwiftCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); - return SwiftCode; - } + } } \ No newline at end of file diff --git a/CodeShowWeb/Index.aspx b/CodeShowWeb/Index.aspx index 373f93c..f1e4148 100644 --- a/CodeShowWeb/Index.aspx +++ b/CodeShowWeb/Index.aspx @@ -14,7 +14,7 @@

服务端参数跨语言代码转换引擎 Ver 0.1.0 -- 皓月

是一个C#服务端API参数实体类、转换成于各类语言客户端解析参数代码,减少客户端开发人员重复工作量

请选择目前支持转换到的目标语言:

-

Java/AndroidSwift/iOSAjax/JavascriptPHPGolang、...

+

Java/AndroidSwift/iOSAjax/JavascriptPHPGolang、...

选择你喜欢的代码配色方案

diff --git a/CodeShowWeb/Index.aspx.cs b/CodeShowWeb/Index.aspx.cs index a643b1e..01c80ba 100644 --- a/CodeShowWeb/Index.aspx.cs +++ b/CodeShowWeb/Index.aspx.cs @@ -20,12 +20,17 @@ namespace CodeShowWeb string CodeLang = GetLang(); if (CodeLang == "Java") { - return GetJavaCode(); + return new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); } - else if (CodeLang == "swift") + else if (CodeLang == "Swift") { - return GetSwiftCode(); + return new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); } + else if (CodeLang == "JavaScript") + { + return new AjaxCodeWrite().ModelToAjaxCode("CoderEngine", "A"); + } + return "代码语言尚未支持"; } @@ -48,25 +53,17 @@ namespace CodeShowWeb { return "androidstudio.css"; } - else if (CodeLang == "swift") + else if (CodeLang == "Swift") { return "xcode.css"; } + else if (CodeLang == "JavaScript") + { + return "codepen-embed.css"; + } return "代码语言尚未支持"; } - - - public string GetJavaCode() - { - string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); - return JavaCode; - } - - public string GetSwiftCode() - { - string SwiftCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); - return SwiftCode; - } + } } \ No newline at end of file diff --git a/CoderEngine/AjaxCodeWrite.cs b/CoderEngine/AjaxCodeWrite.cs index 8a70d60..bd6c5a9 100644 --- a/CoderEngine/AjaxCodeWrite.cs +++ b/CoderEngine/AjaxCodeWrite.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; namespace CoderEngine @@ -24,26 +25,54 @@ namespace CoderEngine CodeStr += CodeLine + "\n"; } - public string ModelToJavaCode(string _AssemblyName, string _TypeName) + /// + /// 倍数缩进 + /// + /// + public string TabstrForNumber(int Number) + { + string str = ""; + + if (Number < 0) + Number = 0; + for (int i = 0; i < Number; i++) + { + str += tabstr; + } + return str; + } + + public string ModelToAjaxCode(string _AssemblyName, string _TypeName) { TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName); - ModelToJavaCodeWriter(tm); + ModelToAjaxCodeWriter(tm); return CodeStr; } - public void ModelToJavaCodeWriter(TypeMode TopMode) + + + public void ModelToAjaxCodeWriter(TypeMode TopMode) { + CoderAddLine(""); + CoderAddLine("//生成Ajax请求后解析返回值的js代码"); + CoderAddLine("success: function(data) {//请求完成"); + CoderAddLine(tabstr + "var ob = JSON.parse(data.Value);//序列化Value字符串为json对象"); //如果有代码嵌套的下级类 在本类外 写入新的类 if (TopMode.TypeList != null && TopMode.TypeList.Count > 0) { CoderAddLine(tabstr); - JsCodeDownRecursion(TopMode); + AjaxCodeDownRecursion(TopMode,"",1); } + CoderAddLine("}"); } - - public void JsCodeDownRecursion(TypeMode TopMode) + public void AjaxCodeDownRecursion(TypeMode TopMode,string ParentCode, int TabNumber) { + + if (ParentCode == null || ParentCode == "") + ParentCode = "ob"; + + string ClassName = ""; //如果自己就是集合类型 则取类型下名字 if (TopMode.MemberType.Name == "List`1") @@ -52,81 +81,130 @@ namespace CoderEngine //其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型” var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); - ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".")+1); - } - else - ClassName = TopMode.MemberType.Name; + ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1); - CoderAddLine("public class " + ClassName + " {"); - foreach (var tl in TopMode.TypeList) - { - CoderAddLine(tabstr + "@SerializedName(value = \"" + tl.Name + "\")"); - CoderAddLine(tabstr + "public " + CSharpToJavaScriptTypeName(tl.MemberType) + " " + tl.Name + ";"); - } - - CoderAddLine("}"); - - - foreach (var tl in TopMode.TypeList) - { - if(tl.MemberType.Name == "List`1") + CoderAddLine(TabstrForNumber(TabNumber-1) + "//循环取内嵌集合"+ ParentCode+ "数据"); + CoderAddLine(TabstrForNumber(TabNumber-1) + "for(i" + TabNumber + " = 0;i" + TabNumber + " < " + ParentCode+ ".lenght;i" + TabNumber + "++){"); + foreach (var tl in TopMode.TypeList) { - if (tl.TypeList != null && tl.TypeList.Count > 0) + if (tl.MemberType.Name == "List`1") { - CoderAddLine(""); - JsCodeDownRecursion(tl); + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType)); + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量"); + + if (tl.TypeList != null && tl.TypeList.Count > 0) + { + CoderAddLine(""); + + + ParentCode = ParentCode + "[i" + TabNumber + "]"; + string ParentCodeStr = ""; + if (ParentCode != null) + ParentCodeStr = ParentCode + "." + tl.Name; + else + ParentCodeStr = "ob." + tl.Name; + AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1); + } + } + else + { + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i" + TabNumber + "]." + tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType)); } } - - } - - } - /// - /// 转换成Java代码 - /// - /// - public string CSharpToJavaScriptTypeName(Type myType) - { - var oneinfo = CSharpToJavaScriptTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault(); - if (oneinfo.Value != null) - { - return oneinfo.Value; + CoderAddLine(TabstrForNumber(TabNumber-1) + "}"); } else { - if(myType.Name == "List`1") + ClassName = TopMode.MemberType.Name; + + foreach (var tl in TopMode.TypeList) + { + if (tl.MemberType.Name == "List`1") + { + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType)); + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量"); + + if (tl.TypeList != null && tl.TypeList.Count > 0) + { + CoderAddLine(""); + + string ParentCodeStr = ""; + if (ParentCode != null) + ParentCodeStr = ParentCode + "." + tl.Name; + else + ParentCodeStr = "ob." + tl.Name; + + AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1); + } + } + else + { + CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "."+ tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType)); + } + } + + } + + + + + } + + + /// + /// 转换成JS代码描述 + /// + /// + public string CSharpToJavaTypeName(Type myType) + { + var oneinfo = CSharpToJSTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault(); + if (oneinfo.Value != null) + { + return "类型为 " + oneinfo.Value; + } + else + { + if (myType.Name == "List`1") + { + return ""; + } + else if (myType.Name == "Nullable`1") { var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); - return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.')+1) + ">"; + //string var = SonParamName.Substring(SonParamName.LastIndexOf('.') + 1); + string SonParamFromAssemblyName = SonParamName.Substring(0, SonParamName.IndexOf('.')); + + + //貌似反射加载System是不行的?是吗 + //Type PerentType = System.GetType(SonParamName); + + + return ""; } else - return "string"; + return "类型为 " + "string"; } } - + /// - /// C#和Java代码对照表 + /// C#和JS代码对照表 /// - public System.Collections.Generic.Dictionary CSharpToJavaScriptTypeItem = + public System.Collections.Generic.Dictionary CSharpToJSTypeItem = new Dictionary() { { "int","int"}, { "Int16","int"}, { "Int32","int"}, { "Int64","int"}, - { "int?","int"}, { "long","long"}, - { "long?","long"}, { "float","float"}, - { "float?","float"}, { "double","double"}, - { "double?","double"}, { "boolean","boolean"}, { "string","string"}, - { "DateTime","DateTime"}, - { "DateTime?","DateTime"}, + { "DateTime","时间格式形如 1999-99-99 10:10:10"}, }; + } } diff --git a/CoderEngine/CoderEngine.csproj b/CoderEngine/CoderEngine.csproj index 013cb19..4f21648 100644 --- a/CoderEngine/CoderEngine.csproj +++ b/CoderEngine/CoderEngine.csproj @@ -39,10 +39,10 @@ - + diff --git a/CoderEngine/JavaCodeWrite.cs b/CoderEngine/JavaCodeWrite.cs index ed10bbe..70b32e6 100644 --- a/CoderEngine/JavaCodeWrite.cs +++ b/CoderEngine/JavaCodeWrite.cs @@ -97,11 +97,8 @@ namespace CoderEngine CoderAddLine(tabstr + "}"); CoderAddLine("}"); - - - foreach (var tl in TopMode.TypeList) { if(tl.MemberType.Name == "List`1") diff --git a/UseEngineConsolo/Program.cs b/UseEngineConsolo/Program.cs index 9776781..9d5e8dd 100644 --- a/UseEngineConsolo/Program.cs +++ b/UseEngineConsolo/Program.cs @@ -19,18 +19,24 @@ namespace UseEngineConsolo Console.WriteLine(); Console.WriteLine(); - string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); + //string JavaCode = new JavaCodeWrite().ModelToJavaCode("CoderEngine", "A"); + //Console.WriteLine(); + //Console.WriteLine("C#实体类动态转Java代码结果:"); + //Console.WriteLine(); + //Console.WriteLine(JavaCode); + //Console.WriteLine(); + //Console.WriteLine(); + //string CSCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); + //Console.WriteLine(); + //Console.WriteLine("C#实体类动态转Ajax客户端代码结果:"); + //Console.WriteLine(); + //Console.WriteLine(CSCode); + //Console.ReadLine(); + string AjaxCode = new AjaxCodeWrite().ModelToAjaxCode("CoderEngine", "A"); Console.WriteLine(); - Console.WriteLine("C#实体类动态转Java代码结果:"); + Console.WriteLine("C#实体类动态转Ajax客户端代码结果:"); Console.WriteLine(); - Console.WriteLine(JavaCode); - Console.WriteLine(); - Console.WriteLine(); - string CSCode = new SwiftCodeWrite().ModelToSwiftCode("CoderEngine", "A"); - Console.WriteLine(); - Console.WriteLine("Swift实体类动态转C#客户端代码结果:"); - Console.WriteLine(); - Console.WriteLine(CSCode); + Console.WriteLine(AjaxCode); Console.ReadLine(); } }