| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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;
- 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>
- /// 生成api文档
- /// </summary>
- /// <param name="serviceId">服务id</param>
- public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser)
- {
- 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;
- }
- 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 project = IProject.getProject(param.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;
- var projectServiceList = IProject.getProjectServiceList(param.projectId);
- foreach(var proService in projectServiceList)
- {
- Id += 1;
- 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];
- }
- List<Dictionary<string, object>> child = new List<Dictionary<string, object>>();
- var apiInfos = apiInfoService.GetList(m => m.serviceId == serId);
- if(param.serviceId > 0)
- {
- apiInfos = apiInfos.Where(m => m.serviceId == param.serviceId).ToList();
- }
- if(param.groupId > 0)
- {
- apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
- }
- foreach(var apiInfo in apiInfos)
- {
- var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
- string Gategory = apiInfo.controllerName ?? "";
- string Router = "";
- var group = apiGroupService.GetList(m => m.id == apiInfo.groupId);
- if (group.Count > 0)
- {
- Router = group[0].groupName ?? "";
- }
- 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", Port);
- if(Port == "443")
- {
- subItem.Add("api_host", "https://" + Host);
- }
- else
- {
- subItem.Add("api_host", "http://" + Host);
- }
- 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", 1);
- item.Add("group_remark", proService.serviceTitle ?? "");
- item.Add("group_name", PublicFunction.transferName(proService.serviceName ?? "", 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 "同步失败";
- }
- }
- }
|