using Attribute; using Model; using Model.Base; using Repository; using Service; using Microsoft.AspNetCore.Mvc; using Vo.Admin; using Common; using Custom; using Tasks; using Infrastructure; using Dto.Admin; using Feign; using Util; using LitJson; using Mapster; namespace Services { /// /// 接口分组Service业务层处理 /// [AppService(ServiceType = typeof(IApiGroupService), ServiceLifetime = LifeTime.Transient)] public class ApiGroupService : BaseService, IApiGroupService { private readonly IApiInfoService _ApiInfoService; public ApiGroupService(IApiInfoService ApiInfoService) { _ApiInfoService = ApiInfoService; } /// /// 接口分组-列表 /// /// 参数请求体 /// 分页参数 /// 列表 public PagedInfo getApiGroupList([FromQuery] PagerInfo page, [FromQuery] ApiGroup param) { //拼装查询条件 var predicate = Expressionable.Create(); predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId); predicate = predicate.AndIF(!string.IsNullOrEmpty(param.groupName), m => m.groupName.Contains(param.groupName)); predicate = predicate.AndIF(param.appId > 0, m => m.appId == param.appId); var response = Queryable() .Where(predicate.ToExpression()) .OrderByDescending(m => m.id) .ToPage(page); return response; } /// /// 获取接口树结构 /// /// 参数请求体 /// 接口树结构 public List getApiTree(GetApiTreeDto param) { //构造返回体 List> outputObjects = new List>(); Dictionary outputObject = new Dictionary(); outputObject.Add("label", "status"); outputObject.Add("type", "number"); outputObjects.Add(outputObject); outputObject = new Dictionary(); outputObject.Add("label", "msg"); outputObject.Add("type", "string"); outputObjects.Add(outputObject); outputObject = new Dictionary(); outputObject.Add("label", "data"); outputObject.Add("type", "#$data$#"); outputObjects.Add(outputObject); string outputJsonTmp = Newtonsoft.Json.JsonConvert.SerializeObject(outputObjects); List treeList = new List(); var services = IProject.getProjectServiceList(param.projectId); foreach(var service in services) { var serviceItem = service.Adapt(); var apiTreeGroup = new List(); var apiGroups = GetList(m => m.serviceId == service.id); foreach(var apiGroup in apiGroups) { var groupItem = apiGroup.Adapt(); var apiTreeInfo = new List(); var apiInfos = _ApiInfoService.GetList(m => m.groupId == apiGroup.id); foreach(var apiInfo in apiInfos) { var apiItem = apiInfo.Adapt(); if(apiInfo.apiParam != null) { apiItem.requestParam = apiInfo.apiParam.Where(m => m.reqParam).ToList(); apiItem.responseParam = apiInfo.apiParam.Where(m => m.resParam).ToList(); } apiItem.apiPath = "/v1/" + groupItem.groupName + "/" + apiInfo.controllerNameUpper + "/" + apiInfo.methodName; apiItem.apiKey = apiInfo.controllerNameUpper + apiInfo.methodName; string outputJson = outputJsonTmp; if(apiInfo.apiKind == 1) { outputJson = outputJson.Replace("#$data$#", "array"); } else { outputJson = outputJson.Replace("#$data$#", "object"); } apiItem.outputObject = outputJson; apiTreeInfo.Add(apiItem); } groupItem.apiInfos = apiTreeInfo; apiTreeGroup.Add(groupItem); } serviceItem.apiGroups = apiTreeGroup; treeList.Add(serviceItem); } return treeList; } /// /// 获取接口组树结构 /// /// 接口组树结构 public List getApiGroupTree() { List treeList = new List(); var projects = IProject.getProjectList(); foreach (var project in projects) { var projectItem = project.Adapt(); projectItem.projectTitle = project.projectName; var serviceList = new List(); var services = IProject.getProjectServiceList(project.id); foreach (var service in services) { var serviceItem = new GetApiGroupTreeVoServices(); serviceItem.serviceTitle = service.serviceTitle; var apiTreeGroup = new List(); var apiGroups = GetList(m => m.serviceId == service.id); foreach (var apiGroup in apiGroups) { var groupItem = new GetApiGroupTreeVoGroups(); groupItem.groupTitle = apiGroup.groupTitle; groupItem.groupId = apiGroup.id; apiTreeGroup.Add(groupItem); } serviceItem.apiGroups = apiTreeGroup; serviceList.Add(serviceItem); } projectItem.services = serviceList; treeList.Add(projectItem); } return treeList; } /// /// 生成api文档 /// /// 服务id public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser) { var tableService = App.GetService(); var service = Feign.IProject.getProjectService(param.serviceId); var apiGroups = GetList(m => m.serviceId == param.serviceId); var apiInfos = _ApiInfoService.GetList(m => m.serviceId == param.serviceId); if(param.groupId > 0) { apiGroups = apiGroups.Where(m => m.id == param.groupId).ToList(); apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList(); } foreach(var apiInfo in apiInfos) { apiInfo.groupTitle = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupTitle; apiInfo.groupName = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupName; var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable(); apiInfo.linkTableTitle = linkTable.tableTitle; apiInfo.linkTableName = linkTable.tableName; } string id = service.id.ToString() + param.groupId.ToString(); Dictionary pushData = new Dictionary(); pushData.Add("serviceName", service.serviceName); pushData.Add("serviceTitle", service.serviceTitle); pushData.Add("apiDocId", id); pushData.Add("apiGroup", apiGroups); pushData.Add("apiInfo", apiInfos); MakeHelper.Push(pushData, "apifoxDoc", "apidoc", loginUser, id); var options = App.OptionsSetting; return new MakeApiDocVo() { docUrl = options.BaseConfigs.gatewayHost + "/v1/omega_make/StaticFile/apiDocQuery?id=" + id + "&fileType=apidoc" }; } /// /// 同步api到管理平台 /// /// 服务id public string sycnApiToSystem(SycnApiToSystemDto param) { var apiGroupService = App.GetService(); var apiInfoService = App.GetService(); var tableService = App.GetService(); var proService = IProject.getProjectService(param.serviceId); var project = IProject.getProject(proService.projectId); string ApiListHost = ""; string Hosts = ""; if(param.env == "test") { ApiListHost = project.apiListNoticeHostDev ?? ""; Hosts = project.gatewayHostDev ?? ""; } else { ApiListHost = project.apiListNoticeHost ?? ""; Hosts = project.gatewayHost ?? ""; } // var projectService = IProject.getProjectService(param.serviceId); List checkStrs = new List(); List> dic = new List>(); string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); int Id = 0; bool withPublicApi = false; int publicApiGroupId = 0; int serId = proService.id; string serviceTitle = proService.serviceTitle ?? ""; string serviceName = proService.serviceName ?? ""; string Language = proService.devLanguage ?? ""; string[] HostList = Hosts.Split(':'); string Host = HostList[0]; string Port = "80"; if(HostList.Length > 1) { Port = HostList[1]; } var groups = GetList(m => m.serviceId == param.serviceId); foreach(var group in groups) { Id += 1; List> child = new List>(); var apiInfos = apiInfoService.GetList(m => m.serviceId == serId); apiInfos = apiInfos.Where(m => m.groupId == group.id).ToList(); foreach(var apiInfo in apiInfos) { var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable(); string Gategory = apiInfo.controllerNameUpper ?? ""; string Router = ""; string apiHost = ""; string apiPort = ""; Router = group.groupName ?? ""; if(param.env == "test") { apiHost = group.apiHostDev ?? ""; } else { apiHost = group.apiHost ?? ""; } if(apiHost.Contains(":")) { string[] apiHostList = apiHost.Split(':'); apiHost = apiHostList[0]; if(apiHostList.Length > 1) { apiPort = apiHostList[1]; } } if(group.withPublicApi && withPublicApi == false) { publicApiGroupId = group.id; withPublicApi = true; string publicApiJson = Function.ReadInstance("public_api.json"); JsonData publicApiList = JsonMapper.ToObject(publicApiJson); for(int i = 0; i < publicApiList.Count; i++) { JsonData publicApi = publicApiList[i]; string publicApiRouter = publicApi["api_router"].ToString(); string publicApiKey = publicApi["api_key"].ToString(); string publicApiName = publicApi["api_name"].ToString(); string publicApiMethod = publicApi["api_method"].ToString(); Dictionary publicItem = new Dictionary(); publicApiRouter = publicApiRouter.Replace("/lkb/", "/" + Router + "/"); publicItem.Add("api_router", publicApiRouter); publicItem.Add("api_port", apiPort); if(apiPort == "443") { publicItem.Add("api_host", "https://" + apiHost); } else { publicItem.Add("api_host", "http://" + apiHost); } publicItem.Add("api_key", publicApiKey); publicItem.Add("api_name", publicApiName); publicItem.Add("api_method", publicApiMethod); publicItem.Add("api_path", "/" + Router + "/public_api/index"); publicItem.Add("api_en_name", "sys"); child.Add(publicItem); } } // if(!string.IsNullOrEmpty(apiHost)) apiHost = Host; // if(!string.IsNullOrEmpty(apiPort)) apiPort = Port; string MethodName = apiInfo.methodName ?? ""; string TabName = apiInfo.tabName ?? ""; string Title = apiInfo.apiName ?? ""; string ApiType = apiInfo.apiKind.ToString(); string Method = "get"; string ApiKey = ""; if(string.IsNullOrEmpty(ApiKey)) { ApiKey = Gategory + MethodName; } if(ApiType == "3") Method = "post"; if(ApiType == "4") Method = "put"; if(ApiType == "5") Method = "delete"; Dictionary subItem = new Dictionary(); string ApiRouter = "/v1/" + Router + "/" + Gategory + "/" + MethodName; subItem.Add("api_router", ApiRouter); subItem.Add("api_port", apiPort); if(apiPort == "443") { subItem.Add("api_host", "https://" + apiHost); } else { subItem.Add("api_host", "http://" + apiHost); } subItem.Add("api_key", ApiKey); subItem.Add("api_name", TabName + "-" + Title); subItem.Add("api_method", Method); subItem.Add("api_path", "/" + Gategory + "/" + linkTable.tableNameUpper + "/index"); subItem.Add("api_en_name", linkTable.tableNameUpper ?? ""); child.Add(subItem); } Dictionary item = new Dictionary(); item.Add("id", Id); item.Add("group_project", param.projectId); item.Add("group_kind", group.groupTitle.Contains("后台") ? 1 : 0); item.Add("group_remark", group.groupTitle ?? ""); item.Add("group_name", PublicFunction.transferName(group.groupName ?? "", 2)); item.Add("child", child); dic.Add(item); } string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic); string result = ""; // if(project.encryptKind == "AES") // { // JsonData encryptParam = JsonMapper.ToObject(project.encryptParam ?? "{}"); // string reqData = CryptHelper.AesEncrypt(req, encryptParam["encrypt_key"].ToString(), encryptParam["encrypt_iv"].ToString(), encryptParam["encrypt_model"].ToString(), encryptParam["encrypt_padding"].ToString()); // result = Function.PostWebRequest(ApiListHost, reqData, "application/json"); // JsonData jsonObj = JsonMapper.ToObject(result); // if(jsonObj["status"].ToString() == "1") // { // return "同步成功"; // } // } // else // { result = Function.PostWebRequest(ApiListHost, "value=" + req); if(result == "success") { return "同步成功"; } // } return "同步失败"; } } }