123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- using DocTools.Entity;
- using DocTools.Entity.Doc;
- using DocTools.Helper;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace DocTools
- {
- public class CreateDocHtml
- {
- private static Dictionary<string, string> PublicEnumDic = new Dictionary<string, string>();
- private static string FileName = "demoHtml.html";
- /// <summary>
- /// 创建最基础json信息
- /// </summary>
- /// <returns></returns>
- public ResultEntity CreateDocProjectString(string version = "", string fileName = "")
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- FileName = fileName;
- }
- var result = new ResultEntity()
- {
- create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- id = "216603",
- name = "新版杏聆荟RPC接口文档",
- project_id = "2116053",
- project_name = "新版杏聆荟RPC接口文档" + version,
- publisher = "mr_kazuta",
- target_id = "2116053",
- target_type = "project"
- };
- var projectEntity = new ProjectEntity();
- var envItem = new EnvEntity()
- {
- env_id = "-1",
- list = new NullParameterEntity(),
- name = "默认环境"
- };
- projectEntity.envs.Add(envItem);
- var respParam = new ResponseParameterEntity()
- {
- description = "",
- is_checked = 1,
- key = "",
- type = "Text",
- value = ""
- };
- var varsItem = new VarsEntity()
- {
- variable = new List<NullParameterEntity>(),
- script = new ScriptEntity(),
- methods = new List<string>() { "POST", "GET", "PUT", "PATCH", "DELETE", "COPY", "HEAD", "OPTIONS", "LINK", "UNLINK", "PURGE", "LOCK", "UNLOCK", "PROPFIND", "VIEW" },
- mark = new List<MarkEntity>() {
- new MarkEntity() { key = "developing", name = "开发中" },
- new MarkEntity() { key = "complated", name = "已完成" },
- new MarkEntity() { key = "modifying", name = "需修改" },
- },
- request = new VarsRequestEntity()
- {
- body = new List<ResponseParameterEntity>() { respParam },
- cookie = new List<ResponseParameterEntity>() { respParam },
- header = new List<ResponseParameterEntity>() { respParam },
- query = new List<ResponseParameterEntity>() { respParam },
- }
- };
- projectEntity.vars = varsItem;
- result.project = projectEntity;
- result.targets = GetTargetsFolderString();
- return result;
- }
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <returns></returns>
- public List<FolderChildrenEntity> GetTargetsFolderString()
- {
- var resultList = new List<FolderChildrenEntity>();
- List<APIModuleEntity> apiModuleList = LoadData.PackageInterfaceMemberInfo() ?? new List<APIModuleEntity>();
- foreach (var item in apiModuleList)
- {
- if (item.Example == "客户端API")
- {
- item.CreateTime = DateTime.Now.AddYears(100);
- }
- if (item.Example == "设备端API")
- {
- item.CreateTime = DateTime.Now.AddYears(50);
- }
- }
- apiModuleList = apiModuleList.OrderByDescending(c => c.CreateTime).ToList();
- for (int i = 0; i < apiModuleList.Count; i++)
- {
- var customerAPI = new FolderChildrenEntity();
- customerAPI.is_doc = "0";
- customerAPI.local_parent_id = "0";
- customerAPI.local_target_id = Guid.NewGuid().ToString();
- customerAPI.mark = "complated";
- customerAPI.method = "POST";
- customerAPI.name = apiModuleList[i].Example;
- customerAPI.script = new ScriptEntity();
- customerAPI.sort = 7 + i;
- customerAPI.status = 1;
- customerAPI.target_id = CommonHelper.GetSpecicalTargetIndex();
- customerAPI.target_type = "folder";
- customerAPI.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- customerAPI.request = new FoldRequestEntity()
- {
- body = new List<NullParameterEntity>(),
- header = new List<NullParameterEntity>(),
- query = new List<NullParameterEntity>(),
- //description = "新版杏聆荟RPC接口文档"
- description = null
- };
- customerAPI.children = GetCustomerChildrenList(customerAPI, apiModuleList[i]);
- resultList.Add(customerAPI);
- }
- return resultList;
- }
- private string CreateFolderHtml(Dictionary<string, string> dic,string apiName)
- {
- string allHtml = "";
- if (dic?.Count > 0)
- {
- allHtml = "<div style=\"overflow: hidden;height: 100%;\" class=\"preview-markdown\"><h3 class=\"title\"><a href=\"javascript:;\" class=\"maodian\"></a>" + apiName + "下属服务<i class=\"fa fa-angle-down\"></i></h3>";
- var tableHtml = "<table><tbody><tr><th>服务名</th><th>查看详情</th></tr>";
- foreach (var key in dic.Keys)
- {
- tableHtml += "<tr><td>" + key + "</td><td><a target=\"_blank\" style=\"text-decoration: underline;color: blue;\" href=\"/" + FileName + "#" + dic[key] + "\">详情</a></td></tr>";
- }
- tableHtml += "</tbody></table>";
- allHtml += tableHtml + "</div>";
- }
- return allHtml;
- }
- /// <summary>
- /// 客户端api接口
- /// </summary>
- /// <returns></returns>
- public List<object> GetCustomerChildrenList(FolderChildrenEntity customerAPI, APIModuleEntity apiModuleEntity)
- {
- var dic = new Dictionary<string, string>();
- var list = new List<object>();
- if (apiModuleEntity.IsShowEnumFolder)
- {
- var dicEnum = new Dictionary<string, string>();
- FolderChildrenEntity enumFolderEntity = new FolderChildrenEntity();
- enumFolderEntity.is_doc = "0";
- enumFolderEntity.local_parent_id = customerAPI.local_target_id;
- enumFolderEntity.local_target_id = Guid.NewGuid().ToString();
- enumFolderEntity.mark = "complated";
- enumFolderEntity.method = "POST";
- enumFolderEntity.name = "枚举代码";
- enumFolderEntity.sort = 17;
- enumFolderEntity.status = 1;
- enumFolderEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
- enumFolderEntity.target_type = "folder";
- enumFolderEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- enumFolderEntity.script = new ScriptEntity();
- enumFolderEntity.request = new FoldRequestEntity();
- List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
- var enumList = memberList.FindAll(c => c.ParamName.Contains("T:WingInterfaceLibrary.Enum.") && !string.IsNullOrEmpty(c.Remarks));
- for (int i = 0; i < enumList.Count; i++)
- {
- var item = enumList[i];
- if (item.ParamName.Contains("WingInterfaceLibrary.Enum.RpcExceptionEnum."))
- {
- continue;
- }
- var enumEntity = GetSpecialEnum(item.ParamName.Replace("T:", ""), enumFolderEntity.local_target_id, item.Remarks, i);
- enumFolderEntity.children.Add(enumEntity);
- dicEnum.Add(enumEntity.name, enumEntity.local_target_id);
- }
- enumFolderEntity.tableHtml = CreateFolderHtml(dicEnum, enumFolderEntity.name);
- list.Add(enumFolderEntity);
- dic.Add(enumFolderEntity.name, enumFolderEntity.local_target_id);
- }
- //定义api下属文件夹
- //这里可以是多个文件夹
- List<InterfaceInfoEntity> interfaceList = apiModuleEntity.MainModelList;
- for (int i = 0; i < interfaceList.Count; i++)
- {
- var interfaceItem = interfaceList[i];
- FolderChildrenEntity folderEntity = new FolderChildrenEntity();
- folderEntity.is_doc = "0";
- folderEntity.local_parent_id = customerAPI.local_target_id;
- folderEntity.local_target_id = Guid.NewGuid().ToString();
- folderEntity.mark = "complated";
- folderEntity.method = "POST";
- folderEntity.name = interfaceItem.Value;
- folderEntity.sort = i + 1 + customerAPI.sort;
- folderEntity.status = 1;
- folderEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
- folderEntity.target_type = "folder";
- folderEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- folderEntity.script = new ScriptEntity();
- folderEntity.request = new FoldRequestEntity();
- folderEntity.children = GetInterfaceChildrenList(folderEntity, interfaceItem.Url, interfaceItem.InterfaceList);
- list.Add(folderEntity);
- dic.Add(folderEntity.name, folderEntity.local_target_id);
- }
- customerAPI.tableHtml = CreateFolderHtml(dic, customerAPI.name);
- return list;
- }
- /// <summary>
- /// 获取分类下的接口
- /// </summary>
- /// <returns></returns>
- public List<object> GetInterfaceChildrenList(FolderChildrenEntity folderEntity, string url, List<InterfaceBasicInfoEntity> interfaceList)
- {
- var dicInterface = new Dictionary<string, string>();
- var exclusiveList = new List<string>() { "IClientLogService", "IEmailService", "ISMSService" };
- List<object> resultList = new List<object>();
- for (int i = 0; i < interfaceList.Count; i++)
- {
- var dicMethod = new Dictionary<string, string>();
- var interfaceEntity = interfaceList[i];
- if (exclusiveList.Contains(interfaceEntity.InterfaceName))
- {
- continue;
- }
- FolderChildrenEntity folderChildEntity = new FolderChildrenEntity();
- folderChildEntity.index = 4;
- folderChildEntity.is_doc = "0";
- folderChildEntity.local_parent_id = folderEntity.local_target_id;
- folderChildEntity.local_target_id = Guid.NewGuid().ToString();
- folderChildEntity.mark = "complated";
- folderChildEntity.method = "POST";
- folderChildEntity.name = interfaceEntity.InterfaceSummary;
- folderChildEntity.sort = Convert.ToInt32(folderEntity.sort.ToString() + i);
- folderChildEntity.status = 1;
- folderChildEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
- folderChildEntity.target_type = "folder";
- folderChildEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- folderChildEntity.script = new ScriptEntity();
- folderChildEntity.request = new FoldRequestEntity();
- folderChildEntity.children = new List<object>();
- for (int j = 0; j < interfaceEntity.MethodList.Count; j++)
- {
- var methodEntity = interfaceEntity.MethodList[j];
- ChildrenEntity childEntity = new ChildrenEntity();
- childEntity.is_doc = "0";
- childEntity.index = 5;
- childEntity.local_parent_id = folderChildEntity.local_target_id;
- childEntity.local_target_id = Guid.NewGuid().ToString();
- childEntity.mark = "complated";
- childEntity.method = methodEntity.SubmitType;
- childEntity.name = methodEntity.Summary;
- childEntity.technology = methodEntity.MethodName;
- childEntity.sort = Convert.ToInt32(folderChildEntity.sort.ToString() + j);
- childEntity.status = 1;
- childEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
- childEntity.target_type = "api";
- childEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- if (methodEntity.SubmitType == "PUT")
- {
- var responseEntity = new ResponseEntity()
- {
- error = new ResponseResultEntity()
- {
- raw = "{\n\t\"IsSuccess\": \"False\", //是否成功: True - 表示成功,False - 表示失败\n\t\"Msg\": \"\" //错误信息内容\n}",
- parameter = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = "是否成功: True-表示成功,False-表示失败",
- is_checked = 1,
- key = "IsSuccess",
- type = "Text",
- field_type = "Boolean",
- value = "False"
- },
- new ResponseParameterEntity()
- {
- description= "错误信息内容",
- is_checked = 1,
- key = "Msg",
- type = "Text",
- field_type = "String",
- value = "Upload Fail"
- }
- }
- },
- success = new ResponseResultEntity()
- {
- raw = "{\n\t\"IsSuccess\": \"True\", //是否成功: True-表示成功,False-表示失败\n\t\"Msg\": \"\" //错误信息,如果成功,则为空\n}",
- parameter = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = "是否成功: True-表示成功,False-表示失败",
- is_checked = 1,
- key = "IsSuccess",
- type = "Text",
- field_type = "Boolean",
- value = "True"
- },
- new ResponseParameterEntity()
- {
- description= "错误信息,如果成功,则为空",
- is_checked = 1,
- key = "Msg",
- type = "Text",
- field_type = "String",
- value = ""
- }
- }
- }
- };
- var fileInfo = methodEntity.RequestList?.FirstOrDefault()?.ParamPropertyList?.Find(c => c.Name == "File") ?? new ParamPropertyEntity();
- var dafultFieldType = CommonHelper.GetJsonDataTypeByString(fileInfo.TypeFullName);
- var requestBodyEntity = new RequestBodyEntity();
-
- requestBodyEntity = new RequestBodyEntity()
- {
- mode = "byte",
- parameter = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = fileInfo.Desc,
- is_checked = 1,
- key = fileInfo.Name,
- type = dafultFieldType,
- not_null = "1",
- field_type = dafultFieldType,
- value = fileInfo.DefaultValue
- }
- },
- raw = "",
- raw_para = new List<ResponseParameterEntity>()
- };
-
- var headerParameterRequest = new List<object>();
- var headerParamList = methodEntity.RequestList?.FirstOrDefault()?.ParamPropertyList?.FindAll(c => c.Name != "File") ?? new List<ParamPropertyEntity>();
- if (headerParamList?.Count > 0)
- {
- foreach (var headerItem in headerParamList)
- {
- headerParameterRequest.Add(new ResponseParameterEntity()
- {
- description = headerItem.Desc,
- is_checked = 1,
- key = headerItem.Name,
- type = "Text",
- not_null = "1",
- field_type = headerItem.TypeFullName,
- value = headerItem.DefaultValue
- });
- }
- }
- var headerRequest = new
- {
- parameter = headerParameterRequest
- };
- childEntity.request = new RequestEntity()
- {
- url = url + "文件名【此请求地址为动态地址,实际值为上传文件权鉴接口返回的StorageUrl字段的值】",
- resful = new RequestParameterEntity(),
- query = new RequestParameterEntity(),
- header = headerRequest,
- Event = new ScriptEntity(),
- description = "",
- cookie = new RequestParameterEntity(),
- body = requestBodyEntity
- };
- childEntity.response = responseEntity;
- }
- else
- {
- var errorParameter = new ResponseResultEntity()
- {
- raw = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"id\": 1, //结果Id\n\t\"error\": {\n\t\t\"code\": 1002, //错误码\n\t\t\"message\": \"Permission validation error\" //错误信息\n\t}\n}",
- parameter = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = "rpc版本",
- is_checked = 1,
- key = "jsonrpc",
- type = "Text",
- field_type = "Number",
- value = "2.0"
- },
- new ResponseParameterEntity()
- {
- description = "结果Id",
- is_checked = 1,
- key = "id",
- type = "Text",
- field_type = "Number",
- value = "1"
- },
- new ResponseParameterEntity()
- {
- description = "错误响应",
- is_checked = 1,
- key = "error",
- type = "Text",
- field_type = "Object",
- value = ""
- },
- new ResponseParameterEntity()
- {
- description = "错误码",
- is_checked = 1,
- key = "error.code",
- type = "Text",
- field_type = "Number",
- value = "1002"
- },
- new ResponseParameterEntity()
- {
- description = "错误信息",
- is_checked = 1,
- key = "error.message",
- type = "Text",
- field_type = "String",
- value = "Permission validation error"
- }
- }
- };
- var successParameter = new ResponseResultEntity()
- {
- parameter = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = "rpc版本",
- is_checked = 1,
- key = "jsonrpc",
- type = "Text",
- field_type = "Number",
- value = "2.0"
- },
- new ResponseParameterEntity()
- {
- description = "结果Id",
- is_checked = 1,
- key = "id",
- type = "Text",
- field_type = "Number",
- value = "1"
- }
- },
- error_result = new List<ExceptionResultEntity>()
- };
- //计算响应列表枚举
- if (!string.IsNullOrEmpty(methodEntity.ErrorCodes))
- {
- var gere = GetExceptionResultEnum(methodEntity.ErrorCodes);
- successParameter.error_result.Add(gere);
- }
- var rawResultStr = "";
- if (methodEntity.ResponseList?.Count > 0)
- {
- var returnTypeStr = CommonHelper.GetJsonDataTypeByString(methodEntity.ResponseList.FirstOrDefault().TypeFullName);
- var resultObject = new ResponseParameterEntity()
- {
- description = "结果集",
- is_checked = 1,
- key = "result",
- type = "Text",
- field_type = returnTypeStr,
- value = ((returnTypeStr != "Object" && returnTypeStr != "Array") ? methodEntity.ResponseList.FirstOrDefault().DefaultValue : "")
- };
- successParameter.parameter.Add(resultObject);
- var rList = GetChildParameterEntity(returnTypeStr, methodEntity.ResponseList, "result");
- if (rList?.Count > 0)
- {
- rawResultStr += returnTypeStr == "Array" ? "[" : "";
- successParameter.parameter.AddRange(rList);
- rawResultStr += "{";
- rawResultStr += DealWithString(GetChildParameterString(returnTypeStr, methodEntity.ResponseList));
- //foreach (var rItem in rList)
- //{
- // rawResultStr += "\n\t\t\""+ rItem.key.Replace("result[0].","").Replace("result.", "") + "\": \""+ rItem.value + "\",";
- //}
- rawResultStr = rawResultStr.TrimEnd(',');
- rawResultStr += "\n\t}";
- rawResultStr += returnTypeStr == "Array" ? "]" : "";
- }
- else
- {
- var firstResponseEntity = methodEntity.ResponseList?.FirstOrDefault() ?? new ParamPropertyEntity();
- if (firstResponseEntity.TypeFullName.Contains("System.Collections.Generic.List"))
- {
- var firstParamEntity = firstResponseEntity.ParamPropertyList.FirstOrDefault() ?? new ParamPropertyEntity();
- if (firstParamEntity.TypeFullName == "System.String" || firstParamEntity.TypeFullName == "System.Int32" || firstParamEntity.TypeFullName == "System.Decimal" || firstParamEntity.TypeFullName == "System.Int16"
- || firstParamEntity.TypeFullName == "System.Long" || firstParamEntity.TypeFullName == "System.Int64" || firstParamEntity.TypeFullName == "System.Float" || firstParamEntity.TypeFullName == "System.Double"
- || firstParamEntity.TypeFullName == "System.Boolean")
- {
- rawResultStr = "["+ firstResponseEntity.Desc + "]";
- }
- else
- {
- rawResultStr = firstResponseEntity.DefaultValue;
- }
- }
- else
- {
- rawResultStr = firstResponseEntity.DefaultValue;
- }
- }
- successParameter.raw = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"id\": 1, //结果Id\n\t\"result\": " + rawResultStr + "\n}";
- }
- else
- {
- rawResultStr = methodEntity.Returns;
- successParameter.raw = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"id\": 1, //结果Id\n\t\"result\": " + rawResultStr + "\n}";
- }
- childEntity.response = new ResponseEntity()
- {
- error = errorParameter,
- success = successParameter
- };
- string rawStr = "";
- var requestRawPara = new List<ResponseParameterEntity>()
- {
- new ResponseParameterEntity()
- {
- description = "rpc版本",
- is_checked = 1,
- key = "jsonrpc",
- not_null = "1",
- type = "Text",
- field_type = "Number",
- value = "2.0"
- },
- new ResponseParameterEntity()
- {
- description = "结果Id",
- is_checked = 1,
- key = "id",
- not_null = "1",
- type = "Text",
- field_type = "Number",
- value = "1"
- },
- new ResponseParameterEntity()
- {
- description = "rpc接口名",
- is_checked = 1,
- key = "method",
- not_null = "1",
- type = "Text",
- field_type = "String",
- value = methodEntity.MethodName
- }
- };
- var paramTypeStr = CommonHelper.GetJsonDataTypeByString(methodEntity.ParamTypeFullName);
- var paramObject = new ResponseParameterEntity()
- {
- description = methodEntity.RequestList?.FirstOrDefault()?.Desc,
- is_checked = 1,
- key = "params",
- not_null = "1",
- type = "Text",
- field_type = "Array",
- value = ""//methodEntity.ParamText
- };
- requestRawPara.Add(paramObject);
- if (methodEntity.RequestList?.Count > 0)
- {
- var rList = GetChildParameterEntity(paramTypeStr, methodEntity.RequestList, "params");
- var rawRequestParamStr = "";
- if (rList?.Count > 0)
- {
- requestRawPara.AddRange(rList);
- rawRequestParamStr += "{";
- rawRequestParamStr += DealWithString(GetChildParameterString(paramTypeStr, methodEntity.RequestList));
- //foreach (var rItem in rList)
- //{
- // rawRequestParamStr += "\n\t\t\"" + rItem.key.Replace("params[0].", "") + "\": \"" + rItem.value + "\",";
- //}
- rawRequestParamStr = rawRequestParamStr.TrimEnd(',');
- rawRequestParamStr += "\n\t}";
- }
- rawStr = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"method\": \"" + methodEntity.MethodName + "\", //rpc接口名\n\t\"params\": [" + rawRequestParamStr + "],\n\t\"id\": 1 //结果Id\n}";
- }
- else
- {
- rawStr = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"method\": \"" + methodEntity.MethodName + "\", //rpc接口名\n\t\"params\": [],\n\t\"id\": 1 //结果Id\n}";
- }
- var requestBodyEntity = new RequestBodyEntity()
- {
- mode = "json",
- parameter = new List<ResponseParameterEntity>(),
- raw = rawStr,
- raw_para = requestRawPara
- };
- childEntity.request = new RequestEntity()
- {
- url = url + interfaceEntity.InterfaceName,
- resful = new RequestParameterEntity(),
- query = new RequestParameterEntity(),
- header = new RequestParameterEntity(),
- Event = new ScriptEntity(),
- description = "",
- cookie = new RequestParameterEntity(),
- body = requestBodyEntity
- };
- }
- folderChildEntity.children.Add(childEntity);
- dicMethod.Add(childEntity.name, childEntity.local_target_id);
- }
- folderChildEntity.tableHtml = CreateFolderHtml(dicMethod, folderChildEntity.name);
- resultList.Add(folderChildEntity);
- dicInterface.Add(folderChildEntity.name, folderChildEntity.local_target_id);
- }
- folderEntity.tableHtml = CreateFolderHtml(dicInterface, folderEntity.name);
- return resultList;
- }
- private string DealWithString(string resultStr)
- {
- if (string.IsNullOrEmpty(resultStr))
- {
- return "";
- }
- string newStr = "";
- int lastIndex = resultStr.LastIndexOf(',');
- var lastStr = resultStr.Substring(lastIndex);
- if (!lastStr.Contains("\n"))
- {
- newStr = resultStr.Substring(0, lastIndex) + resultStr.Substring(lastIndex + 1);
- }
- return newStr;
- }
- public List<ResponseParameterEntity> GetChildParameterEntity(string returnTypeStr, List<ParamPropertyEntity> responseList, string typeStr, int level = 2)
- {
- List<ResponseParameterEntity> resultList = new List<ResponseParameterEntity>();
- if (returnTypeStr == "Object")
- {
- var res = responseList.FirstOrDefault();
- if (res.ParamPropertyList?.Count > 0)
- {
- foreach (var responseItem in res.ParamPropertyList)
- {
- var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
- var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
- var childObject = new ResponseParameterEntity()
- {
- file_name = FileName,
- description = responseItem.Desc,
- is_checked = 0,
- type = "Text",
- field_type = childTypeStr,
- value = responseItem.DefaultValue
- };
- //if (childTypeStr.Contains("Enum"))
- //{
- // childObject.field_type = "<a href=\"#"+ childTypeStr + "\">" + childObject.field_type + "</a>";
- //}
- if (childTypeStr.Contains("Enum"))
- {
- //var key = "WingInterfaceLibrary.Enum." + childTypeStr;
- var key = childTypeStr;
- if (PublicEnumDic.Keys.Contains(key))
- {
- childObject.field_type_value = 1;
- childObject.field_type_target_id = PublicEnumDic[key];
- }
- var arr = childTypeStr.Split('.');
- childObject.field_type = arr[arr.Length - 1];
- }
- if (childTypeStr == "Array" && generalTypeStr != "Object")
- {
- childObject.value = "[" + responseItem.DefaultValue + "]";
- }
- childObject.key = typeStr + "." + responseItem.Name;
- if (typeStr == "params")
- {
- childObject.key = typeStr + "[0]." + responseItem.Name;
- }
- if (typeStr != "result")
- {
- childObject.not_null = responseItem.IsNotNull;
- }
- resultList.Add(childObject);
- if (responseItem.ParamPropertyList?.Count > 0)
- {
- var childList = GetChildParameterEntity(childTypeStr, responseItem.ParamPropertyList, childObject.key, 1);
- resultList.AddRange(childList);
- }
- }
- }
- }
- else if (returnTypeStr == "Array")
- {
- ParamPropertyEntity res = null;
- if (level == 2)
- {
- res = responseList.FirstOrDefault()?.ParamPropertyList?.FirstOrDefault();
- }
- else
- {
- res = responseList.FirstOrDefault();
- }
- if (res?.ParamPropertyList?.Count > 0)
- {
- foreach (var responseItem in res.ParamPropertyList)
- {
- var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
- var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
- var childObject = new ResponseParameterEntity()
- {
- file_name = FileName,
- description = responseItem.Desc,
- is_checked = 0,
- type = "Text",
- field_type = childTypeStr,
- value = responseItem.DefaultValue
- };
- //if (childTypeStr.Contains("Enum"))
- //{
- // childObject.field_type = "<a href=\"#" + childTypeStr + "\">" + childObject.field_type + "</a>";
- //}
- if (childTypeStr.Contains("Enum"))
- {
- //var key = "WingInterfaceLibrary.Enum." + childTypeStr;
- var key = childTypeStr;
- if (PublicEnumDic.Keys.Contains(key))
- {
- childObject.field_type_value = 1;
- childObject.field_type_target_id = PublicEnumDic[key];
- }
- var arr = childTypeStr.Split('.');
- childObject.field_type = arr[arr.Length - 1];
- }
- if (childTypeStr == "Array" && generalTypeStr != "Object")
- {
- childObject.value = "[" + responseItem.DefaultValue + "]";
- }
- childObject.key = typeStr + "[0]." + responseItem.Name;
- if (typeStr != "result")
- {
- childObject.not_null = responseItem.IsNotNull;
- }
- resultList.Add(childObject);
- if (responseItem.ParamPropertyList?.Count > 0)
- {
- var childList = GetChildParameterEntity(childTypeStr, responseItem.ParamPropertyList, childObject.key, 1);
- resultList.AddRange(childList);
- }
- }
- }
- }
- else
- {
- }
- return resultList;
- }
- /// <summary>
- /// 拼接显示的字符串
- /// </summary>
- /// <param name="returnTypeStr">返回类型</param>
- /// <param name="responseList">响应列表</param>
- /// <param name="level">数据层级</param>
- /// <param name="retract">缩进大小</param>
- /// <returns></returns>
- public string GetChildParameterString(string returnTypeStr, List<ParamPropertyEntity> responseList, int level = 2, int retract = 0)
- {
- List<string> typeList = new List<string>() { "Boolean", "Double", "Float", "Number", "Integer" };
- string resultStr = "";
- string retractStr = "";
- for (int i = 0; i < (retract + 1); i++)
- {
- if (i == 0)
- {
- retractStr += "\t\t";
- }
- else
- {
- retractStr += "\t";
- }
- }
- if (returnTypeStr == "Object")
- {
- var res = responseList.FirstOrDefault();
- if (res.ParamPropertyList?.Count > 0)
- {
- foreach (var responseItem in res.ParamPropertyList)
- {
- var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
- var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
- if (childTypeStr == "Object")
- {
- retract++;
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":{";
- }
- else if (childTypeStr == "Array")
- {
- retract++;
- if (generalTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[{";
- }
- else if (generalTypeStr == "Array")
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[[{";
- }
- else
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[";
- resultStr += "\n" + retractStr + "\t" + responseItem.DefaultValue + " //" + responseItem.Desc;
- }
- }
- else
- {
- if (typeList.Contains(childTypeStr) || childTypeStr.Contains("Enum"))
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": " + responseItem.DefaultValue + ", //" + responseItem.Desc;
- }
- else
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": \"" + responseItem.DefaultValue + "\", //" + responseItem.Desc;
- }
- }
- if (responseItem.ParamPropertyList?.Count > 0)
- {
- var childStr = DealWithString(GetChildParameterString(childTypeStr, responseItem.ParamPropertyList, 1, retract));
- resultStr += childStr;
- }
- if (childTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "},";
- }
- else if (childTypeStr == "Array")
- {
- if (generalTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "}],";
- }
- else if (generalTypeStr == "Array")
- {
- resultStr += "\n" + retractStr + "}]],";
- }
- else
- {
- resultStr += "\n" + retractStr + "],";
- }
- }
- else
- {
- }
- }
- }
- }
- else if (returnTypeStr == "Array")
- {
- ParamPropertyEntity res = null;
- if (level == 2)
- {
- res = responseList.FirstOrDefault()?.ParamPropertyList?.FirstOrDefault();
- }
- else
- {
- res = responseList.FirstOrDefault();
- }
- if (res?.ParamPropertyList?.Count > 0)
- {
- foreach (var responseItem in res.ParamPropertyList)
- {
- var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
- var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
- if (childTypeStr == "Object")
- {
- retract++;
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":{";
- }
- else if (childTypeStr == "Array")
- {
- retract++;
- if (generalTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[{";
- }
- else if (generalTypeStr == "Array")
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[[{";
- }
- else
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[";
- resultStr += "\n" + retractStr + "\t" + responseItem.DefaultValue + " //" + responseItem.Desc;
- }
- }
- else
- {
- if (typeList.Contains(childTypeStr) || childTypeStr.Contains("Enum"))
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": " + responseItem.DefaultValue + ", //" + responseItem.Desc;
- }
- else
- {
- resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": \"" + responseItem.DefaultValue + "\", //" + responseItem.Desc;
- }
- }
- if (responseItem.ParamPropertyList?.Count > 0)
- {
- var childStr = DealWithString(GetChildParameterString(childTypeStr, responseItem.ParamPropertyList, 1, retract));
- resultStr += childStr;
- }
- if (childTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "},";
- }
- else if (childTypeStr == "Array")
- {
- if (generalTypeStr == "Object")
- {
- resultStr += "\n" + retractStr + "}],";
- }
- else if (generalTypeStr == "Array")
- {
- resultStr += "\n" + retractStr + "}]],";
- }
- else
- {
- resultStr += "\n" + retractStr + "],";
- }
- }
- else
- {
- }
- }
- }
- }
- return resultStr;
- }
- public ExceptionResultEntity GetExceptionResultEnum(string enumNamespace)
- {
- var resultEntity = new ExceptionResultEntity();
- List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
- var enumInfo = memberList.Find(c => c.ParamName.Contains("T:WingInterfaceLibrary.Enum.CustomerRpcCode") && !string.IsNullOrEmpty(c.Remarks));
- if (enumInfo != null)
- {
- var key = enumInfo.ParamName.Replace("T:", "");
- resultEntity.title = enumInfo.Summary;
- resultEntity.name = enumInfo.Summary;
- resultEntity.url = "/" + FileName + "#" + PublicEnumDic[key];
- resultEntity.header = enumInfo.Remarks.Split('|').ToArray().Select(c => c.Trim()).ToList();
- if (enumNamespace.Split(',').Length > 0)
- {
- var enumList = memberList.FindAll(c => c.ParamName.Contains("F:WingInterfaceLibrary.Enum.CustomerRpcCode"));
- for (var i = 0; i < enumNamespace.Split(',').Length; i++)
- {
- var item = enumNamespace.Split(',')[i];
- if (!string.IsNullOrEmpty(item) && item.Split('|').Length > 2)
- {
- var ece = new ExceptionContentEntity();
- ece.content_data = item.Split('|').ToArray().Select(c => c.Trim()).ToList();
- resultEntity.content.Add(ece);
- }
- else if(!string.IsNullOrEmpty(item))
- {
- foreach (var enumItem in enumList)
- {
- if (enumItem.ParamName.Equals("F:WingInterfaceLibrary.Enum.CustomerRpcCode." + item))
- {
- if (!string.IsNullOrEmpty(enumItem.Summary))
- {
- var bodyList = enumItem.Summary.Split('|');
- if (bodyList?.Length > 2)
- {
- var ece = new ExceptionContentEntity();
- ece.content_data = bodyList.ToArray().Select(c => c.Trim()).ToList();
- resultEntity.content.Add(ece);
- }
- }
- }
- }
- }
- }
- }
- }
- return resultEntity;
- }
- /// <summary>
- /// 加载指定枚举注释到文档中
- /// </summary>
- /// <param name="enumNamespace">枚举的命名空间</param>
- /// <param name="local_target_id">上级id</param>
- /// <param name="formatterDesc">枚举展示格式标题</param>
- /// <param name="index">顺序</param>
- /// <returns>封装好的实体</returns>
- public ChildrenEntity GetSpecialEnum(string enumNamespace, string local_target_id, string formatterDesc = "代码 | 错误描述", int index = 0)
- {
- var strTable = "";
- var enumName = string.Empty;
- List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
- //获取错误枚举、上传文件类型枚举
- var enumList = memberList.FindAll(c => c.ParamName.Contains(enumNamespace));
- if (enumList?.Count > 0)
- {
- strTable = "<table>";
- if (!string.IsNullOrEmpty(formatterDesc))
- {
- var headerList = formatterDesc.Split('|');
- if (headerList?.Length > 1)
- {
- strTable += "<thead><tr>";
- foreach (var headItem in headerList)
- {
- strTable += "<th>" + headItem.Trim() + "</th>";
- }
- strTable += "</tr></thead>";
- }
- }
- strTable += "<tbody>";
- foreach (var item in enumList)
- {
- if (item.ParamName.Equals("T:" + enumNamespace))
- {
- enumName = item.Summary;
- }
- else
- {
- if (!string.IsNullOrEmpty(item.Summary))
- {
- var bodyList = item.Summary.Split('|');
- if (bodyList?.Length > 1)
- {
- strTable += "<tr>";
- foreach (var bodyItem in bodyList)
- {
- strTable += "<td>" + bodyItem.Trim() + "</td>";
- }
- strTable += "</tr>";
- }
- }
- }
- }
- strTable += "</tbody></table>";
- }
- var errorEntity = new ChildrenEntity()
- {
- is_doc = "0",
- local_parent_id = local_target_id,
- local_target_id = Guid.NewGuid().ToString(),
- mark = "complated",
- method = "POST",
- name = enumName,
- sort = index,
- status = 1,
- target_id = CommonHelper.GetSpecicalTargetIndex(),
- target_type = "doc",
- update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- response = new ResponseEntity()
- {
- error = new ResponseResultEntity()
- {
- parameter = new List<ResponseParameterEntity>(),
- raw = ""
- },
- success = new ResponseResultEntity()
- {
- raw = "",
- parameter = new List<ResponseParameterEntity>()
- }
- },
- request = new RequestEntity()
- {
- url = "",
- requestId = Guid.NewGuid().ToString(),
- resful = new RequestParameterEntity(),
- query = new RequestParameterEntity(),
- header = new RequestParameterEntity(),
- Event = new ScriptEntity(),
- //description = formatterDesc + "\n--- | ---" + errorEnumStr,
- description = strTable,
- cookie = new RequestParameterEntity(),
- body = new RequestBodyEntity()
- {
- mode = "form-data",
- parameter = new List<ResponseParameterEntity>(),
- raw = "",
- raw_para = new List<ResponseParameterEntity>()
- }
- }
- };
- if (!PublicEnumDic.Keys.Contains(enumNamespace))
- {
- PublicEnumDic.Add(enumNamespace, errorEntity.local_target_id);
- }
- else
- {
- PublicEnumDic[enumNamespace] = errorEntity.local_target_id;
- }
- return errorEntity;
- }
- }
- }
|