ApiGroupService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(int projectId, int serviceId, string env)
  86. {
  87. var apiGroupService = App.GetService<IApiGroupService>();
  88. var apiInfoService = App.GetService<IApiInfoService>();
  89. var tableService = App.GetService<IDatabaseTableService>();
  90. var project = IProject.getProject(projectId);
  91. string ApiListHost = "";
  92. string Hosts = "";
  93. if(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(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(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. foreach(var apiInfo in apiInfos)
  126. {
  127. var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
  128. string Gategory = apiInfo.controllerName ?? "";
  129. string Router = "";
  130. var group = apiGroupService.GetList(m => m.id == apiInfo.groupId);
  131. if (group.Count > 0)
  132. {
  133. Router = group[0].groupName ?? "";
  134. }
  135. string MethodName = apiInfo.methodName ?? "";
  136. string TabName = apiInfo.tabName ?? "";
  137. string Title = apiInfo.apiName ?? "";
  138. string ApiType = apiInfo.apiKind.ToString();
  139. string Method = "get";
  140. string ApiKey = "";
  141. if(string.IsNullOrEmpty(ApiKey))
  142. {
  143. ApiKey = Gategory + MethodName;
  144. }
  145. if(ApiType == "3") Method = "post";
  146. if(ApiType == "4") Method = "put";
  147. if(ApiType == "5") Method = "delete";
  148. Dictionary<string, object> subItem = new Dictionary<string, object>();
  149. string ApiRouter = "/v1/" + Router + "/" + Gategory + "/" + MethodName;
  150. subItem.Add("api_router", ApiRouter);
  151. subItem.Add("api_port", Port);
  152. if(Port == "443")
  153. {
  154. subItem.Add("api_host", "https://" + Host);
  155. }
  156. else
  157. {
  158. subItem.Add("api_host", "http://" + Host);
  159. }
  160. subItem.Add("api_key", ApiKey);
  161. subItem.Add("api_name", TabName + "-" + Title);
  162. subItem.Add("api_method", Method);
  163. subItem.Add("api_path", "/" + Gategory + "/" + linkTable.tableNameUpper + "/index");
  164. subItem.Add("api_en_name", linkTable.tableNameUpper ?? "");
  165. child.Add(subItem);
  166. }
  167. Dictionary<string, object> item = new Dictionary<string, object>();
  168. item.Add("id", Id);
  169. item.Add("group_project", projectId);
  170. item.Add("group_kind", 1);
  171. item.Add("group_remark", proService.serviceTitle ?? "");
  172. item.Add("group_name", PublicFunction.transferName(proService.serviceName ?? "", 2));
  173. item.Add("child", child);
  174. dic.Add(item);
  175. }
  176. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  177. string result = "";
  178. if(project.encryptKind == 1)
  179. {
  180. JsonData encryptParam = JsonMapper.ToObject(project.encryptParams ?? "{}");
  181. string reqData = CryptHelper.AesEncrypt(req, encryptParam["encrypt_key"].ToString(), encryptParam["encrypt_iv"].ToString(), encryptParam["encrypt_model"].ToString(), encryptParam["encrypt_padding"].ToString());
  182. result = Function.PostWebRequest(ApiListHost, reqData, "application/json");
  183. JsonData jsonObj = JsonMapper.ToObject(result);
  184. if(jsonObj["status"].ToString() == "1")
  185. {
  186. return "同步成功";
  187. }
  188. }
  189. else
  190. {
  191. result = Function.PostWebRequest(ApiListHost, "value=" + req);
  192. if(result == "success")
  193. {
  194. return "同步成功";
  195. }
  196. }
  197. return "同步失败";
  198. }
  199. }
  200. }