CrazyIter_Bin 3 年之前
父節點
當前提交
c8433f4ca1

+ 4 - 1
TEAMModelAPI/ApiTokenAttribute.cs

@@ -8,6 +8,9 @@ using Microsoft.Extensions.DependencyInjection;
 using System.IdentityModel.Tokens.Jwt;
 using System.Linq;
 using TEAMModelOS.SDK.DI;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
 
 namespace TEAMModelOS.Filter
 {
@@ -47,7 +50,7 @@ namespace TEAMModelOS.Filter
         /// </summary>
         ///public decimal cost { get; set; }
     }
-
+    
     public class ApiTokenAttribute : Attribute, IFilterFactory
     {
         public bool IsReusable => true;

+ 1 - 1
TEAMModelAPI/Controllers/School/SchoolController.cs

@@ -58,7 +58,7 @@ namespace TEAMModelAPI.Controllers
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpGet("get-school-info")]
-        [ApiToken(Auth = "1",Name = "学校信息", Limit=true)]
+        [ApiToken(Auth = "1",Name = "学校信息", Limit=false)]
         public async Task<IActionResult> GetSchoolInfo()
         {
             var (id, school) = HttpContext.GetApiTokenInfo();

+ 1 - 1
TEAMModelAPI/Controllers/WeatherForecastController.cs

@@ -45,7 +45,7 @@ namespace TEAMModelAPI.Controllers
             .ToArray();
         }
         [HttpGet("aaaa")]
-        [ApiToken(Auth = "4")]
+        [ApiToken(Auth = "1")]
         public IEnumerable<WeatherForecast> Getaaaa()
         {
             var rng = new Random();

+ 15 - 0
TEAMModelAPI/Startup.cs

@@ -15,6 +15,10 @@ using TEAMModelOS.SDK.DI;
 using System.IdentityModel.Tokens.Jwt;
 using Microsoft.AspNetCore.Authentication.JwtBearer;
 using Microsoft.IdentityModel.Tokens;
+using TEAMModelOS.Filter;
+using TEAMModelOS.SDK.Helper.Common.ReflectorExtensions;
+using System.Reflection;
+using TEAMModelOS.SDK.Extension;
 
 namespace TEAMModelAPI
 {
@@ -89,6 +93,7 @@ namespace TEAMModelAPI
             //HttpContextAccessor,并用来访问HttpContext。(提供組件或非控制器服務存取HttpContext)
             services.AddHttpContextAccessor();
             services.Configure<Option>(options => Configuration.GetSection("Option").Bind(options));
+           
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -109,6 +114,16 @@ namespace TEAMModelAPI
             {
                 endpoints.MapControllers();
             });
+#if DEBUG
+            //在开发模式时,自检 [ApiToken(Auth = "1")] 有重复的接口
+            List<ApiTokenAttribute> auths = new List<ApiTokenAttribute>();
+            List<Attribute> attributes = ReflectorExtensions.GetMethodCustomAttribute<ApiTokenAttribute>(new string[] { "TEAMModelAPI" });
+            attributes.ForEach(x => {
+                ApiTokenAttribute attribute = (ApiTokenAttribute)x;
+                auths.Add(attribute);
+            });
+            auths.GroupBy(x => x.Auth).ToList().ForEach(x => { if (x.Count() > 1) { throw new Exception($"接口定义Auth重复{x.ToList()}"); } });
+#endif
         }
     }
 }

+ 22 - 0
TEAMModelOS.SDK/Helper/Common/ReflectorExtensions/ReflectorExtensions.cs

@@ -38,6 +38,28 @@ namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
             return value;
         }
 
+        /// <summary>
+        /// 获取T类型的属性标记的类的方法集合
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static List<Attribute> GetMethodCustomAttribute<T>(string[] ScanModel)
+        {
+            Type attr = typeof(T);
+            string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
+            List<Attribute> attributes = new List<Attribute>();
+            if (ScanModel != null)
+            {
+                foreach (var model in ScanModel)
+                {
+                    Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
+                    var  TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z=>z.GetCustomAttribute(attr,true)).Where(n=>n!=null);
+                    attributes.AddRange(TypeInModel);
+                }
+            }
+            return attributes;
+        }
+
         /// <summary>
         /// 获取T类型属性标记的类集合
         /// </summary>