TANG JIANG 1 жил өмнө
parent
commit
0e046b1704

+ 8 - 1
Sharing.sln

@@ -31,7 +31,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sharing.Province", "src\Sha
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sharing.WebCore", "src\Sharing.WebCore\Sharing.WebCore.csproj", "{ADF823F0-CF69-4853-8118-13860B4A2F18}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharing.DaoShu110", "src\Sharing.DaoShu110\Sharing.DaoShu110.csproj", "{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sharing.DaoShu110", "src\Sharing.DaoShu110\Sharing.DaoShu110.csproj", "{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharing.Enterprise", "src\Sharing.Enterprise\Sharing.Enterprise.csproj", "{139B193B-B8B2-43B5-8332-63A984F6938B}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -71,6 +73,10 @@ Global
 		{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3}.Release|Any CPU.Build.0 = Release|Any CPU
+		{139B193B-B8B2-43B5-8332-63A984F6938B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{139B193B-B8B2-43B5-8332-63A984F6938B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{139B193B-B8B2-43B5-8332-63A984F6938B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{139B193B-B8B2-43B5-8332-63A984F6938B}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -89,6 +95,7 @@ Global
 		{7E0A47AF-497F-45F9-A96C-4C4904CB1242} = {542857FB-A00E-47B5-9F14-63E9532C0CAA}
 		{ADF823F0-CF69-4853-8118-13860B4A2F18} = {D17C542B-8C07-4E18-979F-A706D79E23A0}
 		{FA6F3606-1ECF-42E8-AD61-EA00E5E792E3} = {542857FB-A00E-47B5-9F14-63E9532C0CAA}
+		{139B193B-B8B2-43B5-8332-63A984F6938B} = {542857FB-A00E-47B5-9F14-63E9532C0CAA}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {38EE029C-2A3A-441C-83E6-A41B16852FC3}

+ 1 - 0
src/Sharing.Api/Sharing.Api.csproj

@@ -18,6 +18,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\Sharing.DaoShu110\Sharing.DaoShu110.csproj" />
+    <ProjectReference Include="..\Sharing.Enterprise\Sharing.Enterprise.csproj" />
     <ProjectReference Include="..\Sharing.Province\Sharing.Province.csproj" />
   </ItemGroup>
 

+ 3 - 1
src/Sharing.Api/StartupExtensions.cs

@@ -6,7 +6,7 @@ using Serilog;
 using Sharing.Application;
 using Sharing.DaoShu110;
 using Sharing.Province;
-using Sharing.Province.Handlers.HuiJu;
+using Sharing.Enterprise;
 using Sharing.Repository.Extensions;
 using XF.Domain.Dependency;
 using XF.Domain.Filters;
@@ -33,6 +33,7 @@ internal static class StartupExtensions
             .AddSharingProvince()
             .AddApplication()
             .AddSharingPoliceDS()
+            .AddSharingEnterprise()
             ;
 
         //Authentication
@@ -73,6 +74,7 @@ internal static class StartupExtensions
             cfg.NotificationPublisherType = typeof(TaskWhenAllPublisher); // this will be the ServiceLifetime
             cfg.RegisterProvinceServices();
             cfg.RegisterPoliceDSServices();
+            cfg.RegisterEnterpriseServices();
         });
 
         //sqlsugar

+ 6 - 0
src/Sharing.Api/config/appsettings.Development.json

@@ -29,6 +29,12 @@
       "XieTong": "http://103.203.219.157/cns-scxthj-rest-test/rest/",
       "ClientId": "6384f475-2e1e-4985-9e34-51a3379452d0",
       "ClientSecret": "3e92b698-d640-4056-be94-128710ac1a7e"
+    },
+    "Enterprise": {
+      "AddressUrl": "http://218.89.66.8:23101/",
+      "ClientId": "1462598736",
+      "ClientSecret": "6nZtVK4rKfnsncGymUHB",
+      "TenantId": "000000"
     }
   },
   "IdentityUrl": "http://identity.fw.com",

+ 46 - 0
src/Sharing.Enterprise/Controllers/test.cs

@@ -0,0 +1,46 @@
+using DotNetCore.CAP;
+using MapsterMapper;
+using MediatR;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Sharing.Enterprise.Dtos;
+using Sharing.WebCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Sharing.Enterprise.Controllers
+{
+    public class test : BaseController
+    {
+        private readonly IChannelConfigurationManager _channelConfigurationManager;
+
+        private readonly ICapPublisher _capPublisher;
+        private readonly IMapper _mapper;
+        private readonly IMediator _mediator;
+
+        [HttpGet("testaaaa")]
+        public async Task<string> Test()
+        {
+            //var request = new SubmitCaseResultRequest();
+            //var sss = Sharing.Enterprise.EnterpriseClient.RequestAsync<SubmitCaseResultRequest, BaseProvinceResponse>
+            //    (request, "http://218.89.66.8:23101/", cancellationToken: HttpContext.RequestAborted);
+
+            return "";
+        }
+    }
+
+    /// <summary>
+    /// 服务工单处理结果
+    /// </summary>
+    public  class SubmitCaseResultRequest : EnterpriseRequest
+    {
+        //服务工单处理结果
+
+        public override string GetRequestUrl() => "platform/12345/selectEnterpriseList";
+
+        public override string GetHttpMethod() => HttpMethods.Post;
+    }
+}

+ 13 - 0
src/Sharing.Enterprise/Dtos/ClientInfo.cs

@@ -0,0 +1,13 @@
+namespace Sharing.Enterprise.Dtos;
+
+public class ClientInfo
+{
+    public ClientInfo(string clientId, string clientSecret)
+    {
+        ClientId = clientId;
+        ClientSecret = clientSecret;
+    }
+
+    public string ClientId { get; set; }
+    public string ClientSecret { get; set; }
+}

+ 9 - 0
src/Sharing.Enterprise/Dtos/EnterpriseRequest.cs

@@ -0,0 +1,9 @@
+namespace Sharing.Enterprise.Dtos;
+
+public abstract class EnterpriseRequest : IEnterpriseRequest
+{
+    public abstract string GetRequestUrl();
+
+    public abstract string GetHttpMethod();
+
+}

+ 33 - 0
src/Sharing.Enterprise/Dtos/EnterpriseResponse.cs

@@ -0,0 +1,33 @@
+namespace Sharing.Enterprise.Dtos;
+
+/// <summary>
+/// 省接口返回
+/// </summary>
+public class BaseProvinceResponse
+{
+    /// <summary>
+    /// 响应码
+    /// </summary>
+    public int code { get; set; }
+
+    /// <summary>
+    /// 是否成功
+    /// </summary>
+    public bool success { get; set; }
+
+    /// <summary>
+    /// 返回消息
+    /// </summary>
+    public string msg { get; set; }
+
+    /// <summary>
+    /// 操作结果
+    /// </summary>
+   // public T data { get; set; } 
+}
+
+
+
+
+
+

+ 7 - 0
src/Sharing.Enterprise/Dtos/IEnterpriseRequest.cs

@@ -0,0 +1,7 @@
+namespace Sharing.Enterprise.Dtos;
+
+public interface IEnterpriseRequest : ISharingRequest
+{
+    
+}
+

+ 52 - 0
src/Sharing.Enterprise/Dtos/TokenRsp.cs

@@ -0,0 +1,52 @@
+using System.Text.Json.Serialization;
+
+namespace Sharing.Enterprise.Dtos;
+
+/// <summary>
+/// token 返回值
+/// </summary>
+[Serializable]
+public class TokenRsp
+{
+    /// <summary>
+    /// 
+    /// </summary>
+    [JsonPropertyName("success")]
+    public bool Success { get; set; }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    [JsonPropertyName("data")]
+    public TokenInfo TokenInfo { get; set; }
+}
+
+/// <summary>
+/// token 内容
+/// </summary>
+public class TokenInfo
+{
+    /// <summary>
+    /// access_token
+    /// </summary>
+    [JsonPropertyName("access_token")]
+    public string AccessToken { get; set; }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    [JsonPropertyName("refresh_token")]
+    public string RefreshToken { get; set; }
+
+    /// <summary>
+    /// token类型
+    /// </summary>
+    [JsonPropertyName("token_type")]
+    public string TokenType { get; set; }
+
+    /// <summary>
+    /// token有效期,单位秒
+    /// </summary>
+    [JsonPropertyName("expires_in")]
+    public string ExpiresIn { get; set; }
+}

+ 91 - 0
src/Sharing.Enterprise/EnterpriseClient.cs

@@ -0,0 +1,91 @@
+using IdentityModel.Client;
+using Microsoft.Extensions.DependencyInjection;
+using Sharing.Enterprise.Dtos;
+using Sharing.Enterprise.Extensions;
+using System.Net.Http.Headers;
+using XF.Domain.Cache;
+using XF.Domain.Exceptions;
+
+namespace Sharing.Enterprise
+{
+    public abstract class EnterpriseClient //: ISingletonDependency, ISelfDependency
+    {
+        private static readonly string KeyToken = "EnterpriseKeyToken";
+        private readonly IServiceScopeFactory _scopeFactory;
+
+        public EnterpriseClient(IServiceScopeFactory scopeFactory)
+        {
+            _scopeFactory = scopeFactory;
+        }
+
+        public async Task<TResponse?> RequestAsync<TRequest, TResponse>(
+            TRequest request,
+            string baseAddress,
+            // ConfigurationEnterprise configEnterprise,
+            CancellationToken cancellationToken)
+            where TRequest : ISharingRequest, new()
+        {
+            using var scope = _scopeFactory.CreateScope();
+            var provider = scope.ServiceProvider;
+
+            var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
+            var cacheToken = provider.GetRequiredService<ITypedCache<TokenInfo>>();
+
+            var token = cacheToken.GetOrSet(KeyToken,
+                d => GetTokenAsync(cancellationToken).GetAwaiter().GetResult(),
+                TimeSpan.FromMinutes(58));
+
+            //   request.BuildClientInfo(configProvince.ClientId, configProvince.ClientSecret);
+
+            return await httpInvoker.RequestAsync<TRequest, TResponse>(request,
+                d => d.SetHttpClient(baseAddress, token?.AccessToken ?? string.Empty),
+                cancellationToken);
+        }
+
+        private async Task<TokenInfo> GetTokenAsync(CancellationToken cancellationToken)
+        {
+            using var scope = _scopeFactory.CreateScope();
+            var provider = scope.ServiceProvider;
+            var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();
+            var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
+
+            var configEnterprise = channelconfigManager.GetConfigurationEnterprise();
+            var baseAddress = configEnterprise.AddressUrl;
+            if (!baseAddress.EndsWith('/'))
+                baseAddress += "/";
+
+            string authorization = configEnterprise.ClientId + ":" + configEnterprise.ClientSecret;
+            authorization = Sharing.Enterprise.Extensions.Encoder.Base64Code(authorization);
+            authorization = authorization.Replace("@", "=");
+
+            var request = new ClientCredentialsTokenRequest
+            {
+                Address = $"{baseAddress}blade-auth/oauth/getAccessToken",
+                Headers =
+                {
+                    Accept =
+                    {
+                        MediaTypeWithQualityHeaderValue.Parse("application/json")
+                    }
+                }
+
+            };
+            request.Headers.Add("Authorization", "Basic " + authorization);
+            request.Headers.Add("Tenant-Id", configEnterprise.TenantId);
+            var tokenResponse = await httpInvoker.GetTokenAsync(request, cancellationToken);
+            if (tokenResponse is null)
+                throw new UserFriendlyException("获取token请求失败");
+            if (tokenResponse.IsError)
+                throw new UserFriendlyException("获取token请求失败");
+
+            var tokenInfo = System.Text.Json.JsonSerializer.Deserialize<TokenRsp>(tokenResponse.Raw);
+            if (tokenInfo is null || !tokenInfo.Success || tokenInfo?.TokenInfo is null)
+                throw new UserFriendlyException("token解析失败");
+            return tokenInfo.TokenInfo;
+        }
+
+
+    }
+
+
+}

+ 148 - 0
src/Sharing.Enterprise/Extensions/Encoder.cs

@@ -0,0 +1,148 @@
+using System.Collections;
+using System.Text;
+
+namespace Sharing.Enterprise.Extensions
+{
+    public static class Encoder
+    {
+        /// <summary>
+        /// 对string 进行 Base64 编码 
+        /// </summary>
+        /// <param name="strMessage">string 参数</param>
+        /// <returns> Base64 编码</returns>
+        public static string Base64Code(string strMessage)
+        {
+            bool flag = string.IsNullOrEmpty(strMessage);
+            string result;
+            if (flag)
+            {
+                result = "";
+            }
+            else
+            {
+                char[] array = new char[]
+                {
+            'A',
+            'B',
+            'C',
+            'D',
+            'E',
+            'F',
+            'G',
+            'H',
+            'I',
+            'J',
+            'K',
+            'L',
+            'M',
+            'N',
+            'O',
+            'P',
+            'Q',
+            'R',
+            'S',
+            'T',
+            'U',
+            'V',
+            'W',
+            'X',
+            'Y',
+            'Z',
+            'a',
+            'b',
+            'c',
+            'd',
+            'e',
+            'f',
+            'g',
+            'h',
+            'i',
+            'j',
+            'k',
+            'l',
+            'm',
+            'n',
+            'o',
+            'p',
+            'q',
+            'r',
+            's',
+            't',
+            'u',
+            'v',
+            'w',
+            'x',
+            'y',
+            'z',
+            '0',
+            '1',
+            '2',
+            '3',
+            '4',
+            '5',
+            '6',
+            '7',
+            '8',
+            '9',
+            '+',
+            '/',
+            '='
+                };
+                byte b = 0;
+                ArrayList arrayList = new ArrayList(Encoding.Default.GetBytes(strMessage));
+                int count = arrayList.Count;
+                int num = count / 3;
+                int num2 = count % 3;
+                bool flag2 = num2 > 0;
+                if (flag2)
+                {
+                    for (int i = 0; i < 3 - num2; i++)
+                    {
+                        arrayList.Add(b);
+                    }
+                    num++;
+                }
+                StringBuilder stringBuilder = new StringBuilder(num * 4);
+                for (int i = 0; i < num; i++)
+                {
+                    byte[] array2 = new byte[]
+                    {
+                (byte)arrayList[i * 3],
+                (byte)arrayList[i * 3 + 1],
+                (byte)arrayList[i * 3 + 2]
+                    };
+                    int[] array3 = new int[4];
+                    array3[0] = array2[0] >> 2;
+                    array3[1] = ((int)(array2[0] & 3) << 4 ^ array2[1] >> 4);
+                    bool flag3 = !array2[1].Equals(b);
+                    if (flag3)
+                    {
+                        array3[2] = ((int)(array2[1] & 15) << 2 ^ array2[2] >> 6);
+                    }
+                    else
+                    {
+                        array3[2] = 64;
+                    }
+                    bool flag4 = !array2[2].Equals(b);
+                    if (flag4)
+                    {
+                        array3[3] = (int)(array2[2] & 63);
+                    }
+                    else
+                    {
+                        array3[3] = 64;
+                    }
+                    stringBuilder.Append(array[array3[0]]);
+                    stringBuilder.Append(array[array3[1]]);
+                    stringBuilder.Append(array[array3[2]]);
+                    stringBuilder.Append(array[array3[3]]);
+                }
+                string text = stringBuilder.ToString();
+                text = text.Replace("=", "@");
+                text = text.Replace("+", "%2B");
+                result = text;
+            }
+            return result;
+        }
+    }
+}

+ 23 - 0
src/Sharing.Enterprise/Extensions/HttpClientExtensions.cs

@@ -0,0 +1,23 @@
+using IdentityModel.Client;
+using System.Net.Http.Headers;
+using XF.Domain.Exceptions;
+using Sharing.Extensions;
+
+namespace Sharing.Enterprise.Extensions
+{
+    public static class HttpClientExtensions
+    {
+        public static HttpClient BuildHeader(this HttpClient httpClient, string token)
+        {
+            if (string.IsNullOrEmpty(token))
+                throw new UserFriendlyException("无效token");
+            httpClient.SetBearerToken(token);
+            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
+
+            return httpClient;
+        }
+
+        public static HttpClient SetHttpClient(this HttpClient httpClient, string baseAddress, string token) =>
+            httpClient.SetBaseAddress(baseAddress).BuildHeader(token);
+    }
+}

+ 17 - 0
src/Sharing.Enterprise/Sharing.Enterprise.csproj

@@ -0,0 +1,17 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net7.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <Folder Include="Handlers\" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\Sharing.WebCore\Sharing.WebCore.csproj" />
+  </ItemGroup>
+
+</Project>

+ 22 - 0
src/Sharing.Enterprise/StartupExtensions.cs

@@ -0,0 +1,22 @@
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Sharing.Enterprise
+{
+    public static class StartupExtensions
+    {
+        public static IServiceCollection AddSharingEnterprise(this IServiceCollection services)
+        {
+            return services;
+        }
+
+        public static void RegisterEnterpriseServices(this MediatRServiceConfiguration config)
+        {
+            config.RegisterServicesFromAssembly(typeof(StartupExtensions).Assembly);
+        }
+    }
+}

+ 4 - 2
src/Sharing.Province/Sharing.Province.csproj

@@ -8,11 +8,13 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\Sharing.WebCore\Sharing.WebCore.csproj" />
+    <Compile Remove="Domain\XieTong\**" />
+    <EmbeddedResource Remove="Domain\XieTong\**" />
+    <None Remove="Domain\XieTong\**" />
   </ItemGroup>
 
   <ItemGroup>
-    <Folder Include="Domain\XieTong\" />
+    <ProjectReference Include="..\Sharing.WebCore\Sharing.WebCore.csproj" />
   </ItemGroup>
 
 </Project>

+ 19 - 0
src/Sharing/ChannelConfiguration.cs

@@ -2,7 +2,15 @@
 
 public class ChannelConfiguration
 {
+    /// <summary>
+    /// 省12345
+    /// </summary>
     public ConfigurationProvince Province { get; set; }
+
+    /// <summary>
+    /// 企业服务
+    /// </summary>
+    public ConfigurationEnterprise Enterprise { get; set; }
 }
 
 public class ConfigurationProvince
@@ -12,4 +20,15 @@ public class ConfigurationProvince
     public string ClientId { get; set; } = string.Empty;
     public string ClientSecret { get; set; } = string.Empty;
     public string Scheme { get; set; } = ConfigurationConsts.SchemeDefault;
+}
+
+/// <summary>
+/// 企业服务
+/// </summary>
+public class ConfigurationEnterprise
+{
+    public string AddressUrl { get; set; } = string.Empty;
+    public string ClientId { get; set; } = string.Empty;
+    public string ClientSecret { get; set; } = string.Empty;
+    public string TenantId { get; set; } = string.Empty;
 }

+ 13 - 0
src/Sharing/ChannelConfigurationManager.cs

@@ -12,9 +12,22 @@ namespace Sharing
             _channelOption = channelOption;
         }
 
+        /// <summary>
+        /// 省12345
+        /// </summary>
+        /// <returns></returns>
         public ConfigurationProvince GetConfigurationProvince()
         {
             return _channelOption.Value.Province;
         }
+
+        /// <summary>
+        /// 企业服务
+        /// </summary>
+        /// <returns></returns>
+        public ConfigurationEnterprise GetConfigurationEnterprise()
+        {
+            return _channelOption.Value.Enterprise;
+        }
     }
 }

+ 2 - 0
src/Sharing/IChannelConfigurationManager.cs

@@ -3,4 +3,6 @@
 public interface IChannelConfigurationManager
 {
     ConfigurationProvince GetConfigurationProvince();
+
+    ConfigurationEnterprise GetConfigurationEnterprise();
 }