CreateDocHtml.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. using DocTools.Entity;
  2. using DocTools.Entity.Doc;
  3. using DocTools.Helper;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace DocTools
  9. {
  10. public class CreateDocHtml
  11. {
  12. private static Dictionary<string, string> PublicEnumDic = new Dictionary<string, string>();
  13. private static string FileName = "demoHtml.html";
  14. /// <summary>
  15. /// 创建最基础json信息
  16. /// </summary>
  17. /// <returns></returns>
  18. public ResultEntity CreateDocProjectString(string version = "", string fileName = "")
  19. {
  20. if (!string.IsNullOrEmpty(fileName))
  21. {
  22. FileName = fileName;
  23. }
  24. var result = new ResultEntity()
  25. {
  26. create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  27. id = "216603",
  28. name = "新版杏聆荟RPC接口文档",
  29. project_id = "2116053",
  30. project_name = "新版杏聆荟RPC接口文档" + version,
  31. publisher = "mr_kazuta",
  32. target_id = "2116053",
  33. target_type = "project"
  34. };
  35. var projectEntity = new ProjectEntity();
  36. var envItem = new EnvEntity()
  37. {
  38. env_id = "-1",
  39. list = new NullParameterEntity(),
  40. name = "默认环境"
  41. };
  42. projectEntity.envs.Add(envItem);
  43. var respParam = new ResponseParameterEntity()
  44. {
  45. description = "",
  46. is_checked = 1,
  47. key = "",
  48. type = "Text",
  49. value = ""
  50. };
  51. var varsItem = new VarsEntity()
  52. {
  53. variable = new List<NullParameterEntity>(),
  54. script = new ScriptEntity(),
  55. methods = new List<string>() { "POST", "GET", "PUT", "PATCH", "DELETE", "COPY", "HEAD", "OPTIONS", "LINK", "UNLINK", "PURGE", "LOCK", "UNLOCK", "PROPFIND", "VIEW" },
  56. mark = new List<MarkEntity>() {
  57. new MarkEntity() { key = "developing", name = "开发中" },
  58. new MarkEntity() { key = "complated", name = "已完成" },
  59. new MarkEntity() { key = "modifying", name = "需修改" },
  60. },
  61. request = new VarsRequestEntity()
  62. {
  63. body = new List<ResponseParameterEntity>() { respParam },
  64. cookie = new List<ResponseParameterEntity>() { respParam },
  65. header = new List<ResponseParameterEntity>() { respParam },
  66. query = new List<ResponseParameterEntity>() { respParam },
  67. }
  68. };
  69. projectEntity.vars = varsItem;
  70. result.project = projectEntity;
  71. result.targets = GetTargetsFolderString();
  72. return result;
  73. }
  74. /// <summary>
  75. /// 创建文件夹
  76. /// </summary>
  77. /// <returns></returns>
  78. public List<FolderChildrenEntity> GetTargetsFolderString()
  79. {
  80. var resultList = new List<FolderChildrenEntity>();
  81. List<APIModuleEntity> apiModuleList = LoadData.PackageInterfaceMemberInfo() ?? new List<APIModuleEntity>();
  82. foreach (var item in apiModuleList)
  83. {
  84. if (item.Example == "客户端API")
  85. {
  86. item.CreateTime = DateTime.Now.AddYears(100);
  87. }
  88. if (item.Example == "设备端API")
  89. {
  90. item.CreateTime = DateTime.Now.AddYears(50);
  91. }
  92. }
  93. apiModuleList = apiModuleList.OrderByDescending(c => c.CreateTime).ToList();
  94. for (int i = 0; i < apiModuleList.Count; i++)
  95. {
  96. var customerAPI = new FolderChildrenEntity();
  97. customerAPI.is_doc = "0";
  98. customerAPI.local_parent_id = "0";
  99. customerAPI.local_target_id = Guid.NewGuid().ToString();
  100. customerAPI.mark = "complated";
  101. customerAPI.method = "POST";
  102. customerAPI.name = apiModuleList[i].Example;
  103. customerAPI.script = new ScriptEntity();
  104. customerAPI.sort = 7 + i;
  105. customerAPI.status = 1;
  106. customerAPI.target_id = CommonHelper.GetSpecicalTargetIndex();
  107. customerAPI.target_type = "folder";
  108. customerAPI.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  109. customerAPI.request = new FoldRequestEntity()
  110. {
  111. body = new List<NullParameterEntity>(),
  112. header = new List<NullParameterEntity>(),
  113. query = new List<NullParameterEntity>(),
  114. //description = "新版杏聆荟RPC接口文档"
  115. description = null
  116. };
  117. customerAPI.children = GetCustomerChildrenList(customerAPI, apiModuleList[i]);
  118. resultList.Add(customerAPI);
  119. }
  120. return resultList;
  121. }
  122. private string CreateFolderHtml(Dictionary<string, string> dic,string apiName)
  123. {
  124. string allHtml = "";
  125. if (dic?.Count > 0)
  126. {
  127. 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>";
  128. var tableHtml = "<table><tbody><tr><th>服务名</th><th>查看详情</th></tr>";
  129. foreach (var key in dic.Keys)
  130. {
  131. tableHtml += "<tr><td>" + key + "</td><td><a target=\"_blank\" style=\"text-decoration: underline;color: blue;\" href=\"/" + FileName + "#" + dic[key] + "\">详情</a></td></tr>";
  132. }
  133. tableHtml += "</tbody></table>";
  134. allHtml += tableHtml + "</div>";
  135. }
  136. return allHtml;
  137. }
  138. /// <summary>
  139. /// 客户端api接口
  140. /// </summary>
  141. /// <returns></returns>
  142. public List<object> GetCustomerChildrenList(FolderChildrenEntity customerAPI, APIModuleEntity apiModuleEntity)
  143. {
  144. var dic = new Dictionary<string, string>();
  145. var list = new List<object>();
  146. if (apiModuleEntity.IsShowEnumFolder)
  147. {
  148. var dicEnum = new Dictionary<string, string>();
  149. FolderChildrenEntity enumFolderEntity = new FolderChildrenEntity();
  150. enumFolderEntity.is_doc = "0";
  151. enumFolderEntity.local_parent_id = customerAPI.local_target_id;
  152. enumFolderEntity.local_target_id = Guid.NewGuid().ToString();
  153. enumFolderEntity.mark = "complated";
  154. enumFolderEntity.method = "POST";
  155. enumFolderEntity.name = "枚举代码";
  156. enumFolderEntity.sort = 17;
  157. enumFolderEntity.status = 1;
  158. enumFolderEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
  159. enumFolderEntity.target_type = "folder";
  160. enumFolderEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  161. enumFolderEntity.script = new ScriptEntity();
  162. enumFolderEntity.request = new FoldRequestEntity();
  163. List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
  164. var enumList = memberList.FindAll(c => c.ParamName.Contains("T:WingInterfaceLibrary.Enum.") && !string.IsNullOrEmpty(c.Remarks));
  165. for (int i = 0; i < enumList.Count; i++)
  166. {
  167. var item = enumList[i];
  168. if (item.ParamName.Contains("WingInterfaceLibrary.Enum.RpcExceptionEnum."))
  169. {
  170. continue;
  171. }
  172. var enumEntity = GetSpecialEnum(item.ParamName.Replace("T:", ""), enumFolderEntity.local_target_id, item.Remarks, i);
  173. enumFolderEntity.children.Add(enumEntity);
  174. dicEnum.Add(enumEntity.name, enumEntity.local_target_id);
  175. }
  176. enumFolderEntity.tableHtml = CreateFolderHtml(dicEnum, enumFolderEntity.name);
  177. list.Add(enumFolderEntity);
  178. dic.Add(enumFolderEntity.name, enumFolderEntity.local_target_id);
  179. }
  180. //定义api下属文件夹
  181. //这里可以是多个文件夹
  182. List<InterfaceInfoEntity> interfaceList = apiModuleEntity.MainModelList;
  183. for (int i = 0; i < interfaceList.Count; i++)
  184. {
  185. var interfaceItem = interfaceList[i];
  186. FolderChildrenEntity folderEntity = new FolderChildrenEntity();
  187. folderEntity.is_doc = "0";
  188. folderEntity.local_parent_id = customerAPI.local_target_id;
  189. folderEntity.local_target_id = Guid.NewGuid().ToString();
  190. folderEntity.mark = "complated";
  191. folderEntity.method = "POST";
  192. folderEntity.name = interfaceItem.Value;
  193. folderEntity.sort = i + 1 + customerAPI.sort;
  194. folderEntity.status = 1;
  195. folderEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
  196. folderEntity.target_type = "folder";
  197. folderEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  198. folderEntity.script = new ScriptEntity();
  199. folderEntity.request = new FoldRequestEntity();
  200. folderEntity.children = GetInterfaceChildrenList(folderEntity, interfaceItem.Url, interfaceItem.InterfaceList);
  201. list.Add(folderEntity);
  202. dic.Add(folderEntity.name, folderEntity.local_target_id);
  203. }
  204. customerAPI.tableHtml = CreateFolderHtml(dic, customerAPI.name);
  205. return list;
  206. }
  207. /// <summary>
  208. /// 获取分类下的接口
  209. /// </summary>
  210. /// <returns></returns>
  211. public List<object> GetInterfaceChildrenList(FolderChildrenEntity folderEntity, string url, List<InterfaceBasicInfoEntity> interfaceList)
  212. {
  213. var dicInterface = new Dictionary<string, string>();
  214. var exclusiveList = new List<string>() { "IClientLogService", "IEmailService", "ISMSService" };
  215. List<object> resultList = new List<object>();
  216. for (int i = 0; i < interfaceList.Count; i++)
  217. {
  218. var dicMethod = new Dictionary<string, string>();
  219. var interfaceEntity = interfaceList[i];
  220. if (exclusiveList.Contains(interfaceEntity.InterfaceName))
  221. {
  222. continue;
  223. }
  224. FolderChildrenEntity folderChildEntity = new FolderChildrenEntity();
  225. folderChildEntity.index = 4;
  226. folderChildEntity.is_doc = "0";
  227. folderChildEntity.local_parent_id = folderEntity.local_target_id;
  228. folderChildEntity.local_target_id = Guid.NewGuid().ToString();
  229. folderChildEntity.mark = "complated";
  230. folderChildEntity.method = "POST";
  231. folderChildEntity.name = interfaceEntity.InterfaceSummary;
  232. folderChildEntity.sort = Convert.ToInt32(folderEntity.sort.ToString() + i);
  233. folderChildEntity.status = 1;
  234. folderChildEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
  235. folderChildEntity.target_type = "folder";
  236. folderChildEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  237. folderChildEntity.script = new ScriptEntity();
  238. folderChildEntity.request = new FoldRequestEntity();
  239. folderChildEntity.children = new List<object>();
  240. for (int j = 0; j < interfaceEntity.MethodList.Count; j++)
  241. {
  242. var methodEntity = interfaceEntity.MethodList[j];
  243. ChildrenEntity childEntity = new ChildrenEntity();
  244. childEntity.is_doc = "0";
  245. childEntity.index = 5;
  246. childEntity.local_parent_id = folderChildEntity.local_target_id;
  247. childEntity.local_target_id = Guid.NewGuid().ToString();
  248. childEntity.mark = "complated";
  249. childEntity.method = methodEntity.SubmitType;
  250. childEntity.name = methodEntity.Summary;
  251. childEntity.technology = methodEntity.MethodName;
  252. childEntity.sort = Convert.ToInt32(folderChildEntity.sort.ToString() + j);
  253. childEntity.status = 1;
  254. childEntity.target_id = CommonHelper.GetSpecicalTargetIndex();
  255. childEntity.target_type = "api";
  256. childEntity.update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  257. if (methodEntity.SubmitType == "PUT")
  258. {
  259. var responseEntity = new ResponseEntity()
  260. {
  261. error = new ResponseResultEntity()
  262. {
  263. raw = "{\n\t\"IsSuccess\": \"False\", //是否成功: True - 表示成功,False - 表示失败\n\t\"Msg\": \"\" //错误信息内容\n}",
  264. parameter = new List<ResponseParameterEntity>()
  265. {
  266. new ResponseParameterEntity()
  267. {
  268. description = "是否成功: True-表示成功,False-表示失败",
  269. is_checked = 1,
  270. key = "IsSuccess",
  271. type = "Text",
  272. field_type = "Boolean",
  273. value = "False"
  274. },
  275. new ResponseParameterEntity()
  276. {
  277. description= "错误信息内容",
  278. is_checked = 1,
  279. key = "Msg",
  280. type = "Text",
  281. field_type = "String",
  282. value = "Upload Fail"
  283. }
  284. }
  285. },
  286. success = new ResponseResultEntity()
  287. {
  288. raw = "{\n\t\"IsSuccess\": \"True\", //是否成功: True-表示成功,False-表示失败\n\t\"Msg\": \"\" //错误信息,如果成功,则为空\n}",
  289. parameter = new List<ResponseParameterEntity>()
  290. {
  291. new ResponseParameterEntity()
  292. {
  293. description = "是否成功: True-表示成功,False-表示失败",
  294. is_checked = 1,
  295. key = "IsSuccess",
  296. type = "Text",
  297. field_type = "Boolean",
  298. value = "True"
  299. },
  300. new ResponseParameterEntity()
  301. {
  302. description= "错误信息,如果成功,则为空",
  303. is_checked = 1,
  304. key = "Msg",
  305. type = "Text",
  306. field_type = "String",
  307. value = ""
  308. }
  309. }
  310. }
  311. };
  312. var fileInfo = methodEntity.RequestList?.FirstOrDefault()?.ParamPropertyList?.Find(c => c.Name == "File") ?? new ParamPropertyEntity();
  313. var dafultFieldType = CommonHelper.GetJsonDataTypeByString(fileInfo.TypeFullName);
  314. var requestBodyEntity = new RequestBodyEntity();
  315. requestBodyEntity = new RequestBodyEntity()
  316. {
  317. mode = "byte",
  318. parameter = new List<ResponseParameterEntity>()
  319. {
  320. new ResponseParameterEntity()
  321. {
  322. description = fileInfo.Desc,
  323. is_checked = 1,
  324. key = fileInfo.Name,
  325. type = dafultFieldType,
  326. not_null = "1",
  327. field_type = dafultFieldType,
  328. value = fileInfo.DefaultValue
  329. }
  330. },
  331. raw = "",
  332. raw_para = new List<ResponseParameterEntity>()
  333. };
  334. var headerParameterRequest = new List<object>();
  335. var headerParamList = methodEntity.RequestList?.FirstOrDefault()?.ParamPropertyList?.FindAll(c => c.Name != "File") ?? new List<ParamPropertyEntity>();
  336. if (headerParamList?.Count > 0)
  337. {
  338. foreach (var headerItem in headerParamList)
  339. {
  340. headerParameterRequest.Add(new ResponseParameterEntity()
  341. {
  342. description = headerItem.Desc,
  343. is_checked = 1,
  344. key = headerItem.Name,
  345. type = "Text",
  346. not_null = "1",
  347. field_type = headerItem.TypeFullName,
  348. value = headerItem.DefaultValue
  349. });
  350. }
  351. }
  352. var headerRequest = new
  353. {
  354. parameter = headerParameterRequest
  355. };
  356. childEntity.request = new RequestEntity()
  357. {
  358. url = url + "文件名【此请求地址为动态地址,实际值为上传文件权鉴接口返回的StorageUrl字段的值】",
  359. resful = new RequestParameterEntity(),
  360. query = new RequestParameterEntity(),
  361. header = headerRequest,
  362. Event = new ScriptEntity(),
  363. description = "",
  364. cookie = new RequestParameterEntity(),
  365. body = requestBodyEntity
  366. };
  367. childEntity.response = responseEntity;
  368. }
  369. else
  370. {
  371. var errorParameter = new ResponseResultEntity()
  372. {
  373. 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}",
  374. parameter = new List<ResponseParameterEntity>()
  375. {
  376. new ResponseParameterEntity()
  377. {
  378. description = "rpc版本",
  379. is_checked = 1,
  380. key = "jsonrpc",
  381. type = "Text",
  382. field_type = "Number",
  383. value = "2.0"
  384. },
  385. new ResponseParameterEntity()
  386. {
  387. description = "结果Id",
  388. is_checked = 1,
  389. key = "id",
  390. type = "Text",
  391. field_type = "Number",
  392. value = "1"
  393. },
  394. new ResponseParameterEntity()
  395. {
  396. description = "错误响应",
  397. is_checked = 1,
  398. key = "error",
  399. type = "Text",
  400. field_type = "Object",
  401. value = ""
  402. },
  403. new ResponseParameterEntity()
  404. {
  405. description = "错误码",
  406. is_checked = 1,
  407. key = "error.code",
  408. type = "Text",
  409. field_type = "Number",
  410. value = "1002"
  411. },
  412. new ResponseParameterEntity()
  413. {
  414. description = "错误信息",
  415. is_checked = 1,
  416. key = "error.message",
  417. type = "Text",
  418. field_type = "String",
  419. value = "Permission validation error"
  420. }
  421. }
  422. };
  423. var successParameter = new ResponseResultEntity()
  424. {
  425. parameter = new List<ResponseParameterEntity>()
  426. {
  427. new ResponseParameterEntity()
  428. {
  429. description = "rpc版本",
  430. is_checked = 1,
  431. key = "jsonrpc",
  432. type = "Text",
  433. field_type = "Number",
  434. value = "2.0"
  435. },
  436. new ResponseParameterEntity()
  437. {
  438. description = "结果Id",
  439. is_checked = 1,
  440. key = "id",
  441. type = "Text",
  442. field_type = "Number",
  443. value = "1"
  444. }
  445. },
  446. error_result = new List<ExceptionResultEntity>()
  447. };
  448. //计算响应列表枚举
  449. if (!string.IsNullOrEmpty(methodEntity.ErrorCodes))
  450. {
  451. var gere = GetExceptionResultEnum(methodEntity.ErrorCodes);
  452. successParameter.error_result.Add(gere);
  453. }
  454. var rawResultStr = "";
  455. if (methodEntity.ResponseList?.Count > 0)
  456. {
  457. var returnTypeStr = CommonHelper.GetJsonDataTypeByString(methodEntity.ResponseList.FirstOrDefault().TypeFullName);
  458. var resultObject = new ResponseParameterEntity()
  459. {
  460. description = "结果集",
  461. is_checked = 1,
  462. key = "result",
  463. type = "Text",
  464. field_type = returnTypeStr,
  465. value = ((returnTypeStr != "Object" && returnTypeStr != "Array") ? methodEntity.ResponseList.FirstOrDefault().DefaultValue : "")
  466. };
  467. successParameter.parameter.Add(resultObject);
  468. var rList = GetChildParameterEntity(returnTypeStr, methodEntity.ResponseList, "result");
  469. if (rList?.Count > 0)
  470. {
  471. rawResultStr += returnTypeStr == "Array" ? "[" : "";
  472. successParameter.parameter.AddRange(rList);
  473. rawResultStr += "{";
  474. rawResultStr += DealWithString(GetChildParameterString(returnTypeStr, methodEntity.ResponseList));
  475. //foreach (var rItem in rList)
  476. //{
  477. // rawResultStr += "\n\t\t\""+ rItem.key.Replace("result[0].","").Replace("result.", "") + "\": \""+ rItem.value + "\",";
  478. //}
  479. rawResultStr = rawResultStr.TrimEnd(',');
  480. rawResultStr += "\n\t}";
  481. rawResultStr += returnTypeStr == "Array" ? "]" : "";
  482. }
  483. else
  484. {
  485. var firstResponseEntity = methodEntity.ResponseList?.FirstOrDefault() ?? new ParamPropertyEntity();
  486. if (firstResponseEntity.TypeFullName.Contains("System.Collections.Generic.List"))
  487. {
  488. var firstParamEntity = firstResponseEntity.ParamPropertyList.FirstOrDefault() ?? new ParamPropertyEntity();
  489. if (firstParamEntity.TypeFullName == "System.String" || firstParamEntity.TypeFullName == "System.Int32" || firstParamEntity.TypeFullName == "System.Decimal" || firstParamEntity.TypeFullName == "System.Int16"
  490. || firstParamEntity.TypeFullName == "System.Long" || firstParamEntity.TypeFullName == "System.Int64" || firstParamEntity.TypeFullName == "System.Float" || firstParamEntity.TypeFullName == "System.Double"
  491. || firstParamEntity.TypeFullName == "System.Boolean")
  492. {
  493. rawResultStr = "["+ firstResponseEntity.Desc + "]";
  494. }
  495. else
  496. {
  497. rawResultStr = firstResponseEntity.DefaultValue;
  498. }
  499. }
  500. else
  501. {
  502. rawResultStr = firstResponseEntity.DefaultValue;
  503. }
  504. }
  505. successParameter.raw = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"id\": 1, //结果Id\n\t\"result\": " + rawResultStr + "\n}";
  506. }
  507. else
  508. {
  509. rawResultStr = methodEntity.Returns;
  510. successParameter.raw = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"id\": 1, //结果Id\n\t\"result\": " + rawResultStr + "\n}";
  511. }
  512. childEntity.response = new ResponseEntity()
  513. {
  514. error = errorParameter,
  515. success = successParameter
  516. };
  517. string rawStr = "";
  518. var requestRawPara = new List<ResponseParameterEntity>()
  519. {
  520. new ResponseParameterEntity()
  521. {
  522. description = "rpc版本",
  523. is_checked = 1,
  524. key = "jsonrpc",
  525. not_null = "1",
  526. type = "Text",
  527. field_type = "Number",
  528. value = "2.0"
  529. },
  530. new ResponseParameterEntity()
  531. {
  532. description = "结果Id",
  533. is_checked = 1,
  534. key = "id",
  535. not_null = "1",
  536. type = "Text",
  537. field_type = "Number",
  538. value = "1"
  539. },
  540. new ResponseParameterEntity()
  541. {
  542. description = "rpc接口名",
  543. is_checked = 1,
  544. key = "method",
  545. not_null = "1",
  546. type = "Text",
  547. field_type = "String",
  548. value = methodEntity.MethodName
  549. }
  550. };
  551. var paramTypeStr = CommonHelper.GetJsonDataTypeByString(methodEntity.ParamTypeFullName);
  552. var paramObject = new ResponseParameterEntity()
  553. {
  554. description = methodEntity.RequestList?.FirstOrDefault()?.Desc,
  555. is_checked = 1,
  556. key = "params",
  557. not_null = "1",
  558. type = "Text",
  559. field_type = "Array",
  560. value = ""//methodEntity.ParamText
  561. };
  562. requestRawPara.Add(paramObject);
  563. if (methodEntity.RequestList?.Count > 0)
  564. {
  565. var rList = GetChildParameterEntity(paramTypeStr, methodEntity.RequestList, "params");
  566. var rawRequestParamStr = "";
  567. if (rList?.Count > 0)
  568. {
  569. requestRawPara.AddRange(rList);
  570. rawRequestParamStr += "{";
  571. rawRequestParamStr += DealWithString(GetChildParameterString(paramTypeStr, methodEntity.RequestList));
  572. //foreach (var rItem in rList)
  573. //{
  574. // rawRequestParamStr += "\n\t\t\"" + rItem.key.Replace("params[0].", "") + "\": \"" + rItem.value + "\",";
  575. //}
  576. rawRequestParamStr = rawRequestParamStr.TrimEnd(',');
  577. rawRequestParamStr += "\n\t}";
  578. }
  579. rawStr = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"method\": \"" + methodEntity.MethodName + "\", //rpc接口名\n\t\"params\": [" + rawRequestParamStr + "],\n\t\"id\": 1 //结果Id\n}";
  580. }
  581. else
  582. {
  583. rawStr = "{\n\t\"jsonrpc\": \"2.0\", //rpc版本\n\t\"method\": \"" + methodEntity.MethodName + "\", //rpc接口名\n\t\"params\": [],\n\t\"id\": 1 //结果Id\n}";
  584. }
  585. var requestBodyEntity = new RequestBodyEntity()
  586. {
  587. mode = "json",
  588. parameter = new List<ResponseParameterEntity>(),
  589. raw = rawStr,
  590. raw_para = requestRawPara
  591. };
  592. childEntity.request = new RequestEntity()
  593. {
  594. url = url + interfaceEntity.InterfaceName,
  595. resful = new RequestParameterEntity(),
  596. query = new RequestParameterEntity(),
  597. header = new RequestParameterEntity(),
  598. Event = new ScriptEntity(),
  599. description = "",
  600. cookie = new RequestParameterEntity(),
  601. body = requestBodyEntity
  602. };
  603. }
  604. folderChildEntity.children.Add(childEntity);
  605. dicMethod.Add(childEntity.name, childEntity.local_target_id);
  606. }
  607. folderChildEntity.tableHtml = CreateFolderHtml(dicMethod, folderChildEntity.name);
  608. resultList.Add(folderChildEntity);
  609. dicInterface.Add(folderChildEntity.name, folderChildEntity.local_target_id);
  610. }
  611. folderEntity.tableHtml = CreateFolderHtml(dicInterface, folderEntity.name);
  612. return resultList;
  613. }
  614. private string DealWithString(string resultStr)
  615. {
  616. if (string.IsNullOrEmpty(resultStr))
  617. {
  618. return "";
  619. }
  620. string newStr = "";
  621. int lastIndex = resultStr.LastIndexOf(',');
  622. var lastStr = resultStr.Substring(lastIndex);
  623. if (!lastStr.Contains("\n"))
  624. {
  625. newStr = resultStr.Substring(0, lastIndex) + resultStr.Substring(lastIndex + 1);
  626. }
  627. return newStr;
  628. }
  629. public List<ResponseParameterEntity> GetChildParameterEntity(string returnTypeStr, List<ParamPropertyEntity> responseList, string typeStr, int level = 2)
  630. {
  631. List<ResponseParameterEntity> resultList = new List<ResponseParameterEntity>();
  632. if (returnTypeStr == "Object")
  633. {
  634. var res = responseList.FirstOrDefault();
  635. if (res.ParamPropertyList?.Count > 0)
  636. {
  637. foreach (var responseItem in res.ParamPropertyList)
  638. {
  639. var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
  640. var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
  641. var childObject = new ResponseParameterEntity()
  642. {
  643. file_name = FileName,
  644. description = responseItem.Desc,
  645. is_checked = 0,
  646. type = "Text",
  647. field_type = childTypeStr,
  648. value = responseItem.DefaultValue
  649. };
  650. //if (childTypeStr.Contains("Enum"))
  651. //{
  652. // childObject.field_type = "<a href=\"#"+ childTypeStr + "\">" + childObject.field_type + "</a>";
  653. //}
  654. if (childTypeStr.Contains("Enum"))
  655. {
  656. //var key = "WingInterfaceLibrary.Enum." + childTypeStr;
  657. var key = childTypeStr;
  658. if (PublicEnumDic.Keys.Contains(key))
  659. {
  660. childObject.field_type_value = 1;
  661. childObject.field_type_target_id = PublicEnumDic[key];
  662. }
  663. var arr = childTypeStr.Split('.');
  664. childObject.field_type = arr[arr.Length - 1];
  665. }
  666. if (childTypeStr == "Array" && generalTypeStr != "Object")
  667. {
  668. childObject.value = "[" + responseItem.DefaultValue + "]";
  669. }
  670. childObject.key = typeStr + "." + responseItem.Name;
  671. if (typeStr == "params")
  672. {
  673. childObject.key = typeStr + "[0]." + responseItem.Name;
  674. }
  675. if (typeStr != "result")
  676. {
  677. childObject.not_null = responseItem.IsNotNull;
  678. }
  679. resultList.Add(childObject);
  680. if (responseItem.ParamPropertyList?.Count > 0)
  681. {
  682. var childList = GetChildParameterEntity(childTypeStr, responseItem.ParamPropertyList, childObject.key, 1);
  683. resultList.AddRange(childList);
  684. }
  685. }
  686. }
  687. }
  688. else if (returnTypeStr == "Array")
  689. {
  690. ParamPropertyEntity res = null;
  691. if (level == 2)
  692. {
  693. res = responseList.FirstOrDefault()?.ParamPropertyList?.FirstOrDefault();
  694. }
  695. else
  696. {
  697. res = responseList.FirstOrDefault();
  698. }
  699. if (res?.ParamPropertyList?.Count > 0)
  700. {
  701. foreach (var responseItem in res.ParamPropertyList)
  702. {
  703. var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
  704. var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
  705. var childObject = new ResponseParameterEntity()
  706. {
  707. file_name = FileName,
  708. description = responseItem.Desc,
  709. is_checked = 0,
  710. type = "Text",
  711. field_type = childTypeStr,
  712. value = responseItem.DefaultValue
  713. };
  714. //if (childTypeStr.Contains("Enum"))
  715. //{
  716. // childObject.field_type = "<a href=\"#" + childTypeStr + "\">" + childObject.field_type + "</a>";
  717. //}
  718. if (childTypeStr.Contains("Enum"))
  719. {
  720. //var key = "WingInterfaceLibrary.Enum." + childTypeStr;
  721. var key = childTypeStr;
  722. if (PublicEnumDic.Keys.Contains(key))
  723. {
  724. childObject.field_type_value = 1;
  725. childObject.field_type_target_id = PublicEnumDic[key];
  726. }
  727. var arr = childTypeStr.Split('.');
  728. childObject.field_type = arr[arr.Length - 1];
  729. }
  730. if (childTypeStr == "Array" && generalTypeStr != "Object")
  731. {
  732. childObject.value = "[" + responseItem.DefaultValue + "]";
  733. }
  734. childObject.key = typeStr + "[0]." + responseItem.Name;
  735. if (typeStr != "result")
  736. {
  737. childObject.not_null = responseItem.IsNotNull;
  738. }
  739. resultList.Add(childObject);
  740. if (responseItem.ParamPropertyList?.Count > 0)
  741. {
  742. var childList = GetChildParameterEntity(childTypeStr, responseItem.ParamPropertyList, childObject.key, 1);
  743. resultList.AddRange(childList);
  744. }
  745. }
  746. }
  747. }
  748. else
  749. {
  750. }
  751. return resultList;
  752. }
  753. /// <summary>
  754. /// 拼接显示的字符串
  755. /// </summary>
  756. /// <param name="returnTypeStr">返回类型</param>
  757. /// <param name="responseList">响应列表</param>
  758. /// <param name="level">数据层级</param>
  759. /// <param name="retract">缩进大小</param>
  760. /// <returns></returns>
  761. public string GetChildParameterString(string returnTypeStr, List<ParamPropertyEntity> responseList, int level = 2, int retract = 0)
  762. {
  763. List<string> typeList = new List<string>() { "Boolean", "Double", "Float", "Number", "Integer" };
  764. string resultStr = "";
  765. string retractStr = "";
  766. for (int i = 0; i < (retract + 1); i++)
  767. {
  768. if (i == 0)
  769. {
  770. retractStr += "\t\t";
  771. }
  772. else
  773. {
  774. retractStr += "\t";
  775. }
  776. }
  777. if (returnTypeStr == "Object")
  778. {
  779. var res = responseList.FirstOrDefault();
  780. if (res.ParamPropertyList?.Count > 0)
  781. {
  782. foreach (var responseItem in res.ParamPropertyList)
  783. {
  784. var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
  785. var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
  786. if (childTypeStr == "Object")
  787. {
  788. retract++;
  789. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":{";
  790. }
  791. else if (childTypeStr == "Array")
  792. {
  793. retract++;
  794. if (generalTypeStr == "Object")
  795. {
  796. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[{";
  797. }
  798. else if (generalTypeStr == "Array")
  799. {
  800. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[[{";
  801. }
  802. else
  803. {
  804. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[";
  805. resultStr += "\n" + retractStr + "\t" + responseItem.DefaultValue + " //" + responseItem.Desc;
  806. }
  807. }
  808. else
  809. {
  810. if (typeList.Contains(childTypeStr) || childTypeStr.Contains("Enum"))
  811. {
  812. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": " + responseItem.DefaultValue + ", //" + responseItem.Desc;
  813. }
  814. else
  815. {
  816. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": \"" + responseItem.DefaultValue + "\", //" + responseItem.Desc;
  817. }
  818. }
  819. if (responseItem.ParamPropertyList?.Count > 0)
  820. {
  821. var childStr = DealWithString(GetChildParameterString(childTypeStr, responseItem.ParamPropertyList, 1, retract));
  822. resultStr += childStr;
  823. }
  824. if (childTypeStr == "Object")
  825. {
  826. resultStr += "\n" + retractStr + "},";
  827. }
  828. else if (childTypeStr == "Array")
  829. {
  830. if (generalTypeStr == "Object")
  831. {
  832. resultStr += "\n" + retractStr + "}],";
  833. }
  834. else if (generalTypeStr == "Array")
  835. {
  836. resultStr += "\n" + retractStr + "}]],";
  837. }
  838. else
  839. {
  840. resultStr += "\n" + retractStr + "],";
  841. }
  842. }
  843. else
  844. {
  845. }
  846. }
  847. }
  848. }
  849. else if (returnTypeStr == "Array")
  850. {
  851. ParamPropertyEntity res = null;
  852. if (level == 2)
  853. {
  854. res = responseList.FirstOrDefault()?.ParamPropertyList?.FirstOrDefault();
  855. }
  856. else
  857. {
  858. res = responseList.FirstOrDefault();
  859. }
  860. if (res?.ParamPropertyList?.Count > 0)
  861. {
  862. foreach (var responseItem in res.ParamPropertyList)
  863. {
  864. var childTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.TypeFullName);
  865. var generalTypeStr = CommonHelper.GetJsonDataTypeByString(responseItem.GenericityType);
  866. if (childTypeStr == "Object")
  867. {
  868. retract++;
  869. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":{";
  870. }
  871. else if (childTypeStr == "Array")
  872. {
  873. retract++;
  874. if (generalTypeStr == "Object")
  875. {
  876. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[{";
  877. }
  878. else if (generalTypeStr == "Array")
  879. {
  880. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[[{";
  881. }
  882. else
  883. {
  884. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\":[";
  885. resultStr += "\n" + retractStr + "\t" + responseItem.DefaultValue + " //" + responseItem.Desc;
  886. }
  887. }
  888. else
  889. {
  890. if (typeList.Contains(childTypeStr) || childTypeStr.Contains("Enum"))
  891. {
  892. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": " + responseItem.DefaultValue + ", //" + responseItem.Desc;
  893. }
  894. else
  895. {
  896. resultStr += "\n" + retractStr + "\"" + responseItem.Name + "\": \"" + responseItem.DefaultValue + "\", //" + responseItem.Desc;
  897. }
  898. }
  899. if (responseItem.ParamPropertyList?.Count > 0)
  900. {
  901. var childStr = DealWithString(GetChildParameterString(childTypeStr, responseItem.ParamPropertyList, 1, retract));
  902. resultStr += childStr;
  903. }
  904. if (childTypeStr == "Object")
  905. {
  906. resultStr += "\n" + retractStr + "},";
  907. }
  908. else if (childTypeStr == "Array")
  909. {
  910. if (generalTypeStr == "Object")
  911. {
  912. resultStr += "\n" + retractStr + "}],";
  913. }
  914. else if (generalTypeStr == "Array")
  915. {
  916. resultStr += "\n" + retractStr + "}]],";
  917. }
  918. else
  919. {
  920. resultStr += "\n" + retractStr + "],";
  921. }
  922. }
  923. else
  924. {
  925. }
  926. }
  927. }
  928. }
  929. return resultStr;
  930. }
  931. public ExceptionResultEntity GetExceptionResultEnum(string enumNamespace)
  932. {
  933. var resultEntity = new ExceptionResultEntity();
  934. List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
  935. var enumInfo = memberList.Find(c => c.ParamName.Contains("T:WingInterfaceLibrary.Enum.CustomerRpcCode") && !string.IsNullOrEmpty(c.Remarks));
  936. if (enumInfo != null)
  937. {
  938. var key = enumInfo.ParamName.Replace("T:", "");
  939. resultEntity.title = enumInfo.Summary;
  940. resultEntity.name = enumInfo.Summary;
  941. resultEntity.url = "/" + FileName + "#" + PublicEnumDic[key];
  942. resultEntity.header = enumInfo.Remarks.Split('|').ToArray().Select(c => c.Trim()).ToList();
  943. if (enumNamespace.Split(',').Length > 0)
  944. {
  945. var enumList = memberList.FindAll(c => c.ParamName.Contains("F:WingInterfaceLibrary.Enum.CustomerRpcCode"));
  946. for (var i = 0; i < enumNamespace.Split(',').Length; i++)
  947. {
  948. var item = enumNamespace.Split(',')[i];
  949. if (!string.IsNullOrEmpty(item) && item.Split('|').Length > 2)
  950. {
  951. var ece = new ExceptionContentEntity();
  952. ece.content_data = item.Split('|').ToArray().Select(c => c.Trim()).ToList();
  953. resultEntity.content.Add(ece);
  954. }
  955. else if(!string.IsNullOrEmpty(item))
  956. {
  957. foreach (var enumItem in enumList)
  958. {
  959. if (enumItem.ParamName.Equals("F:WingInterfaceLibrary.Enum.CustomerRpcCode." + item))
  960. {
  961. if (!string.IsNullOrEmpty(enumItem.Summary))
  962. {
  963. var bodyList = enumItem.Summary.Split('|');
  964. if (bodyList?.Length > 2)
  965. {
  966. var ece = new ExceptionContentEntity();
  967. ece.content_data = bodyList.ToArray().Select(c => c.Trim()).ToList();
  968. resultEntity.content.Add(ece);
  969. }
  970. }
  971. }
  972. }
  973. }
  974. }
  975. }
  976. }
  977. return resultEntity;
  978. }
  979. /// <summary>
  980. /// 加载指定枚举注释到文档中
  981. /// </summary>
  982. /// <param name="enumNamespace">枚举的命名空间</param>
  983. /// <param name="local_target_id">上级id</param>
  984. /// <param name="formatterDesc">枚举展示格式标题</param>
  985. /// <param name="index">顺序</param>
  986. /// <returns>封装好的实体</returns>
  987. public ChildrenEntity GetSpecialEnum(string enumNamespace, string local_target_id, string formatterDesc = "代码 | 错误描述", int index = 0)
  988. {
  989. var strTable = "";
  990. var enumName = string.Empty;
  991. List<MemberEntity> memberList = LoadData.GetInterfaceConfigToCache();
  992. //获取错误枚举、上传文件类型枚举
  993. var enumList = memberList.FindAll(c => c.ParamName.Contains(enumNamespace));
  994. if (enumList?.Count > 0)
  995. {
  996. strTable = "<table>";
  997. if (!string.IsNullOrEmpty(formatterDesc))
  998. {
  999. var headerList = formatterDesc.Split('|');
  1000. if (headerList?.Length > 1)
  1001. {
  1002. strTable += "<thead><tr>";
  1003. foreach (var headItem in headerList)
  1004. {
  1005. strTable += "<th>" + headItem.Trim() + "</th>";
  1006. }
  1007. strTable += "</tr></thead>";
  1008. }
  1009. }
  1010. strTable += "<tbody>";
  1011. foreach (var item in enumList)
  1012. {
  1013. if (item.ParamName.Equals("T:" + enumNamespace))
  1014. {
  1015. enumName = item.Summary;
  1016. }
  1017. else
  1018. {
  1019. if (!string.IsNullOrEmpty(item.Summary))
  1020. {
  1021. var bodyList = item.Summary.Split('|');
  1022. if (bodyList?.Length > 1)
  1023. {
  1024. strTable += "<tr>";
  1025. foreach (var bodyItem in bodyList)
  1026. {
  1027. strTable += "<td>" + bodyItem.Trim() + "</td>";
  1028. }
  1029. strTable += "</tr>";
  1030. }
  1031. }
  1032. }
  1033. }
  1034. strTable += "</tbody></table>";
  1035. }
  1036. var errorEntity = new ChildrenEntity()
  1037. {
  1038. is_doc = "0",
  1039. local_parent_id = local_target_id,
  1040. local_target_id = Guid.NewGuid().ToString(),
  1041. mark = "complated",
  1042. method = "POST",
  1043. name = enumName,
  1044. sort = index,
  1045. status = 1,
  1046. target_id = CommonHelper.GetSpecicalTargetIndex(),
  1047. target_type = "doc",
  1048. update_dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  1049. response = new ResponseEntity()
  1050. {
  1051. error = new ResponseResultEntity()
  1052. {
  1053. parameter = new List<ResponseParameterEntity>(),
  1054. raw = ""
  1055. },
  1056. success = new ResponseResultEntity()
  1057. {
  1058. raw = "",
  1059. parameter = new List<ResponseParameterEntity>()
  1060. }
  1061. },
  1062. request = new RequestEntity()
  1063. {
  1064. url = "",
  1065. requestId = Guid.NewGuid().ToString(),
  1066. resful = new RequestParameterEntity(),
  1067. query = new RequestParameterEntity(),
  1068. header = new RequestParameterEntity(),
  1069. Event = new ScriptEntity(),
  1070. //description = formatterDesc + "\n--- | ---" + errorEnumStr,
  1071. description = strTable,
  1072. cookie = new RequestParameterEntity(),
  1073. body = new RequestBodyEntity()
  1074. {
  1075. mode = "form-data",
  1076. parameter = new List<ResponseParameterEntity>(),
  1077. raw = "",
  1078. raw_para = new List<ResponseParameterEntity>()
  1079. }
  1080. }
  1081. };
  1082. if (!PublicEnumDic.Keys.Contains(enumNamespace))
  1083. {
  1084. PublicEnumDic.Add(enumNamespace, errorEntity.local_target_id);
  1085. }
  1086. else
  1087. {
  1088. PublicEnumDic[enumNamespace] = errorEntity.local_target_id;
  1089. }
  1090. return errorEntity;
  1091. }
  1092. }
  1093. }