Parcourir la source

优化同步api到管理平台功能

lichunlei il y a 3 mois
Parent
commit
39db9fe2c4

+ 1 - 1
Common/CryptUtil.cs

@@ -1,7 +1,7 @@
 using System.Text;
 using System.Security.Cryptography;
 
-namespace MySystem
+namespace Common
 {
     public class CryptHelper
     {

+ 7 - 2
Controllers/Admin/ApiGroupController.cs

@@ -126,9 +126,14 @@ namespace Controllers.Admin
         /// <returns>同步api到管理平台</returns>
         [HttpPost]
         [Route("/v1/omega_source/ApiGroup/sycnApiToSystem")]
-        public IActionResult sycnApiToSystem([FromBody] ApiGroup param)
+        public IActionResult sycnApiToSystem([FromBody] SycnApiToSystemDto param)
         {
-            return SUCCESS("ok");
+            var response = _ApiGroupService.sycnApiToSystem(param.projectId, param.serviceId, param.env);
+            if(response == "同步成功")
+            {
+                return SUCCESS(response);
+            }
+            return ToResponse(ResultCode.FAIL, response);
         }
 
 

+ 25 - 6
Feign/IProject.cs

@@ -6,12 +6,19 @@ namespace Feign
 {
     public class IProject
     {
-        // public static List<PageForMiniProgramVo> GetPageForMiniProgram(List<int> moduleIdList)
-        // {
-        //     string content = Function.GetWebRequest(Utils.FeignUrl["ym_material"] + "/v1/pub/pageForMiniProgram?moduleIdList=" + Newtonsoft.Json.JsonConvert.SerializeObject(moduleIdList).Replace("[", "").Replace("]", ""));
-        //     Utils.WriteLog(content, "GetPageForMiniProgram");
-        //     return Newtonsoft.Json.JsonConvert.DeserializeObject<List<PageForMiniProgramVo>>(content);
-        // }
+        public static List<ProjectService> getProjectServiceList(int projectId)
+        {
+            string url = Utils.FeignUrl["omega_project"] + "/v1/omega_project/ProjectService/getProjectServiceList?projectId=" + projectId;
+            Function.WriteLog(url, "获取项目信息");
+            string content = Function.GetWebRequest(url);
+            Function.WriteLog(content, "获取项目信息");
+            JsonData jsonObj = JsonMapper.ToObject(content);
+            if(jsonObj["status"].ToString() == "1")
+            {
+                return Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProjectService>>(jsonObj["data"].ToJson());
+            }
+            return new List<ProjectService>();
+        }
 
         public static ProjectService getProjectService(int id)
         {
@@ -26,5 +33,17 @@ namespace Feign
             }
             return new ProjectService();
         }
+
+        public static Project getProject(int id)
+        {
+            string url = Utils.FeignUrl["omega_project"] + "/v1/omega_project/Project/getProjectQuery?id=" + id;
+            string content = Function.GetWebRequest(url);
+            JsonData jsonObj = JsonMapper.ToObject(content);
+            if(jsonObj["status"].ToString() == "1")
+            {
+                return Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(jsonObj["data"].ToJson());
+            }
+            return new Project();
+        }
     }
 }

+ 147 - 0
Model/Custom/Project.cs

@@ -0,0 +1,147 @@
+namespace Custom
+{
+    /// <summary>
+    /// 项目 project
+    /// </summary>
+    public class Project
+    {
+        /// <summary>
+        /// ID
+        /// </summary>
+        public int id { get; set; }
+
+
+        /// <summary>
+        /// 版本号
+        /// </summary>
+        public int version { get; set; }
+
+
+        /// <summary>
+        /// 删除标记
+        /// </summary>
+        public int delFlag { get; set; }
+
+
+        /// <summary>
+        /// 创建时间
+        /// </summary>
+        public DateTime? createTime { get; set; }
+
+
+        /// <summary>
+        /// 更新时间
+        /// </summary>
+        public DateTime? updateTime { get; set; }
+
+
+        /// <summary>
+        /// LOGO
+        /// </summary>
+        public string? logo { get; set; }
+
+
+        /// <summary>
+        /// 项目名称
+        /// </summary>
+        public string? projectName { get; set; }
+
+
+        /// <summary>
+        /// 微服务标记
+        /// </summary>
+        public bool microServiceFlag { get; set; }
+
+
+        /// <summary>
+        /// 创建人
+        /// </summary>
+        public string? createBy { get; set; }
+
+
+        /// <summary>
+        /// 更新人
+        /// </summary>
+        public string? updateBy { get; set; }
+
+
+        /// <summary>
+        /// 应用端
+        /// </summary>
+        public int appType { get; set; }
+
+
+        /// <summary>
+        /// 后端开发语言
+        /// </summary>
+        public string? backendLanguage { get; set; }
+
+
+        /// <summary>
+        /// 项目介绍
+        /// </summary>
+        public string? projectDetail { get; set; }
+
+
+        /// <summary>
+        /// 使用功能
+        /// </summary>
+        public string? featureList { get; set; }
+
+
+        /// <summary>
+        /// 业务模块
+        /// </summary>
+        public string? businessList { get; set; }
+
+
+        /// <summary>
+        /// 项目分组
+        /// </summary>
+        public int projectGroupId { get; set; }
+
+
+        /// <summary>
+        /// 状态
+        /// </summary>
+        public int status { get; set; }
+
+
+        /// <summary>
+        /// api数据同步地址测试
+        /// </summary>
+        public string? apiListNoticeHostDev { get; set; }
+
+
+        /// <summary>
+        /// api数据同步地址生产
+        /// </summary>
+        public string? apiListNoticeHost { get; set; }
+
+
+        /// <summary>
+        /// 网关地址测试
+        /// </summary>
+        public string? gatewayHostDev { get; set; }
+
+
+        /// <summary>
+        /// 网关地址生产
+        /// </summary>
+        public string? gatewayHost { get; set; }
+
+
+        /// <summary>
+        /// 加密方式
+        /// </summary>
+        public int encryptKind { get; set; }
+
+        /// <summary>
+        /// 加密参数
+        /// </summary>
+        public string? encryptParams { get; set; }
+
+
+
+    }
+}

+ 28 - 0
Model/Dto/Admin/SycnApiToSystemDto.cs

@@ -0,0 +1,28 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using Model.Base;
+
+namespace Dto.Admin
+{
+    /// <summary>
+    /// 添加
+    /// </summary>
+    public class SycnApiToSystemDto
+    {
+        /// <summary>
+        /// 项目Id
+        /// </summary>
+        public int projectId { get; set; }
+
+        /// <summary>
+        /// 服务Id
+        /// </summary>
+        public int serviceId { get; set; }
+        
+        /// <summary>
+        /// 环境
+        /// </summary>
+        public string env { get; set; }
+
+    }
+}

+ 118 - 2
Services/ApiGroupService.cs

@@ -10,6 +10,9 @@ using Custom;
 using Tasks;
 using Infrastructure;
 using Dto.Admin;
+using Feign;
+using Util;
+using LitJson;
 
 
 namespace Services
@@ -87,9 +90,122 @@ namespace Services
         /// 同步api到管理平台
         /// </summary>
         /// <param name="serviceId">服务id</param>
-        public void sycnApiToSystem(int serviceId)
+        public string sycnApiToSystem(int projectId, int serviceId, string env)
         {
-            
+            var apiGroupService = App.GetService<IApiGroupService>();
+            var apiInfoService = App.GetService<IApiInfoService>();
+            var tableService = App.GetService<IDatabaseTableService>();
+            var project = IProject.getProject(projectId);
+            string ApiListHost = "";
+            string Hosts = "";
+            if(env == "test")
+            {
+                ApiListHost = project.apiListNoticeHostDev ?? "";
+                Hosts = project.gatewayHostDev ?? "";
+            }
+            else
+            {
+                ApiListHost = project.apiListNoticeHost ?? "";
+                Hosts = project.gatewayHost ?? "";
+            }
+            var projectService = IProject.getProjectService(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(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);
+                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", 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 == 1)
+            {
+                JsonData encryptParam = JsonMapper.ToObject(project.encryptParams ?? "{}");
+                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 "同步失败";
         }
     }
 }

+ 1 - 1
Services/IService/IApiGroupService.cs

@@ -29,6 +29,6 @@ namespace Services
         /// 同步api到管理平台
         /// </summary>
         /// <param name="serviceId">服务id</param>
-        void sycnApiToSystem(int serviceId);
+        string sycnApiToSystem(int projectId, int serviceId, string env);
     }
 }