public IActionResult GenCode(GenCodeConfigInput input) { if (string.IsNullOrEmpty(input.JsonData)) { return(View()); } try { var ErrorDict = new Dictionary <string, List <string> >(); var output = GetConfig(input, ErrorDict); ViewBag.ErrorDict = ErrorDict; Response.ContentType = "text/plian;charset=utf-8"; return(View(output)); } catch (Exception ex) { var errorResult = new { error = "JsonPath Error : " + ex }; return(new JsonResult(errorResult)); } }
public GenCodeOutput GetConfig(GenCodeConfigInput input, Dictionary <string, List <string> > ErrorDict) { var Model = JObject.Parse(input.JsonData); GenCodeOutput genCodeConfig = new GenCodeOutput(); genCodeConfig.Tag = Model["tag"].ToString(); genCodeConfig.ServiceClassName = genCodeConfig.Tag + "ServiceProxy"; List <string> dtoNames = new List <string>(); dtoNames.Add(genCodeConfig.ServiceClassName); #region list var listApi = new ListApi(); if (!string.IsNullOrEmpty(input.ListDtoName)) { listApi.ListResultDtoName = input.ListDtoName; } else { listApi.ListResultDtoName = Model.SelectToken("$.ListApi.responses[0].value[1].value[0].value.refName").ToString(); } var ListApiName = Model.SelectToken("$.ListApi.path").ToString(); if (!string.IsNullOrEmpty(ListApiName)) { ListApiName = ListApiName.Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); } listApi.JsMethodName = ListApiName; dtoNames.Add(listApi.ListResultDtoName); genCodeConfig.ListApi = listApi; if (!string.IsNullOrEmpty(input.ListItemsDtoName)) { listApi.ListResultDtoItemName = input.ListItemsDtoName; } else { try { listApi.ListResultDtoItemName = Model.SelectToken("$.ListApi.responses[0].value[1].value[0].value.refData[1].value[1].value[2].value[0].value.refName").ToString(); } catch (Exception) { throw new Exception("ListResultDtoItemName Not Found , 它不在默认路径下:$.ListApi.responses[0].value[1].value[0].value.refData[1].value[1].value[2].value[0].value.refName/r/n 请手动指定名称"); } } genCodeConfig.ListApi = listApi; dtoNames.Add(listApi.ListResultDtoItemName); JToken ListReturnPropertiesDef = Model.SelectToken("$.ListApi.responses[0].value[1].value[0].value.refData[1].value[1].value[2].value[0].value.refData[1].value"); if (!string.IsNullOrEmpty(input.ListPropertiesJsonPath)) { ListReturnPropertiesDef = Model.SelectToken(input.ListPropertiesJsonPath); } if (ListReturnPropertiesDef != null) { foreach (var item in ListReturnPropertiesDef) { ParamDef paramDef = new ParamDef(); try { GetProperties(item, paramDef); } catch { PushDict(ErrorDict, "ListReturnPropertiesDef", item.ToString()); continue; } genCodeConfig.ListApi.Properties.Add(paramDef); } } var SelectParams = Model.SelectToken("ListApi.parameters"); if (!string.IsNullOrEmpty(input.ListParamsJsonPath)) { SelectParams = Model.SelectToken(input.ListParamsJsonPath); } foreach (var item in SelectParams) { ParamDef paramDef = new ParamDef(); try { GetParemeters(item, paramDef); } catch { PushDict(ErrorDict, "SelectParams", item.ToString()); continue; } genCodeConfig.ListApi.Parameters.Add(paramDef); } #endregion var EnableCreate = Model["CreateApi"] != null; if (EnableCreate) { var createApi = new Createapi(); if (!string.IsNullOrWhiteSpace(input.UpdateDtoName)) { createApi.RequestDtoName = input.UpdateDtoName; } else { var UpdateDtoNamePath = Model.SelectToken("$.CreateApi.parameters[0]"); foreach (var o in UpdateDtoNamePath) { if (o["value"] != null && o["value"].Count() > 1) { var item = o["value"].FirstOrDefault(); if (item.SelectToken("value.refName") != null) { createApi.RequestDtoName = item.SelectToken("value.refName").ToString(); } else { continue; } } else { continue; } } } //if (string.IsNullOrWhiteSpace(createApi.RequestDtoName)) //{ // throw new Exception("未在默认路径下找到 CreateDtoName 请指定 CreateDtoName "); //} var CreateDtoDef = Model.SelectToken("$.CreateApi.parameters[0][3].value[0].value.refData[1].value"); if (!string.IsNullOrEmpty(input.CreateParamsJsonPath)) { CreateDtoDef = Model.SelectToken(input.CreateParamsJsonPath); } if (CreateDtoDef == null) { throw new Exception("在指定路径:未找到创建参数清单。"); } foreach (var item in CreateDtoDef) { ParamDef paramDef = new ParamDef(); try { GetProperties(item, paramDef); } catch { PushDict(ErrorDict, "CreateDtoDef", item.ToString()); continue; } createApi.Properties.Add(paramDef); } var CreateApiName = Model["CreateApi"]["path"].ToString().Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); createApi.JsMethodName = CreateApiName.ToLowerStart(); dtoNames.Add(createApi.RequestDtoName); genCodeConfig.CreateApi = createApi; } var EnableUpdate = Model["UpdateApi"] != null; if (EnableUpdate) { Updateapi updateapi = new Updateapi(); var UpdateDtoDef = Model.SelectToken("$.UpdateApi.parameters[0][3].value[0].value.refData[1].value"); if (!string.IsNullOrWhiteSpace(input.UpdateParamsJsonPath)) { UpdateDtoDef = Model.SelectToken(input.UpdateParamsJsonPath); } foreach (var item in UpdateDtoDef) { ParamDef paramDef = new ParamDef(); try { GetProperties(item, paramDef); } catch { PushDict(ErrorDict, "CreateDtoDef", item.ToString()); continue; } updateapi.Properties.Add(paramDef); } if (!string.IsNullOrWhiteSpace(input.UpdateDtoName)) { updateapi.RequestDtoName = input.UpdateDtoName; } else { var UpdateDtoNamePath = Model.SelectToken("$.UpdateApi.parameters[0]"); foreach (var o in UpdateDtoNamePath) { if (o["value"] != null && o["value"].Count() > 1) { var item = o["value"].FirstOrDefault(); if (item.SelectToken("value.refName") != null) { updateapi.RequestDtoName = item.SelectToken("value.refName").ToString(); } else { continue; } } else { continue; } } } //if (string.IsNullOrWhiteSpace(updateapi.RequestDtoName)) //{ // throw new Exception("未在默认路径下找到 CreateDtoName 请指定 CreateDtoName "); //} dtoNames.Add(updateapi.RequestDtoName); var UpdateApiName = Model["UpdateApi"]["path"].ToString().Split('/', StringSplitOptions.RemoveEmptyEntries).Last(); updateapi.JsMethodName = UpdateApiName.ToLowerStart(); genCodeConfig.UpdateApi = updateapi; } var EnableDelete = Model["DeleteApi"] != null; if (EnableDelete) { Deleteapi deleteapi = new Deleteapi(); var DeleteApiName = Model["DeleteApi"]["path"].ToString().Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); deleteapi.JsMethodName = DeleteApiName.ToLowerStart(); genCodeConfig.DeleteApi = deleteapi; } genCodeConfig.ImportClassNames = dtoNames; return(genCodeConfig); }