/// <summary> /// 生成TsCode /// </summary> public TsResultModel GetTsResultModel(string itemId, string itemType = "") { TsResultModel result = null; if (itemType.ToLower() == TsCreateType.Controller.ToString().ToLower()) { ControllerModel controller = JcApiHelper.GetController(itemId); if (controller == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(controller); } else if (itemType.ToLower() == TsCreateType.Action.ToString().ToLower()) { ActionModel action = JcApiHelper.GetAction(itemId); if (action == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(action); } else { PTypeModel ptype = JcApiHelper.GetPTypeModel(itemId); if (ptype == null) { throw new Exception("无效的ItemId."); } result = GetTsResultModel(ptype); } return(result); }
/// <summary> /// 生成TsService /// </summary> private string GetTsServiceCommonCode(ActionModel action) { StringBuilder strBuilder = new StringBuilder(); string actionRouteName = (string.IsNullOrEmpty(action.AreaName) ? "" : $"{action.AreaName}/") + $"{action.ControllerName}/{action.ActionName}"; string inputParamStr = ""; string ajaxParamStr = ""; string returnParamTypeStr = ""; if (action.InputParameters != null && action.InputParameters.Count > 0) { if (action.InputParameters.Count == 1 && action.InputParameters[0].HasPiList == true) { inputParamStr = $"{action.InputParameters[0].Name}:{GetTsType(action.InputParameters[0].PType)}"; ajaxParamStr = $",{action.InputParameters[0].Name}"; } else if (action.InputParameters.Any(param => param.Name.ToLower().Contains("index")) && action.InputParameters.Any(param => param.Name.ToLower().Contains("size"))) { //处理分页查询方法 string queryObjTypeName = "PgQueryObj"; if (action.ReturnParameter.PType.PiList?.Count > 0) { for (int i = 0; i < action.ReturnParameter.PType.PiList.Count; i++) { if (action.ReturnParameter.PType.PiList[i].IsIEnumerable) { PTypeModel enumPType = JcApiHelper.GetPTypeModel(action.ReturnParameter.PType.PiList[i].EnumItemId); queryObjTypeName = GetTsType(enumPType) + "QueryObj"; break; } } } inputParamStr = $"{FirstToLower(queryObjTypeName)}: {queryObjTypeName}"; ajaxParamStr = $",{FirstToLower(queryObjTypeName)}"; } else { ajaxParamStr = ",{"; for (int i = 0; i < action.InputParameters.Count; i++) { if (i > 0) { inputParamStr += ","; ajaxParamStr += ","; } inputParamStr += $"{action.InputParameters[i].Name}: {GetTsType(action.InputParameters[i].PType)}"; ajaxParamStr += $"{action.InputParameters[i].Name}: {action.InputParameters[i].Name}"; } ajaxParamStr += "}"; } } returnParamTypeStr = GetTsType(action.ReturnParameter.PType); strBuilder.AppendLine($" /*{(string.IsNullOrEmpty(action.Summary) ? action.ActionName : action.Summary)}*/"); strBuilder.AppendLine($" public { FirstToLower(action.ActionName)}({inputParamStr}): Observable<{returnParamTypeStr}>{{"); strBuilder.AppendLine($" return this.http.post('{actionRouteName}'{ajaxParamStr});"); strBuilder.AppendLine(" }"); return(strBuilder.ToString()); }
public IActionResult GetControllerList() { Robj <List <ControllerModel> > robj = new Robj <List <ControllerModel> >(); try { List <ControllerModel> controllerList = JcApiHelper.GetControllerList(); robj.Result = controllerList; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
/// <summary> /// 获取TsModelList /// </summary> /// <param name="list"></param> /// <param name="ptype"></param> private void FillTsModelList(List <TsModel> list, PTypeModel ptype) { ptype = JcApiHelper.GetPTypeModel(ptype.Id); //读取Ptype注释内容 if (list.Any(tsPtype => tsPtype.Id == ptype.Id)) { //已添加过,不再添加 return; } if (ptype.IsIEnumerable) { //枚举类型 添加其泛型类型 PTypeModel enumItemPtype = JcApiHelper.GetPTypeModel(ptype.EnumItemId); //读取Ptype注释内容 if (enumItemPtype?.PiList?.Count > 0) { FillTsModelList(list, enumItemPtype); } return; } TsModel tsPType = new TsModel() { Id = ptype.Id, Name = GetTsType(ptype), Summary = ptype.Summary, TsModelCode = GetTsModelCode(ptype), PgQueryModelCode = GetTsQueryModelCode(ptype), PiList = new List <TsPi>() }; list.Add(tsPType); for (int i = 0; i < ptype.PiList?.Count; i++) { TsPi tsPi = new TsPi() { Name = ptype.PiList[i].Name, Summary = ptype.PiList[i].Summary, TsType = GetTsType(ptype.PiList[i].PType) }; tsPType.PiList.Add(tsPi); if (ptype.PiList[i].PType.PiList?.Count > 0) { FillTsModelList(list, ptype.PiList[i].PType); } } }
public IActionResult GetPType([FromForm] string typeId) { Robj <PTypeModel> robj = new Robj <PTypeModel>(); try { if (string.IsNullOrEmpty(typeId)) { throw new Exception("参数typeId不能为空"); } PTypeModel ptype = JcApiHelper.GetPTypeModel(typeId); robj.Result = ptype; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
public IActionResult GetAction([FromForm] string actionId) { Robj <ActionModel> robj = new Robj <ActionModel>(); try { if (string.IsNullOrEmpty(actionId)) { throw new Exception("参数Id不能为空"); } ActionModel action = JcApiHelper.GetAction(actionId); robj.Result = action; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
public IActionResult GetController([FromForm] string controllerId) { Robj <ControllerModel> robj = new Robj <ControllerModel>(); try { if (string.IsNullOrEmpty(controllerId)) { throw new Exception("参数controllerId不能为空"); } ControllerModel controller = JcApiHelper.GetController(controllerId); robj.Result = controller; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
public IActionResult GetControllerListByIds([FromBody] List <string> ids) { Robj <List <ControllerModel> > robj = new Robj <List <ControllerModel> >(); try { // List<string> ids = idsStr.Split(',').ToList(); List <ControllerModel> list = new List <ControllerModel>(); for (int i = 0; i < ids.Count; i++) { list.Add(JcApiHelper.GetController(ids[i])); } robj.Result = list; } catch (Exception ex) { robj.Error(ex.Message); } return(new JsonResult(robj)); }
/// <summary> /// c#中的数据类型与TsType对照 /// </summary> /// <param name="ptype"></param> /// <returns></returns> private string GetTsType(PTypeModel ptype) { if (ptype == null) { return(""); } string tsTypeStr = ""; Type type = ptype.SourceType; if (type == typeof(Microsoft.AspNetCore.Mvc.IActionResult)) { tsTypeStr = "any"; } else if (ptype.IsIEnumerable) { //枚举类型特殊处理 PTypeModel enumPType = JcApiHelper.GetPTypeModel(ptype.EnumItemId); tsTypeStr = $"{GetTsType(enumPType)}[]"; } else { tsTypeStr = GetTsType(ptype.TypeName); } return(tsTypeStr); }
/// <summary> /// 使用JcApiHelper /// </summary> /// <param name="app"></param> /// <returns></returns> public static IApplicationBuilder UseJcApiHelper(this IApplicationBuilder app) { app.UseMiddleware <JcApiHelperUIMiddleware>(); JcApiHelper.UseJcApiHelper(); return(app); }
/// <summary> /// Ctor /// </summary> /// <param name="actionProvider">actionProvider</param> public ApiHelperController(IActionDescriptorCollectionProvider actionProvider) { JcApiHelper.Init(actionProvider); }