| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 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
- {
- /// <summary>
- /// 接口分组Service业务层处理
- /// </summary>
- [AppService(ServiceType = typeof(IApiGroupService), ServiceLifetime = LifeTime.Transient)]
- public class ApiGroupService : BaseService<ApiGroup>, IApiGroupService
- {
- private readonly IApiInfoService _ApiInfoService;
- public ApiGroupService(IApiInfoService ApiInfoService)
- {
- _ApiInfoService = ApiInfoService;
- }
- /// <summary>
- /// 接口分组-列表
- /// </summary>
- /// <param name="param">参数请求体</param>
- /// <param name="page">分页参数</param>
- /// <returns>列表</returns>
- public PagedInfo<GetApiGroupListVo> getApiGroupList([FromQuery] PagerInfo page, [FromQuery] ApiGroup param)
- {
- //拼装查询条件
- var predicate = Expressionable.Create<ApiGroup>();
- 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<ApiGroup, GetApiGroupListVo>(page);
- return response;
- }
- /// <summary>
- /// 获取接口树结构
- /// </summary>
- /// <param name="param">参数请求体</param>
- /// <returns>接口树结构</returns>
- public List<GetApiTreeVo> getApiTree(GetApiTreeDto param)
- {
- //构造返回体
- List<Dictionary<string, object>> outputObjects = new List<Dictionary<string, object>>();
- Dictionary<string, object> outputObject = new Dictionary<string, object>();
- outputObject.Add("label", "status");
- outputObject.Add("type", "number");
- outputObjects.Add(outputObject);
- outputObject = new Dictionary<string, object>();
- outputObject.Add("label", "msg");
- outputObject.Add("type", "string");
- outputObjects.Add(outputObject);
- outputObject = new Dictionary<string, object>();
- outputObject.Add("label", "data");
- outputObject.Add("type", "#$data$#");
- outputObjects.Add(outputObject);
- string outputJsonTmp = Newtonsoft.Json.JsonConvert.SerializeObject(outputObjects);
- List<GetApiTreeVo> treeList = new List<GetApiTreeVo>();
- var services = IProject.getProjectServiceList(param.projectId);
- foreach(var service in services)
- {
- var serviceItem = service.Adapt<GetApiTreeVo>();
- var apiTreeGroup = new List<ApiTreeGroup>();
- var apiGroups = GetList(m => m.serviceId == service.id);
- foreach(var apiGroup in apiGroups)
- {
- var groupItem = apiGroup.Adapt<ApiTreeGroup>();
- var apiTreeInfo = new List<ApiTreeInfo>();
- var apiInfos = _ApiInfoService.GetList(m => m.groupId == apiGroup.id);
- foreach(var apiInfo in apiInfos)
- {
- var apiItem = apiInfo.Adapt<ApiTreeInfo>();
- 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;
- }
- /// <summary>
- /// 获取接口组树结构
- /// </summary>
- /// <returns>接口组树结构</returns>
- public List<GetApiGroupTreeVo> getApiGroupTree()
- {
- List<GetApiGroupTreeVo> treeList = new List<GetApiGroupTreeVo>();
- var projects = IProject.getProjectList();
- foreach (var project in projects)
- {
- var projectItem = project.Adapt<GetApiGroupTreeVo>();
- projectItem.projectTitle = project.projectName;
- var serviceList = new List<GetApiGroupTreeVoServices>();
- var services = IProject.getProjectServiceList(project.id);
- foreach (var service in services)
- {
- var serviceItem = new GetApiGroupTreeVoServices();
- serviceItem.serviceTitle = service.serviceTitle;
- var apiTreeGroup = new List<GetApiGroupTreeVoGroups>();
- 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;
- }
- /// <summary>
- /// 生成api文档
- /// </summary>
- /// <param name="serviceId">服务id</param>
- public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser)
- {
- var tableService = App.GetService<IDatabaseTableService>();
- 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<string, object> pushData = new Dictionary<string, object>();
- 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"
- };
- }
- /// <summary>
- /// 同步api到管理平台
- /// </summary>
- /// <param name="serviceId">服务id</param>
- public string sycnApiToSystem(SycnApiToSystemDto param)
- {
- var apiGroupService = App.GetService<IApiGroupService>();
- var apiInfoService = App.GetService<IApiInfoService>();
- var tableService = App.GetService<IDatabaseTableService>();
- 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<string> checkStrs = new List<string>();
- List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>();
- 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<Dictionary<string, object>> child = new List<Dictionary<string, object>>();
- 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<string, object> publicItem = new Dictionary<string, object>();
- 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<string, object> subItem = new Dictionary<string, object>();
- 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<string, object> item = new Dictionary<string, object>();
- 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 "同步失败";
- }
- }
- }
|