/// <summary> /// 通过接口类型和接口名称获取接口定义 /// </summary> /// <param name="apiType"></param> /// <param name="apiName"></param> /// <returns></returns> public APIItem GetApiItemByTypeAndName(string apiType, string apiName) { var apiTypes = this._WebAPI._project._ApiList._ApiTypes; APIItem apiItem = null; if (apiTypes != null) { foreach (var api in apiTypes) { if (!api.TypeName.Equals(apiType)) { continue; } foreach (APIItem item in api.Apies) { if (item.APIName.Equals(apiName)) { apiItem = item; break; } } } } return(apiItem); }
/// <summary> /// 读取单个API信息 /// </summary> /// <param name="element"></param> /// <param name="assemblyName"></param> /// <returns></returns> private APIItem GetApiItemFromElement(XElement element, string assemblyName) { APIItem item = new APIItem(); item.APIBrief = element.Attributes("APIBrief").FirstOrDefault().Value; item.APIName = element.Attributes("APIName").FirstOrDefault().Value; item.AssemblyName = assemblyName; string parentType = element.Parent.Attributes("TypeName").FirstOrDefault().Value; item.MethodName = element.Attributes("MethodName").FirstOrDefault().Value; item.TypeName = parentType + "." + element.Attributes("TypeName").FirstOrDefault().Value; XElement inputElement = element.Element("InputData"); XElement outputElement = element.Element("OutputData"); item.input = GetParamDefFromElement(inputElement); item.output = GetParamDefFromElement(outputElement); item.ObjectMode = false; bool tempMode; if (element.Attributes("ObjectMode").FirstOrDefault() != null) { if (bool.TryParse(element.Attributes("ObjectMode").FirstOrDefault().Value, out tempMode)) { item.ObjectMode = tempMode; } } return(item); }
/// <summary> /// 从缓存中获取接口定义 /// </summary> /// <param name="strApiName">接口名称</param> /// <returns></returns> public APIItem GetApiFromCache(string strApiName) { APIItem item = null; Cache cache = HttpRuntime.Cache; List <APIItem> items = (List <APIItem>)cache.Get("apilist"); if (items == null) { APIManage manager = APIManage.GetInstance(); items = manager.GetApiList(); } item = GetApiByName(items, strApiName); return(item); }
/// <summary> /// 读取API分类信息 /// </summary> /// <param name="element"></param> /// <returns></returns> private APIType GetApiTypeFromElement(XElement element) { APIType api = new APIType(); api.AssemblyName = element.Attributes("AssemblyName").FirstOrDefault().Value; api.TypeBrief = element.Attributes("TypeBrief").FirstOrDefault().Value; api.TypeName = element.Attributes("TypeName").FirstOrDefault().Value; api.TypeOrder = element.Attributes("TypeOrder").FirstOrDefault().Value; api.Apies = new List <APIItem>(); foreach (var xElement in element.Elements()) { APIItem item = GetApiItemFromElement(xElement, api.AssemblyName); api.Apies.Add(item); } return(api); }
/// <summary> /// 根据接口名称,从接口定义列表中获取指定的接口定义 /// </summary> /// <param name="items">接口定义列表</param> /// <param name="strApiName">接口名称</param> /// <returns></returns> public APIItem GetApiByName(List <APIItem> items, string strApiName) { APIItem item = null; if (items != null) { foreach (APIItem api in items) { if (api.APIName.ToLower().Equals(strApiName.ToLower())) { item = api; break; } } } return(item); }
/// <summary> /// 执行API方法并把执行结果返回 /// </summary> /// <param name="APIName"></param> /// <param name="APIData"></param> /// <returns></returns> public static bool ExecAPI(string APIName, string APIData, out string ExecResult, ref string apiChinaName) { bool isSuccess = false; //实例化API管理对象 APIManage manager = APIManage.GetInstance(); //获取指定API名称的详细信息 APIItem item = manager.GetApiFromCache(APIName); //API执行结果描述 APIResult apiResult = new APIResult(); try { if (item == null) { throw new Exception("接口不存在,找不到此接口:" + APIName); } //获取命名空间 string strNameSpace = manager.GetNameSpace(); //程序集名称 string strAssemblyName = item.AssemblyName; //类型全名称 string strTypeFullName = strNameSpace + "." + item.TypeName; //方法名 string strMethodName = item.MethodName; apiChinaName = item.APIBrief; ///接口不存在 object obj = Reflect.ExecAPI(strAssemblyName, strTypeFullName, strMethodName, APIData, item.ObjectMode); if (item.ObjectMode) { apiResult.result = 0; apiResult.resultStr = "执行成功"; apiResult.data = obj; ExecResult = ReturnStrJson(apiResult); } else { ExecResult = ReturnStrJson(obj); } isSuccess = true; } catch (Exception e) { apiResult.result = 1; if (e.InnerException != null) { apiResult.resultStr = e.InnerException.Message.Replace("\r\n", ""); } else { apiResult.resultStr = e.Message.Replace("\r\n", ""); } ExecResult = ReturnStrJson(apiResult); } //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 return(isSuccess); }