xf 1 년 전
부모
커밋
f1e9b20d69

+ 1 - 8
Hotline.sln

@@ -41,9 +41,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wex.Sdk", "src\Wex.Sdk\Wex.
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hotline.Wex", "src\Hotline.Wex\Hotline.Wex.csproj", "{40B6FBEC-0524-430C-8B28-22229681D0F8}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tr.Sdk", "src\Tr.Sdk\Tr.Sdk.csproj", "{BB901B26-EA97-4D6F-8CAC-14DB321E1733}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hotline.Tr", "src\Hotline.Tr\Hotline.Tr.csproj", "{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tr.Sdk", "src\Tr.Sdk\Tr.Sdk.csproj", "{BB901B26-EA97-4D6F-8CAC-14DB321E1733}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -107,10 +105,6 @@ Global
 		{BB901B26-EA97-4D6F-8CAC-14DB321E1733}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{BB901B26-EA97-4D6F-8CAC-14DB321E1733}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{BB901B26-EA97-4D6F-8CAC-14DB321E1733}.Release|Any CPU.Build.0 = Release|Any CPU
-		{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -134,7 +128,6 @@ Global
 		{F2E2E925-39FF-41BB-B199-34E50A3AC1FB} = {D041C554-B78E-4AAF-B597-E309DC8EEF4F}
 		{40B6FBEC-0524-430C-8B28-22229681D0F8} = {D041C554-B78E-4AAF-B597-E309DC8EEF4F}
 		{BB901B26-EA97-4D6F-8CAC-14DB321E1733} = {D041C554-B78E-4AAF-B597-E309DC8EEF4F}
-		{42A37A81-9B4A-4A05-8EBC-90D1E580E1DD} = {D041C554-B78E-4AAF-B597-E309DC8EEF4F}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {4B8EA790-BD13-4422-8D63-D6DBB77B823F}

+ 1 - 5
src/Hotline.Api/Controllers/TestController.cs

@@ -18,7 +18,6 @@ using NewRock.Sdk;
 using NewRock.Sdk.Security;
 using SqlSugar;
 using Tr.Sdk;
-using Tr.Sdk.Tel;
 using Wex.Sdk;
 using XC.RSAUtil;
 using XF.Domain.Authentications;
@@ -137,10 +136,7 @@ public class TestController : BaseController
 
         //var rsp = await _daprClient.InvokeMethodAsync<ApiResponse<string>>(HttpMethod.Get, "identity", "api/v1/Test/time", HttpContext.RequestAborted);
         //var rsp1 = await _daprClient.InvokeMethodAsync<int, ApiResponse<string>>(HttpMethod.Post, "identity", "api/v1/Test/time1", 222, HttpContext.RequestAborted);
-
-        var tels = await _trClient.ExecuteAsync<QueryTelRequest, List<QueryTelResponse>>(new QueryTelRequest(),
-            HttpContext.RequestAborted);
-
+        
         return OpenResponse.Ok(DateTime.Now.ToString("F"));
     }
 

+ 0 - 70
src/Hotline.Api/Program.cs

@@ -12,63 +12,6 @@ Log.Logger = new LoggerConfiguration()
 
 Log.Information("Hotline service is Starting up");
 
-//var a = "http://internal.ttf-cti.com:8080";
-////var r = await a
-////    .AppendPathSegment("api/login")
-////    .SetQueryParams(new { username = "yscs", password = "123456" })
-////    .GetAsync();
-////var h = r.Headers.FirstOrDefault(d => d.Name == "X-Auth-Token");
-
-//var p = new Pa { Phone = "13312341234", SpecialFlag = 4 };
-//var njp = Newtonsoft.Json.JsonConvert.SerializeObject(p);
-//var jp = System.Text.Json.JsonSerializer.Serialize(p);
-
-//FlurlHttp.GlobalSettings.JsonSerializer = new TrUrlEncodedSerializer();
-
-//var r1 = await a
-//    .AppendPathSegment("api/special_phone/insert")
-//    //.WithHeader(h.Name, h.Value)
-//    .WithHeader("X-Auth-Token", "8352d42165d7cf6821e53e926d66adba54c309e6")
-//    //.PostUrlEncodedAsync(new { phone = "13312341234", special_flag = 4 });
-//    //.PostUrlEncodedAsync(p);
-//    //.ConfigureRequest(s =>
-//    //{
-//    //    s.JsonSerializer = new Tr.Sdk.TrUrlEncodedSerializer();
-//    //})
-//    .BeforeCall(d =>
-//    {
-//        var b = d.RequestBody;
-//        var c = d.Request;
-//    })
-//    .PostUrlEncodedAsync(p);
-
-//var r2 = await r1;
-
-//var options = new RestClientOptions("http://internal.ttf-cti.com:8080");
-
-//var client = new RestClient(options);
-//client.AddDefaultHeader("X-Auth-Token", "8352d42165d7cf6821e53e926d66adba54c309e6");
-
-//var reqToken = new RestRequest("api/login")
-//    .AddObject(new { username = "yscs", password = "123456" });
-
-////var r1 = await client.GetAsync(reqToken);
-////var h = r1.Headers.FirstOrDefault(d => d.Name == "X-Auth-Token");
-////7a906970b1ce06a503807e75762da323782f4a29
-
-//var pa = new Pa
-//{
-//    Phone = "13333333333",
-//    SpecialFlag = 3
-//};
-
-//var reqAdd = new RestRequest("api/special_phone/insert", Method.Post)
-//    //.AddObject(new { phone = "13312341234", special_flag = 4 });
-//    .AddObject(pa);
-
-//var r2 = await client.PostAsync(reqAdd);
-
-
 try
 {
     var builder = WebApplication.CreateBuilder(args);
@@ -109,16 +52,3 @@ finally
     Log.Information("Shut down complete");
     Log.CloseAndFlush();
 }
-
-class Pa
-{
-    //[JsonProperty(PropertyName = "phone")]
-    //[JsonPropertyName("phone")]
-    [RequestProperty(Name = "phone")]
-    public string Phone { get; set; }
-
-    //[JsonProperty(PropertyName = "special_flag")]
-    //[JsonPropertyName("special_flag")]
-    [RequestProperty(Name = "special_flag")]
-    public int SpecialFlag { get; set; }
-}

+ 2 - 2
src/Hotline.Api/config/appsettings.Development.json

@@ -17,14 +17,14 @@
   },
   "ConnectionStrings": {
     "Hotline": "PORT=5432;DATABASE=hotline;HOST=110.188.24.182;PASSWORD=fengwo11!!;USER ID=dev;",
-    "Redis": "110.188.24.182:50179,password=fengwo22@@",
+    "Redis": "110.188.24.182:50179",
     "MongoDB": "mongodb://192.168.100.121:27017",
     "Wex": "server=222.212.82.225;Port=4509;Database=fs_kft;Uid=root;Pwd=Wex@12345;"
   },
   "Cache": {
     "Host": "110.188.24.182",
     "Port": 50179,
-    "Password": "fengwo22@@",
+    //"Password": "fengwo22@@",
     "Database": 1
   },
   "Swagger": true,

+ 1 - 1
src/Hotline.Application/Hotline.Application.csproj

@@ -14,9 +14,9 @@
     <ProjectReference Include="..\Hotline.Application.Contracts\Hotline.Application.Contracts.csproj" />
     <ProjectReference Include="..\Hotline.NewRock\Hotline.NewRock.csproj" />
     <ProjectReference Include="..\Hotline.Repository.SqlSugar\Hotline.Repository.SqlSugar.csproj" />
-    <ProjectReference Include="..\Hotline.Tr\Hotline.Tr.csproj" />
     <ProjectReference Include="..\Hotline.Wex\Hotline.Wex.csproj" />
     <ProjectReference Include="..\Hotline\Hotline.csproj" />
+    <ProjectReference Include="..\Tr.Sdk\Tr.Sdk.csproj" />
     <ProjectReference Include="..\XF.EasyCaching\XF.EasyCaching.csproj" />
   </ItemGroup>
 

+ 0 - 7
src/Hotline.Tr/Class1.cs

@@ -1,7 +0,0 @@
-namespace Hotline.Tr
-{
-    public class Class1
-    {
-
-    }
-}

+ 0 - 14
src/Hotline.Tr/Hotline.Tr.csproj

@@ -1,14 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
-  <PropertyGroup>
-    <TargetFramework>net7.0</TargetFramework>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
-  </PropertyGroup>
-
-  <ItemGroup>
-    <ProjectReference Include="..\Hotline\Hotline.csproj" />
-    <ProjectReference Include="..\Tr.Sdk\Tr.Sdk.csproj" />
-  </ItemGroup>
-
-</Project>

+ 23 - 0
src/Tr.Sdk/Blacklist/AddBlacklistRequest.cs

@@ -0,0 +1,23 @@
+using RestSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Linq;
+
+namespace Tr.Sdk.Blacklist
+{
+    public class AddBlacklistRequest : TrRequest
+    {
+        [RequestProperty(Name = "phone")]
+        public string Phone { get; set; }
+
+        [RequestProperty(Name = "special_flag")]
+        public int SpecialFlag { get; set; }
+
+        public override string Path() => "api/special_phone/insert";
+
+        public override Method HttpMethod() => Method.Post;
+    }
+}

+ 2 - 37
src/Tr.Sdk/ITrClient.cs

@@ -4,11 +4,10 @@ using System.Linq;
 using System.Text;
 using System.Text.Json.Serialization;
 using System.Threading.Tasks;
-using RestSharp;
 
 namespace Tr.Sdk
 {
-    public interface ITrClient
+    public partial interface ITrClient
     {
         /// <summary>
         /// 执行操作呼叫中心请求
@@ -18,41 +17,7 @@ namespace Tr.Sdk
         /// <param name="request"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
+        Task<TResponse> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
             where TRequest : TrRequest;
-
-        Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken);
-    }
-
-    public class TokenRequest : ITrRequest
-    {
-        public TokenRequest()
-        {
-            Path = "login/token";
-            Method = Method.Get;
-        }
-
-        public string Path { get; init; }
-
-        public Method Method { get; init; }
-
-        public string Username { get; set; }
-
-        public string Password { get; set; }
-    }
-
-    public interface ITrRequest
-    {
-        [JsonIgnore]
-        string Path { get; }
-
-        [JsonIgnore]
-        Method Method { get; }
-    }
-
-    public abstract class TrRequest
-    {
-        public abstract string Path();
-        public abstract Method Method();
     }
 }

+ 15 - 0
src/Tr.Sdk/Tels/ITrClient.Tel.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tr.Sdk.Tels;
+
+namespace Tr.Sdk
+{
+    public partial interface ITrClient
+    {
+        Task<List<QueryTelResponse>> QueryTelsAsync(QueryTelRequest request, CancellationToken cancellationToken) =>
+            ExecuteAsync<QueryTelRequest, List<QueryTelResponse>>(request, cancellationToken);
+    }
+}

+ 2 - 2
src/Tr.Sdk/Tel/QueryTelRequest.cs → src/Tr.Sdk/Tels/QueryTelRequest.cs

@@ -1,14 +1,14 @@
 using System.Text.Json.Serialization;
 using RestSharp;
 
-namespace Tr.Sdk.Tel
+namespace Tr.Sdk.Tels
 {
     public class QueryTelRequest : TrRequest
     {
         public override string Path() => "api/ola/extensions/detail";
 
 
-        public override Method Method() => RestSharp.Method.Get;
+        public override Method HttpMethod() => Method.Get;
     }
 
     public class QueryTelResponse

+ 55 - 0
src/Tr.Sdk/TrAuthenticator.cs

@@ -0,0 +1,55 @@
+using RestSharp;
+using RestSharp.Authenticators;
+
+namespace Tr.Sdk;
+
+public class TrAuthenticator : AuthenticatorBase
+{
+    private readonly string _tokenKey = "X-Auth-Token";
+    private readonly string _baseUrl;
+    private readonly string _apiKey;
+    private readonly string _apiSecret;
+
+    public TrAuthenticator(string baseUrl, string apiKey, string apiSecret) : base("")
+    {
+        _baseUrl = baseUrl;
+        _apiKey = apiKey;
+        _apiSecret = apiSecret;
+    }
+
+    protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
+    {
+        if (string.IsNullOrEmpty(Token))
+        {
+            Token = await GetTokenAsync(default);
+        }
+        return new HeaderParameter(_tokenKey, Token);
+    }
+
+    public async Task<string> GetTokenAsync(CancellationToken cancellationToken)
+    {
+        var options = new RestClientOptions(_baseUrl);
+        using var client = new RestClient(options);
+        var request = new RestRequest("api/login")
+            .AddObject(new { username = _apiKey, password = _apiSecret });
+        try
+        {
+            var response = await client.GetAsync(request, cancellationToken);
+            if (!response.IsSuccessful)
+                throw new HttpRequestException(
+                    $"呼叫中心服务授权请求失败,HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
+            var token = response.Headers?.FirstOrDefault(d => d.Name == _tokenKey);
+            if (token is null)
+                throw new HttpRequestException("呼叫中心服务授权失败");
+            if (token?.Value is null)
+                throw new HttpRequestException("呼叫中心token无效");
+
+            return token.Value.ToString();
+        }
+        catch (Exception e)
+        {
+            throw new HttpRequestException($"呼叫中心授权请求失败,Error: {e.Message}");
+        }
+
+    }
+}

+ 13 - 27
src/Tr.Sdk/TrClient.cs

@@ -1,11 +1,10 @@
 using System.Net.Http.Json;
 using System.Text.Json;
 using RestSharp;
-using RestSharp.Authenticators;
 
 namespace Tr.Sdk;
 
-public partial class TrClient : ITrClient, IDisposable
+public class TrClient : ITrClient, IDisposable
 {
     private readonly RestClient _client;
 
@@ -13,7 +12,7 @@ public partial class TrClient : ITrClient, IDisposable
     {
         var options = new RestClientOptions(baseUrl)
         {
-            Authenticator = new TrAuthenticator()
+            Authenticator = new TrAuthenticator(baseUrl, apiKey, apiSecret)
         };
         _client = new RestClient(options);
     }
@@ -26,23 +25,23 @@ public partial class TrClient : ITrClient, IDisposable
     /// <param name="request"></param>
     /// <param name="cancellationToken"></param>
     /// <returns></returns>
-    public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
+    public async Task<TResponse> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
         where TRequest : TrRequest
     {
-        var req = new RestRequest(request.Path(), request.Method())
+        var req = new RestRequest(request.Path(), request.HttpMethod())
             .AddObject(request);
+        try
+        {
+            var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
+            if (!response?.IsSuccessful ?? false)
+                throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
 
-        var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
-        if (!response.IsSuccessful)
+            return response.Data;
+        }
+        catch (Exception e)
         {
-            throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
+            throw new HttpRequestException($"呼叫中心请求失败,Error: {e.Message}");
         }
-        return response.Data;
-    }
-
-    public async Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken)
-    {
-        throw new NotImplementedException();
     }
 
     /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
@@ -51,17 +50,4 @@ public partial class TrClient : ITrClient, IDisposable
         _client?.Dispose();
         GC.SuppressFinalize(this);
     }
-}
-
-public class TrAuthenticator : AuthenticatorBase
-{
-    public TrAuthenticator() : base("")
-    {
-    }
-
-    protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
-    {
-        Token = string.IsNullOrEmpty(Token) ? "8352d42165d7cf6821e53e926d66adba54c309e6" : Token;
-        return new HeaderParameter("X-Auth-Token", Token);
-    }
 }

+ 9 - 0
src/Tr.Sdk/TrRequest.cs

@@ -0,0 +1,9 @@
+using RestSharp;
+
+namespace Tr.Sdk;
+
+public abstract class TrRequest
+{
+    public abstract string Path();
+    public abstract Method HttpMethod();
+}

+ 0 - 13
src/Tr.Sdk/TrResponse.cs

@@ -1,13 +0,0 @@
-namespace Tr.Sdk;
-
-public class TrResponse
-{
-    public int HttpCode { get; set; }
-
-    public bool IsSuccess { get; set; }
-}
-
-public class TrResponse<TData> : TrResponse
-{
-    public TData Data { get; set; }
-}

+ 1 - 1
src/Tr.Sdk/TrSdkStartupExtensions.cs

@@ -11,7 +11,7 @@ namespace Tr.Sdk
     {
         public static IServiceCollection AddTrSdk(this IServiceCollection services, string baseUrl, string apiKey, string apiSecret)
         {
-            services.AddScoped<ITrClient, TrClient>(_ => new TrClient(baseUrl, apiKey, apiSecret));
+            services.AddSingleton<ITrClient, TrClient>(_ => new TrClient(baseUrl, apiKey, apiSecret));
             return services;
         }
     }