Browse Source

API调试生成。

CrazyIter_Bin 3 years ago
parent
commit
1b870a28dd

+ 5 - 3
TEAMModelAPI/Startup.cs

@@ -99,7 +99,7 @@ namespace TEAMModelAPI
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
-        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,AzureStorageFactory azureStorage)
+        public async void Configure(IApplicationBuilder app, IWebHostEnvironment env,AzureStorageFactory azureStorage)
         {
             if (env.IsDevelopment())
             {
@@ -119,8 +119,7 @@ namespace TEAMModelAPI
 #if DEBUG
             //在开发模式时,自检 [ApiToken(Auth = "1")] 有重复的接口 https://teammodelos.table.core.chinacloudapi.cn/IESOpenApi
             List<ApiTokenAttribute> auths = new List<ApiTokenAttribute>();
-            List<Attribute> attributes = ReflectorExtensions.GetMethodCustomAttribute<ApiTokenAttribute>(new string[] { "TEAMModelAPI" });
-            List<OpenApi> openApis = new List<OpenApi>();
+            (List < OpenApi > openApis ,List<Attribute> attributes  ) = ReflectorExtensions.GetMethodCustomAttribute<ApiTokenAttribute, HttpPostAttribute>(new string[] { "TEAMModelAPI" });
             attributes.ForEach(x => {
                 ApiTokenAttribute attribute = (ApiTokenAttribute)x;
                 openApis.Add(new OpenApi { 
@@ -143,6 +142,9 @@ namespace TEAMModelAPI
             });
 
             var table =  azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
+            foreach (var item in openApis) {
+                await table.SaveOrUpdate<OpenApi>(item);
+            }
 #endif
         }
     }

TEAMModelAPI/ApiTokenAttribute.cs → TEAMModelOS.SDK/Context/Attributes/Filter/ApiTokenAttribute.cs


+ 30 - 11
TEAMModelOS.SDK/Helper/Common/ReflectorExtensions/ReflectorExtensions.cs

@@ -1,12 +1,15 @@
 using AspectCore.Extensions.Reflection;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Reflection.Metadata;
+using TEAMModelOS.Filter;
 using TEAMModelOS.SDK.Context.Attributes.Azure;
+using TEAMModelOS.SDK.Models;
 
 namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
 {
@@ -65,8 +68,9 @@ namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <returns></returns>
-        public static List<Attribute> GetMethodCustomAttribute<T,V>(string[] ScanModel)
+        public static (List<OpenApi>,List<Attribute>) GetMethodCustomAttribute<T,V>(string[] ScanModel)
         {
+            List<OpenApi> openApis = new List<OpenApi>();
             Type attrT = typeof(T);
             Type attrV = typeof(V);
             string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
@@ -77,19 +81,34 @@ namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
                 {
                     Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
                     var TypeInModelS = assembly.GetTypes().SelectMany(x => x.GetMethods()).GroupBy(z => z.Name).ToList();
-                  //  var TypeInModelSS = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(x=>);
-                    TypeInModelS.ForEach(x => {
-                        if (x.Key.Equals("GetTeacherInfo"))
-                        {
-                            var at = x.ToList().SelectMany(z => z.GetCustomAttributes()).Where(m => m.GetType().Equals(attrT));
-                            string ke = x.Key;
+                    //  var TypeInModelSS = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(x=>);
+
+                    var TypeInModelTV = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(m=>m.GetCustomAttributes().Where(t=>t.GetType().Equals(attrT)).Any() && m.GetCustomAttributes().Where(t => t.GetType().Equals(attrV)).Any());
+                    Type attrR = typeof(RouteAttribute);
+                    foreach (var item in TypeInModelTV) {
+                        var routeAttr = item.ReflectedType.GetCustomAttribute<RouteAttribute>();
+                        var postAttr = item.GetCustomAttribute(attrV,true);
+                        var apiAttr = item.GetCustomAttribute(attrT, true);
+                        if (routeAttr!=null && postAttr!=null && apiAttr!=null) {
+                            ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr;
+                            HttpPostAttribute httpPostAttribute = (HttpPostAttribute)postAttr;
+                            OpenApi openApi = new OpenApi
+                            {
+                                PartitionKey = "IES5-API",
+                                RowKey = apiTokenAttribute.Auth,
+                                auth = int.Parse(apiTokenAttribute.Auth),
+                                method = "POST",
+                                name = apiTokenAttribute.Name,
+                                type= apiTokenAttribute.RW,
+                                url =$"/{routeAttr.Template}/{httpPostAttribute.Template}"
+                            };
+                            openApis.Add(openApi);
+                            attributes.Add(apiTokenAttribute);
                         }
-                    });
-                    var TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z => z.GetCustomAttribute(attrT, true)).Where(n => n != null);
-                    attributes.AddRange(TypeInModel);
+                    }
                 }
             }
-            return attributes;
+            return (openApis, attributes) ;
         }
         /// <summary>
         /// 获取T类型属性标记的类集合