public static string CreateApiFile_All(List <string> list) { var i = 0; var CApiConfigList = CApiConfig.GetList(); if (list != null && list.Count > 0) { CApiConfigList = CApiConfigList.Where(x => list.Contains(x.Url)).ToList(); } foreach (var item in CApiConfigList) { if (CreateApiFile(item.CName, item.AName, item.IsPre).Count > 0) { i++; } } var HtmlStr = ""; HtmlStr = string.Format("<h2>重新生成完毕!</h2><br>成功:{0}条<br>失败:{1}条", CApiConfigList.Count, i); if (CApiConfigList.Count == 0) { HtmlStr = "<h2>无可生成的文件!</h2>"; } return(HtmlStr); }
public static List <string> CreateApiHtml(ApiData apiData) { var resultList = new List <string>(); try { var jsonHelper = new JavaScriptSerializer(); apiData.EntityNullToStr <ApiData>();//对象中的string类型空值转成"" var html = File.ReadAllText(CApiConfig.templateUrl); var paramHtml = GetFiledsHtml(apiData.ParamDataList, 1); var backparamHtml = GetFiledsHtml(apiData.BackDataList, 0, true); var dataDic = new Dictionary <string, object>(); foreach (var item in apiData.BackDataList) { dataDic.Add(item.Name, ""); } var dic = new Dictionary <string, object>() { }; dic.Add("Stata", "200"); dic.Add("Message", "ok"); dic.Add("Data", dataDic); var backSuccess = jsonHelper.Serialize(dic); #region 替换文本 html = html.Replace("{{ParamDataList}}", paramHtml); html = html.Replace("{{BackDataList}}", backparamHtml); html = html.Replace("{{Name}}", apiData.Name); html = html.Replace("{{BackSuccess}}", backSuccess); html = html.Replace("{{Describtion}}", apiData.Describtion); html = html.Replace("{{ApiUrl}}", apiData.ApiUrl); html = html.Replace("{{Methods}}", apiData.Methods); html = html.Replace("{{FuncName}}", apiData.FuncName); #endregion #region 生成文件-html string path = CApiConfig.fileUrl; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = string.Format("{0}{1}.html", apiData.CName, "_" + apiData.FuncName); string fileFullPath = CApiConfig.fileUrl + fileName; StreamWriter sw; if (!File.Exists(fileFullPath)) { sw = File.CreateText(fileFullPath); sw.WriteLine(html.ToString()); sw.Close(); } else { File.WriteAllText(fileFullPath, html); } #endregion #region 保存生成的记录-config.json var list = CApiConfig.GetList() ?? new List <CApiConfig>(); list.Add(new CApiConfig(apiData.CName, apiData.Name, fileName, apiData.FuncName, apiData.IsPre)); list = list.GroupBy(x => x.Url).Select(x => x.FirstOrDefault()).ToList();//去重复 CApiConfig.SaveData(list); #endregion #region 生成文件-Index文件 var IndexHtml = File.ReadAllText(CApiConfig.templateUrl_index);//读取模板文件 var one_Url = list == null ? "" : list[0].Url; var one_Name = list == null ? "" : list[0].Name; foreach (var item in list.GroupBy(x => x.CName).Select(x => x.FirstOrDefault()).ToList()) { var sbHtml = new StringBuilder(); var i = 0; foreach (var a in list.Where(x => x.CName == item.CName)) { sbHtml.AppendFormat(aHtml, a.Url, a.Name, i++); } IndexHtml = IndexHtml.Replace("{{" + item.CName + "}}", sbHtml.ToString()); } #region 替换配置信息 IndexHtml = IndexHtml.Replace("{{One_Url}}", one_Url); IndexHtml = IndexHtml.Replace("{{One_Name}}", one_Name); IndexHtml = IndexHtml.Replace("{{IndexTitle}}", _IndexTitle); IndexHtml = IndexHtml.Replace("{{Api_Url}}", _ApiRequestUrl); #endregion string IndexFileFullPath = CApiConfig.indexUrl; File.WriteAllText(IndexFileFullPath, IndexHtml); #endregion } catch (Exception ex) { resultList.Add(ex.Message); } return(resultList); }