ApiGroupService.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. namespace Services
  17. {
  18. /// <summary>
  19. /// 接口分组Service业务层处理
  20. /// </summary>
  21. [AppService(ServiceType = typeof(IApiGroupService), ServiceLifetime = LifeTime.Transient)]
  22. public class ApiGroupService : BaseService<ApiGroup>, IApiGroupService
  23. {
  24. private readonly IApiInfoService _ApiInfoService;
  25. public ApiGroupService(IApiInfoService ApiInfoService)
  26. {
  27. _ApiInfoService = ApiInfoService;
  28. }
  29. /// <summary>
  30. /// 接口分组-列表
  31. /// </summary>
  32. /// <param name="param">参数请求体</param>
  33. /// <param name="page">分页参数</param>
  34. /// <returns>列表</returns>
  35. public PagedInfo<GetApiGroupListVo> getApiGroupList([FromQuery] PagerInfo page, [FromQuery] ApiGroup param)
  36. {
  37. //拼装查询条件
  38. var predicate = Expressionable.Create<ApiGroup>();
  39. predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
  40. predicate = predicate.AndIF(!string.IsNullOrEmpty(param.groupName), m => m.groupName.Contains(param.groupName));
  41. predicate = predicate.AndIF(param.appId > 0, m => m.appId == param.appId);
  42. var response = Queryable()
  43. .Where(predicate.ToExpression())
  44. .OrderByDescending(m => m.id)
  45. .ToPage<ApiGroup, GetApiGroupListVo>(page);
  46. return response;
  47. }
  48. /// <summary>
  49. /// 生成api文档
  50. /// </summary>
  51. /// <param name="serviceId">服务id</param>
  52. public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser)
  53. {
  54. var service = Feign.IProject.getProjectService(param.serviceId);
  55. var apiGroups = GetList(m => m.serviceId == param.serviceId);
  56. var apiInfos = _ApiInfoService.GetList(m => m.serviceId == param.serviceId);
  57. if(param.groupId > 0)
  58. {
  59. apiGroups = apiGroups.Where(m => m.id == param.groupId).ToList();
  60. apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
  61. }
  62. foreach(var apiInfo in apiInfos)
  63. {
  64. apiInfo.groupTitle = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupTitle;
  65. apiInfo.groupName = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupName;
  66. }
  67. string id = service.id.ToString() + param.groupId.ToString();
  68. Dictionary<string, object> pushData = new Dictionary<string, object>();
  69. pushData.Add("serviceName", service.serviceName);
  70. pushData.Add("serviceTitle", service.serviceTitle);
  71. pushData.Add("apiDocId", id);
  72. pushData.Add("apiGroup", apiGroups);
  73. pushData.Add("apiInfo", apiInfos);
  74. MakeHelper.Push(pushData, "apifoxDoc", "apidoc", loginUser, id);
  75. var options = App.OptionsSetting;
  76. return new MakeApiDocVo()
  77. {
  78. docUrl = options.BaseConfigs.gatewayHost + "/v1/omega_make/StaticFile/apiDocQuery?id=" + id + "&fileType=apidoc"
  79. };
  80. }
  81. /// <summary>
  82. /// 同步api到管理平台
  83. /// </summary>
  84. /// <param name="serviceId">服务id</param>
  85. public string sycnApiToSystem(SycnApiToSystemDto param)
  86. {
  87. var apiGroupService = App.GetService<IApiGroupService>();
  88. var apiInfoService = App.GetService<IApiInfoService>();
  89. var tableService = App.GetService<IDatabaseTableService>();
  90. var project = IProject.getProject(param.projectId);
  91. string ApiListHost = "";
  92. string Hosts = "";
  93. if(param.env == "test")
  94. {
  95. ApiListHost = project.apiListNoticeHostDev ?? "";
  96. Hosts = project.gatewayHostDev ?? "";
  97. }
  98. else
  99. {
  100. ApiListHost = project.apiListNoticeHost ?? "";
  101. Hosts = project.gatewayHost ?? "";
  102. }
  103. var projectService = IProject.getProjectService(param.serviceId);
  104. List<string> checkStrs = new List<string>();
  105. List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>();
  106. string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  107. int Id = 0;
  108. var projectServiceList = IProject.getProjectServiceList(param.projectId);
  109. foreach(var proService in projectServiceList)
  110. {
  111. Id += 1;
  112. int serId = proService.id;
  113. string serviceTitle = proService.serviceTitle ?? "";
  114. string serviceName = proService.serviceName ?? "";
  115. string Language = proService.devLanguage ?? "";
  116. string[] HostList = Hosts.Split(':');
  117. string Host = HostList[0];
  118. string Port = "80";
  119. if(HostList.Length > 1)
  120. {
  121. Port = HostList[1];
  122. }
  123. List<Dictionary<string, object>> child = new List<Dictionary<string, object>>();
  124. var apiInfos = apiInfoService.GetList(m => m.serviceId == serId);
  125. if(param.serviceId > 0)
  126. {
  127. apiInfos = apiInfos.Where(m => m.serviceId == param.serviceId).ToList();
  128. }
  129. if(param.groupId > 0)
  130. {
  131. apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
  132. }
  133. foreach(var apiInfo in apiInfos)
  134. {
  135. var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
  136. string Gategory = apiInfo.controllerName ?? "";
  137. string Router = "";
  138. var group = apiGroupService.GetList(m => m.id == apiInfo.groupId);
  139. if (group.Count > 0)
  140. {
  141. Router = group[0].groupName ?? "";
  142. }
  143. string MethodName = apiInfo.methodName ?? "";
  144. string TabName = apiInfo.tabName ?? "";
  145. string Title = apiInfo.apiName ?? "";
  146. string ApiType = apiInfo.apiKind.ToString();
  147. string Method = "get";
  148. string ApiKey = "";
  149. if(string.IsNullOrEmpty(ApiKey))
  150. {
  151. ApiKey = Gategory + MethodName;
  152. }
  153. if(ApiType == "3") Method = "post";
  154. if(ApiType == "4") Method = "put";
  155. if(ApiType == "5") Method = "delete";
  156. Dictionary<string, object> subItem = new Dictionary<string, object>();
  157. string ApiRouter = "/v1/" + Router + "/" + Gategory + "/" + MethodName;
  158. subItem.Add("api_router", ApiRouter);
  159. subItem.Add("api_port", Port);
  160. if(Port == "443")
  161. {
  162. subItem.Add("api_host", "https://" + Host);
  163. }
  164. else
  165. {
  166. subItem.Add("api_host", "http://" + Host);
  167. }
  168. subItem.Add("api_key", ApiKey);
  169. subItem.Add("api_name", TabName + "-" + Title);
  170. subItem.Add("api_method", Method);
  171. subItem.Add("api_path", "/" + Gategory + "/" + linkTable.tableNameUpper + "/index");
  172. subItem.Add("api_en_name", linkTable.tableNameUpper ?? "");
  173. child.Add(subItem);
  174. }
  175. Dictionary<string, object> item = new Dictionary<string, object>();
  176. item.Add("id", Id);
  177. item.Add("group_project", param.projectId);
  178. item.Add("group_kind", 1);
  179. item.Add("group_remark", proService.serviceTitle ?? "");
  180. item.Add("group_name", PublicFunction.transferName(proService.serviceName ?? "", 2));
  181. item.Add("child", child);
  182. dic.Add(item);
  183. }
  184. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  185. string result = "";
  186. if(project.encryptKind == "AES")
  187. {
  188. JsonData encryptParam = JsonMapper.ToObject(project.encryptParam ?? "{}");
  189. string reqData = CryptHelper.AesEncrypt(req, encryptParam["encrypt_key"].ToString(), encryptParam["encrypt_iv"].ToString(), encryptParam["encrypt_model"].ToString(), encryptParam["encrypt_padding"].ToString());
  190. result = Function.PostWebRequest(ApiListHost, reqData, "application/json");
  191. JsonData jsonObj = JsonMapper.ToObject(result);
  192. if(jsonObj["status"].ToString() == "1")
  193. {
  194. return "同步成功";
  195. }
  196. }
  197. else
  198. {
  199. result = Function.PostWebRequest(ApiListHost, "value=" + req);
  200. if(result == "success")
  201. {
  202. return "同步成功";
  203. }
  204. }
  205. return "同步失败";
  206. }
  207. }
  208. }