/// <summary> /// 接口工廠 /// </summary> public class APIFactoryController : BaseApiController { /// <summary> /// 控制器緩存 /// </summary> private static readonly IList<ControllerCacheModel> ControllerList = new List<ControllerCacheModel>(); static APIFactoryController() { foreach (var item in PublicVars.WebAppAssemblys) { foreach (var controller in item.GetTypes().Where(p => p.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))) { ControllerList.Add(new ControllerCacheModel() { Name = controller.Name, Type = controller, LoginAttr = controller.CustomAttributes.Any(p => p.AttributeType == typeof(AuthorizeAttribute)) }); } } } /// <summary> /// 統一請求接口(★☆★將所有get請求合併爲一個請求後通過該接口一次調用★☆★) /// </summary> /// <param name="model">請求參數</param> /// <returns>數據處理結果</returns> [Route("api/APIFactory")] [CompressionFilter] public async Task<IHttpActionResult> PostAsync(List<ReqPublicFun> model) { return await Task.Run(() => { return HandleReqData(model); }); } /// <summary> /// 數據處理 /// </summary> /// <param name="model"></param> /// <returns></returns> private IHttpActionResult HandleReqData(List<ReqPublicFun> model) { BaseResult<Dictionary<string, object>> rel = new BaseResult<Dictionary<string, object>>() { Data = new Dictionary<string, object>() }; string[] urlArr; string methodName; ControllerCacheModel chController; MethodCacheModel chMethod; MethodInfo method; ParameterInfo[] methodPram; Object[] invokeParas; ReqParam reqParam; bool isLogined = Thread.CurrentPrincipal.Identity.IsAuthenticated; foreach (ReqPublicFun item in model) { #region 調用前驗證 urlArr = item.Url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); //獲取類 chController = ControllerList.FirstOrDefault(p => p.Name.Equals(urlArr[0] + "Controller", StringComparison.OrdinalIgnoreCase)); if (chController == null) { return Content(HttpStatusCode.NotFound, item.Flag + "==>接口地址錯誤"); } if (chController.TypeObj == null)//按需加載下次使用 { chController.TypeObj = AutofacHelper.Resolve(chController.Type); } //獲取方法 methodName = urlArr[1]; chMethod = chController.Methods.FirstOrDefault(p => p.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase)); if (chMethod == null) { method = chController.Type.GetMethod(methodName); if (method == null) { return Content(HttpStatusCode.NotFound, item.Flag + "==>接口地址錯誤"); } chMethod = new MethodCacheModel() { Name = methodName, Method = method, NotLoginAttr = method.CustomAttributes.Any(p => p.AttributeType == typeof(AllowAnonymousAttribute)), LoginAttr = method.CustomAttributes.Any(p => p.AttributeType == typeof(AuthorizeAttribute)) }; chController.Methods.Add(chMethod); //按需加載下次使用 } else { method = chMethod.Method; } //登錄判斷 if (isLogined == false && chMethod.NotLoginAttr == false && (chController.LoginAttr || chMethod.LoginAttr)) { return Content(HttpStatusCode.Unauthorized, item.Flag + "==>請先登錄"); } #endregion #region 參數處理 if (item.Param != null && item.Param.Length > 0) { methodPram = method.GetParameters(); //方法參數集合 invokeParas = new Object[methodPram.Length]; for (int i = 0; i < methodPram.Length; i++) { reqParam = item.Param.FirstOrDefault(p => p.Name.Equals(methodPram[i].Name, StringComparison.OrdinalIgnoreCase)); if (reqParam != null) { switch (methodPram[i].ParameterType.Name) { case "Guid": invokeParas[i] = Guid.Parse(reqParam.Value.ToString()); break; default: invokeParas[i] = Convert.ChangeType(reqParam.Value, methodPram[i].ParameterType); break; } continue; } if (methodPram[i].HasDefaultValue) { invokeParas[i] = methodPram[i].DefaultValue; } } } else { invokeParas = null; } #endregion //方法調用(用了委託發現並無性能提升故使用原生方法) rel.Data.Add(item.Flag, method.Invoke(chController.TypeObj, invokeParas)); } return Json(rel, SerializeHelper.JsonSerializerSettings); } }
/// <summary>
/// 程序項目dll
/// </summary>
public static Assembly[] WebAppAssemblys => BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(p => p.GetName().Name.StartsWith("XX.Web.")).ToArray();