ApiGroupService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using Attribute;
  2. using Model;
  3. using Model.Base;
  4. using Repository;
  5. using Service;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Vo.Admin;
  8. using Common;
  9. using Custom;
  10. using Tasks;
  11. using Infrastructure;
  12. using Dto.Admin;
  13. using Feign;
  14. using Util;
  15. using LitJson;
  16. using Mapster;
  17. namespace Services
  18. {
  19. /// <summary>
  20. /// 接口分组Service业务层处理
  21. /// </summary>
  22. [AppService(ServiceType = typeof(IApiGroupService), ServiceLifetime = LifeTime.Transient)]
  23. public class ApiGroupService : BaseService<ApiGroup>, IApiGroupService
  24. {
  25. private readonly IApiInfoService _ApiInfoService;
  26. public ApiGroupService(IApiInfoService ApiInfoService)
  27. {
  28. _ApiInfoService = ApiInfoService;
  29. }
  30. /// <summary>
  31. /// 接口分组-列表
  32. /// </summary>
  33. /// <param name="param">参数请求体</param>
  34. /// <param name="page">分页参数</param>
  35. /// <returns>列表</returns>
  36. public PagedInfo<GetApiGroupListVo> getApiGroupList([FromQuery] PagerInfo page, [FromQuery] ApiGroup param)
  37. {
  38. //拼装查询条件
  39. var predicate = Expressionable.Create<ApiGroup>();
  40. predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
  41. predicate = predicate.AndIF(!string.IsNullOrEmpty(param.groupName), m => m.groupName.Contains(param.groupName));
  42. predicate = predicate.AndIF(param.appId > 0, m => m.appId == param.appId);
  43. var response = Queryable()
  44. .Where(predicate.ToExpression())
  45. .OrderByDescending(m => m.id)
  46. .ToPage<ApiGroup, GetApiGroupListVo>(page);
  47. return response;
  48. }
  49. /// <summary>
  50. /// 获取接口树结构
  51. /// </summary>
  52. /// <param name="param">参数请求体</param>
  53. /// <returns>接口树结构</returns>
  54. public List<GetApiTreeVo> getApiTree(GetApiTreeDto param)
  55. {
  56. //构造返回体
  57. List<Dictionary<string, object>> outputObjects = new List<Dictionary<string, object>>();
  58. Dictionary<string, object> outputObject = new Dictionary<string, object>();
  59. outputObject.Add("label", "status");
  60. outputObject.Add("type", "number");
  61. outputObjects.Add(outputObject);
  62. outputObject = new Dictionary<string, object>();
  63. outputObject.Add("label", "msg");
  64. outputObject.Add("type", "string");
  65. outputObjects.Add(outputObject);
  66. outputObject = new Dictionary<string, object>();
  67. outputObject.Add("label", "data");
  68. outputObject.Add("type", "#$data$#");
  69. outputObjects.Add(outputObject);
  70. string outputJsonTmp = Newtonsoft.Json.JsonConvert.SerializeObject(outputObjects);
  71. List<GetApiTreeVo> treeList = new List<GetApiTreeVo>();
  72. var services = IProject.getProjectServiceList(param.projectId);
  73. foreach(var service in services)
  74. {
  75. var serviceItem = service.Adapt<GetApiTreeVo>();
  76. var apiTreeGroup = new List<ApiTreeGroup>();
  77. var apiGroups = GetList(m => m.serviceId == service.id);
  78. foreach(var apiGroup in apiGroups)
  79. {
  80. var groupItem = apiGroup.Adapt<ApiTreeGroup>();
  81. var apiTreeInfo = new List<ApiTreeInfo>();
  82. var apiInfos = _ApiInfoService.GetList(m => m.groupId == apiGroup.id);
  83. foreach(var apiInfo in apiInfos)
  84. {
  85. var apiItem = apiInfo.Adapt<ApiTreeInfo>();
  86. if(apiInfo.apiParam != null)
  87. {
  88. apiItem.requestParam = apiInfo.apiParam.Where(m => m.reqParam).ToList();
  89. apiItem.responseParam = apiInfo.apiParam.Where(m => m.resParam).ToList();
  90. }
  91. apiItem.apiPath = "/v1/" + groupItem.groupName + "/" + apiInfo.controllerNameUpper + "/" + apiInfo.methodName;
  92. apiItem.apiKey = apiInfo.controllerNameUpper + apiInfo.methodName;
  93. string outputJson = outputJsonTmp;
  94. if(apiInfo.apiKind == 1)
  95. {
  96. outputJson = outputJson.Replace("#$data$#", "array");
  97. }
  98. else
  99. {
  100. outputJson = outputJson.Replace("#$data$#", "object");
  101. }
  102. apiItem.outputObject = outputJson;
  103. apiTreeInfo.Add(apiItem);
  104. }
  105. groupItem.apiInfos = apiTreeInfo;
  106. apiTreeGroup.Add(groupItem);
  107. }
  108. serviceItem.apiGroups = apiTreeGroup;
  109. treeList.Add(serviceItem);
  110. }
  111. return treeList;
  112. }
  113. /// <summary>
  114. /// 获取接口组树结构
  115. /// </summary>
  116. /// <returns>接口组树结构</returns>
  117. public List<GetApiGroupTreeVo> getApiGroupTree()
  118. {
  119. List<GetApiGroupTreeVo> treeList = new List<GetApiGroupTreeVo>();
  120. var projects = IProject.getProjectList();
  121. foreach (var project in projects)
  122. {
  123. var projectItem = project.Adapt<GetApiGroupTreeVo>();
  124. projectItem.projectTitle = project.projectName;
  125. var serviceList = new List<GetApiGroupTreeVoServices>();
  126. var services = IProject.getProjectServiceList(project.id);
  127. foreach (var service in services)
  128. {
  129. var serviceItem = new GetApiGroupTreeVoServices();
  130. serviceItem.serviceTitle = service.serviceTitle;
  131. var apiTreeGroup = new List<GetApiGroupTreeVoGroups>();
  132. var apiGroups = GetList(m => m.serviceId == service.id);
  133. foreach (var apiGroup in apiGroups)
  134. {
  135. var groupItem = new GetApiGroupTreeVoGroups();
  136. groupItem.groupTitle = apiGroup.groupTitle;
  137. groupItem.groupId = apiGroup.id;
  138. apiTreeGroup.Add(groupItem);
  139. }
  140. serviceItem.apiGroups = apiTreeGroup;
  141. serviceList.Add(serviceItem);
  142. }
  143. projectItem.services = serviceList;
  144. treeList.Add(projectItem);
  145. }
  146. return treeList;
  147. }
  148. /// <summary>
  149. /// 生成api文档
  150. /// </summary>
  151. /// <param name="serviceId">服务id</param>
  152. public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser)
  153. {
  154. var tableService = App.GetService<IDatabaseTableService>();
  155. var service = Feign.IProject.getProjectService(param.serviceId);
  156. var apiGroups = GetList(m => m.serviceId == param.serviceId);
  157. var apiInfos = _ApiInfoService.GetList(m => m.serviceId == param.serviceId);
  158. if(param.groupId > 0)
  159. {
  160. apiGroups = apiGroups.Where(m => m.id == param.groupId).ToList();
  161. apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
  162. }
  163. foreach(var apiInfo in apiInfos)
  164. {
  165. apiInfo.groupTitle = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupTitle;
  166. apiInfo.groupName = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupName;
  167. var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
  168. apiInfo.linkTableTitle = linkTable.tableTitle;
  169. apiInfo.linkTableName = linkTable.tableName;
  170. }
  171. string id = service.id.ToString() + param.groupId.ToString();
  172. Dictionary<string, object> pushData = new Dictionary<string, object>();
  173. pushData.Add("serviceName", service.serviceName);
  174. pushData.Add("serviceTitle", service.serviceTitle);
  175. pushData.Add("apiDocId", id);
  176. pushData.Add("apiGroup", apiGroups);
  177. pushData.Add("apiInfo", apiInfos);
  178. MakeHelper.Push(pushData, "apifoxDoc", "apidoc", loginUser, id);
  179. var options = App.OptionsSetting;
  180. return new MakeApiDocVo()
  181. {
  182. docUrl = options.BaseConfigs.gatewayHost + "/v1/omega_make/StaticFile/apiDocQuery?id=" + id + "&fileType=apidoc"
  183. };
  184. }
  185. /// <summary>
  186. /// 同步api到管理平台
  187. /// </summary>
  188. /// <param name="serviceId">服务id</param>
  189. public string sycnApiToSystem(SycnApiToSystemDto param)
  190. {
  191. var apiGroupService = App.GetService<IApiGroupService>();
  192. var apiInfoService = App.GetService<IApiInfoService>();
  193. var tableService = App.GetService<IDatabaseTableService>();
  194. var proService = IProject.getProjectService(param.serviceId);
  195. var project = IProject.getProject(proService.projectId);
  196. string ApiListHost = "";
  197. string Hosts = "";
  198. if(param.env == "test")
  199. {
  200. ApiListHost = project.apiListNoticeHostDev ?? "";
  201. Hosts = project.gatewayHostDev ?? "";
  202. }
  203. else
  204. {
  205. ApiListHost = project.apiListNoticeHost ?? "";
  206. Hosts = project.gatewayHost ?? "";
  207. }
  208. // var projectService = IProject.getProjectService(param.serviceId);
  209. List<string> checkStrs = new List<string>();
  210. List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>();
  211. string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  212. int Id = 0;
  213. bool withPublicApi = false;
  214. int publicApiGroupId = 0;
  215. int serId = proService.id;
  216. string serviceTitle = proService.serviceTitle ?? "";
  217. string serviceName = proService.serviceName ?? "";
  218. string Language = proService.devLanguage ?? "";
  219. string[] HostList = Hosts.Split(':');
  220. string Host = HostList[0];
  221. string Port = "80";
  222. if(HostList.Length > 1)
  223. {
  224. Port = HostList[1];
  225. }
  226. var groups = GetList(m => m.serviceId == param.serviceId);
  227. foreach(var group in groups)
  228. {
  229. Id += 1;
  230. List<Dictionary<string, object>> child = new List<Dictionary<string, object>>();
  231. var apiInfos = apiInfoService.GetList(m => m.serviceId == serId);
  232. apiInfos = apiInfos.Where(m => m.groupId == group.id).ToList();
  233. foreach(var apiInfo in apiInfos)
  234. {
  235. var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
  236. string Gategory = apiInfo.controllerNameUpper ?? "";
  237. string Router = "";
  238. string apiHost = "";
  239. string apiPort = "";
  240. Router = group.groupName ?? "";
  241. if(param.env == "test")
  242. {
  243. apiHost = group.apiHostDev ?? "";
  244. }
  245. else
  246. {
  247. apiHost = group.apiHost ?? "";
  248. }
  249. if(apiHost.Contains(":"))
  250. {
  251. string[] apiHostList = apiHost.Split(':');
  252. apiHost = apiHostList[0];
  253. if(apiHostList.Length > 1)
  254. {
  255. apiPort = apiHostList[1];
  256. }
  257. }
  258. if(group.withPublicApi && withPublicApi == false)
  259. {
  260. publicApiGroupId = group.id;
  261. withPublicApi = true;
  262. string publicApiJson = Function.ReadInstance("public_api.json");
  263. JsonData publicApiList = JsonMapper.ToObject(publicApiJson);
  264. for(int i = 0; i < publicApiList.Count; i++)
  265. {
  266. JsonData publicApi = publicApiList[i];
  267. string publicApiRouter = publicApi["api_router"].ToString();
  268. string publicApiKey = publicApi["api_key"].ToString();
  269. string publicApiName = publicApi["api_name"].ToString();
  270. string publicApiMethod = publicApi["api_method"].ToString();
  271. Dictionary<string, object> publicItem = new Dictionary<string, object>();
  272. publicApiRouter = publicApiRouter.Replace("/lkb/", "/" + Router + "/");
  273. publicItem.Add("api_router", publicApiRouter);
  274. publicItem.Add("api_port", apiPort);
  275. if(apiPort == "443")
  276. {
  277. publicItem.Add("api_host", "https://" + apiHost);
  278. }
  279. else
  280. {
  281. publicItem.Add("api_host", "http://" + apiHost);
  282. }
  283. publicItem.Add("api_key", publicApiKey);
  284. publicItem.Add("api_name", publicApiName);
  285. publicItem.Add("api_method", publicApiMethod);
  286. publicItem.Add("api_path", "/" + Router + "/public_api/index");
  287. publicItem.Add("api_en_name", "sys");
  288. child.Add(publicItem);
  289. }
  290. }
  291. // if(!string.IsNullOrEmpty(apiHost)) apiHost = Host;
  292. // if(!string.IsNullOrEmpty(apiPort)) apiPort = Port;
  293. string MethodName = apiInfo.methodName ?? "";
  294. string TabName = apiInfo.tabName ?? "";
  295. string Title = apiInfo.apiName ?? "";
  296. string ApiType = apiInfo.apiKind.ToString();
  297. string Method = "get";
  298. string ApiKey = "";
  299. if(string.IsNullOrEmpty(ApiKey))
  300. {
  301. ApiKey = Gategory + MethodName;
  302. }
  303. if(ApiType == "3") Method = "post";
  304. if(ApiType == "4") Method = "put";
  305. if(ApiType == "5") Method = "delete";
  306. Dictionary<string, object> subItem = new Dictionary<string, object>();
  307. string ApiRouter = "/v1/" + Router + "/" + Gategory + "/" + MethodName;
  308. subItem.Add("api_router", ApiRouter);
  309. subItem.Add("api_port", apiPort);
  310. if(apiPort == "443")
  311. {
  312. subItem.Add("api_host", "https://" + apiHost);
  313. }
  314. else
  315. {
  316. subItem.Add("api_host", "http://" + apiHost);
  317. }
  318. subItem.Add("api_key", ApiKey);
  319. subItem.Add("api_name", TabName + "-" + Title);
  320. subItem.Add("api_method", Method);
  321. subItem.Add("api_path", "/" + Gategory + "/" + linkTable.tableNameUpper + "/index");
  322. subItem.Add("api_en_name", linkTable.tableNameUpper ?? "");
  323. child.Add(subItem);
  324. }
  325. Dictionary<string, object> item = new Dictionary<string, object>();
  326. item.Add("id", Id);
  327. item.Add("group_project", param.projectId);
  328. item.Add("group_kind", group.groupTitle.Contains("后台") ? 1 : 0);
  329. item.Add("group_remark", group.groupTitle ?? "");
  330. item.Add("group_name", PublicFunction.transferName(group.groupName ?? "", 2));
  331. item.Add("child", child);
  332. dic.Add(item);
  333. }
  334. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  335. string result = "";
  336. // if(project.encryptKind == "AES")
  337. // {
  338. // JsonData encryptParam = JsonMapper.ToObject(project.encryptParam ?? "{}");
  339. // string reqData = CryptHelper.AesEncrypt(req, encryptParam["encrypt_key"].ToString(), encryptParam["encrypt_iv"].ToString(), encryptParam["encrypt_model"].ToString(), encryptParam["encrypt_padding"].ToString());
  340. // result = Function.PostWebRequest(ApiListHost, reqData, "application/json");
  341. // JsonData jsonObj = JsonMapper.ToObject(result);
  342. // if(jsonObj["status"].ToString() == "1")
  343. // {
  344. // return "同步成功";
  345. // }
  346. // }
  347. // else
  348. // {
  349. result = Function.PostWebRequest(ApiListHost, "value=" + req);
  350. if(result == "success")
  351. {
  352. return "同步成功";
  353. }
  354. // }
  355. return "同步失败";
  356. }
  357. }
  358. }