xf 1 éve
szülő
commit
d0789162b4

+ 13 - 4
src/Hotline.Api/Controllers/TestController.cs

@@ -13,11 +13,12 @@ using Hotline.Users;
 using MediatR;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.SignalR;
 using Microsoft.Extensions.Options;
 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;
@@ -57,6 +58,7 @@ public class TestController : BaseController
 
     private readonly ITimeLimitDomainService _timeLimitDomainService;
     private readonly IWfModuleDomainService _wfModuleDomainService;
+    private readonly ITrClient _trClient;
 
 
     //private readonly ITypedCache<List<User>> _cache;
@@ -85,7 +87,9 @@ public class TestController : BaseController
         IRepository<OrderUrge> orderUrgeRepository,
         IRepositoryTextSearch<OrderTs> repositoryts,
         ITimeLimitDomainService timeLimitDomainService,
-        IWfModuleDomainService wfModuleDomainService)
+        IWfModuleDomainService wfModuleDomainService,
+        ITrClient trClient
+        )
     {
         _logger = logger;
         _authorizeGenerator = authorizeGenerator;
@@ -106,6 +110,7 @@ public class TestController : BaseController
         _repositoryts = repositoryts;
         _timeLimitDomainService = timeLimitDomainService;
         _wfModuleDomainService = wfModuleDomainService;
+        _trClient = trClient;
     }
 
     [HttpGet("time")]
@@ -128,11 +133,15 @@ public class TestController : BaseController
         //int a = _timeLimitDomainService.CalcWorkTime(DateTime.Now, DateTime.Parse("2023-09-11 16:21:00"));
         //int m = _timeLimitDomainService.CalcWorkTime(DateTime.Parse("2023-09-19 12:00:00"), DateTime.Parse("2023-09-20 18:00:00"), false);
         //var r = _timeLimitDomainService.CalcEndTime(DateTime.Parse("2023-09-19 18:00:00"), "35", false);
-        await _wfModuleDomainService.PersistenceModulesAsync(HttpContext.RequestAborted);
-        return OpenResponse.Ok(DateTime.Now.ToString("F"));
+        //await _wfModuleDomainService.PersistenceModulesAsync(HttpContext.RequestAborted);
 
         //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"));
     }
 
     [HttpGet("pgsql")]

+ 22 - 7
src/Hotline.Api/Program.cs

@@ -1,4 +1,5 @@
 using System.Text.Json.Serialization;
+using System.Xml.Linq;
 using Hotline.Api;
 using Newtonsoft.Json;
 using RestSharp;
@@ -51,9 +52,21 @@ Log.Information("Hotline service is Starting up");
 //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 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
@@ -99,11 +112,13 @@ finally
 
 class Pa
 {
-    [JsonProperty(PropertyName = "phone")]
-    [JsonPropertyName("phone")]
+    //[JsonProperty(PropertyName = "phone")]
+    //[JsonPropertyName("phone")]
+    [RequestProperty(Name = "phone")]
     public string Phone { get; set; }
 
-    [JsonProperty(PropertyName = "special_flag")]
-    [JsonPropertyName("special_flag")]
+    //[JsonProperty(PropertyName = "special_flag")]
+    //[JsonPropertyName("special_flag")]
+    [RequestProperty(Name = "special_flag")]
     public int SpecialFlag { get; set; }
 }

+ 5 - 2
src/Hotline.Api/StartupExtensions.cs

@@ -12,10 +12,9 @@ using Hotline.Wex;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Identity;
 using Serilog;
-using System.Text.Json;
 using Fw.Utility.Client;
 using Mapster;
-using MapsterMapper;
+using Tr.Sdk;
 using XF.Domain.Dependency;
 using XF.Domain.Filters;
 using XF.Domain.Options;
@@ -89,6 +88,10 @@ internal static class StartupExtensions
         //wex
         services.AddWex(callCenterConfiguration.Wex.Address);
 
+        //tr
+        var trConfig = configuration.GetRequiredSection("Tr").Get<TrConfiguration>();
+        services.AddTrSdk(trConfig.Address, trConfig.Username, trConfig.Password);
+
         //sqlsugar
         services.AddSqlSugar(configuration);
         services.AddWexDb(configuration);

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

@@ -13,11 +13,6 @@
       "Address": "http://222.212.82.225:8083",
       "Username": "admin",
       "Password": "Wex@12345"
-    },
-    "Tr": {
-      "Address": "http://internal.ttf-cti.com:8080",
-      "Username": "yscs",
-      "Password": "123456"
     }
   },
   "ConnectionStrings": {
@@ -93,6 +88,11 @@
     "ServerAddresses": [ "http://110.188.24.28:8848" ],
     "Namespace": "17503980-9b0d-4d3e-8e35-c842c41fb888", //debug
     "ServiceName": "hotline"
+  },
+  "Tr": {
+    "Address": "http://internal.ttf-cti.com:8080",
+    "Username": "yscs",
+    "Password": "123456"
   }
 
 }

+ 5 - 5
src/Hotline.Api/config/appsettings.json

@@ -13,11 +13,6 @@
       "Address": "http://222.212.82.225:8083",
       "Username": "admin",
       "Password": "Wex@12345"
-    },
-    "Tr": {
-      "Address": "http://internal.ttf-cti.com:8080",
-      "Username": "yscs",
-      "Password": "123456"
     }
   },
   "ConnectionStrings": {
@@ -93,5 +88,10 @@
     "ServerAddresses": [ "http://110.188.24.28:8848" ],
     "Namespace": "f5017bc5-af0a-4f85-8e38-6718accc8f36", //dev
     "ServiceName": "hotline"
+  },
+  "Tr": {
+    "Address": "http://internal.ttf-cti.com:8080",
+    "Username": "yscs",
+    "Password": "123456"
   }
 }

+ 0 - 2
src/Hotline/CallCenter/Devices/CallCenterConfiguration.cs

@@ -7,6 +7,4 @@ public class CallCenterConfiguration
     public DeviceConfigs DeviceConfigs { get; set; }
 
     public WexConfiguration Wex { get; set; }
-
-    public TrConfiguration Tr { get; set; }
 }

+ 15 - 0
src/NewRock.Sdk/XsConfiguration.cs

@@ -0,0 +1,15 @@
+namespace NewRock.Sdk
+{
+    public class XsConfiguration
+    {
+        public string Address { get; set; }
+        public bool Authorize { get; set; }
+        public string ReceiveKey { get; set; }
+        public string SendKey { get; set; }
+
+        /// <summary>
+        /// 认证过期时间(秒)
+        /// </summary>
+        public int Expired { get; set; }
+    }
+}

+ 15 - 9
src/Tr.Sdk/ITrClient.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Text.Json.Serialization;
 using System.Threading.Tasks;
+using RestSharp;
 
 namespace Tr.Sdk
 {
@@ -17,9 +18,8 @@ namespace Tr.Sdk
         /// <param name="request"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        Task<TResponse?> ExecuteAsync<TRequest, TResponse>(
-            TRequest request,
-            CancellationToken cancellationToken) where TRequest : ITrRequest;
+        Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
+            where TRequest : TrRequest;
 
         Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken);
     }
@@ -28,13 +28,13 @@ namespace Tr.Sdk
     {
         public TokenRequest()
         {
-            Url = "login/token";
-            Method = HttpMethod.Get;
+            Path = "login/token";
+            Method = Method.Get;
         }
 
-        public string Url { get; init; }
+        public string Path { get; init; }
 
-        public HttpMethod Method { get; init; }
+        public Method Method { get; init; }
 
         public string Username { get; set; }
 
@@ -44,9 +44,15 @@ namespace Tr.Sdk
     public interface ITrRequest
     {
         [JsonIgnore]
-        string Url { get; }
+        string Path { get; }
 
         [JsonIgnore]
-        HttpMethod Method { get; }
+        Method Method { get; }
+    }
+
+    public abstract class TrRequest
+    {
+        public abstract string Path();
+        public abstract Method Method();
     }
 }

+ 20 - 0
src/Tr.Sdk/Tel/QueryTelRequest.cs

@@ -0,0 +1,20 @@
+using System.Text.Json.Serialization;
+using RestSharp;
+
+namespace Tr.Sdk.Tel
+{
+    public class QueryTelRequest : TrRequest
+    {
+        public override string Path() => "api/ola/extensions/detail";
+
+
+        public override Method Method() => RestSharp.Method.Get;
+    }
+
+    public class QueryTelResponse
+    {
+        [JsonPropertyName("uuid")]
+        public string Id { get; set; }
+        public string Name { get; set; }
+    }
+}

+ 60 - 68
src/Tr.Sdk/TrClient.cs

@@ -1,75 +1,67 @@
-//using System.Net.Http.Json;
-//using System.Text.Json;
+using System.Net.Http.Json;
+using System.Text.Json;
+using RestSharp;
+using RestSharp.Authenticators;
 
-//namespace Tr.Sdk;
+namespace Tr.Sdk;
 
-//public partial class TrClient : ITrClient
-//{
-//    private readonly IHttpClientFactory _httpClientFactory;
+public partial class TrClient : ITrClient, IDisposable
+{
+    private readonly RestClient _client;
 
-//    public TrClient(IHttpClientFactory httpClientFactory)
-//    {
-//        _httpClientFactory = httpClientFactory;
-//    }
+    public TrClient(string baseUrl, string apiKey, string apiSecret)
+    {
+        var options = new RestClientOptions(baseUrl)
+        {
+            Authenticator = new TrAuthenticator()
+        };
+        _client = new RestClient(options);
+    }
 
-//    /// <summary>
-//    /// 执行操作呼叫中心请求
-//    /// </summary>
-//    /// <typeparam name="TRequest"></typeparam>
-//    /// <typeparam name="TResponse"></typeparam>
-//    /// <param name="request"></param>
-//    /// <param name="cancellationToken"></param>
-//    /// <returns></returns>
-//    public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken) where TRequest : ITrRequest
-//    {
-//        //var httpClient = _httpClientFactory.CreateClient(Defaults.ServiceName);
-//        //var token = await _tokenManager.GetTokenAsync(cancellationToken);
-//        //httpClient.DefaultRequestHeaders.Add("X-Auth-Token", token);
-//        //try
-//        //{
-//        //    if (request.Method == HttpMethod.Get)
-//        //    {
-//        //        return await httpClient.GetFromJsonAsync<TResponse>(request.Url, cancellationToken);
-//        //    }
-//        //    else if (request.Method == HttpMethod.Post)
-//        //    {
-//        //        using var content = new FormUrlEncodedContent
-//        //        await httpClient.PostAsync(request.Url, new FormUrlEncodedContent(), cancellationToken);
-//        //    }
-//        //    else
-//        //    {
-//        //        throw new ArgumentOutOfRangeException();
-//        //    }
+    /// <summary>
+    /// 执行操作呼叫中心请求
+    /// </summary>
+    /// <typeparam name="TRequest"></typeparam>
+    /// <typeparam name="TResponse"></typeparam>
+    /// <param name="request"></param>
+    /// <param name="cancellationToken"></param>
+    /// <returns></returns>
+    public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
+        where TRequest : TrRequest
+    {
+        var req = new RestRequest(request.Path(), request.Method())
+            .AddObject(request);
 
-//        //    var rsp = await httpClient.PostAsJsonAsync(request.Url, request, cancellationToken: cancellationToken);
-//        //    if (!rsp.IsSuccessStatusCode)
-//        //        throw new HttpRequestException("请求呼叫中心服务失败");
-//        //    var response = await rsp.Content.ReadFromJsonAsync<TResponse>(new JsonSerializerOptions
-//        //    {
-//        //        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
-//        //    }, cancellationToken);
-//        //    if (response?.Code == 401)
-//        //    {
-//        //        await _tokenManager.RefreshTokenAsync(cancellationToken);
-//        //        response = await rsp.Content.ReadFromJsonAsync<TResponse>(new JsonSerializerOptions
-//        //        {
-//        //            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
-//        //        }, cancellationToken);
-//        //    }
-//        //    if (response?.Code != 200)
-//        //        throw new HttpRequestException(response?.Msg);
-//        //    return response;
-//        //}
-//        //catch (Exception)
-//        //{
-//        //    throw;
-//        //}
+        var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
+        if (!response.IsSuccessful)
+        {
+            throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
+        }
+        return response.Data;
+    }
 
-//        throw new NotImplementedException();
-//    }
+    public async Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken)
+    {
+        throw new NotImplementedException();
+    }
 
-//    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>
+    public void Dispose()
+    {
+        _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);
+    }
+}

+ 1 - 1
src/Hotline/CallCenter/Devices/TrConfiguration.cs → src/Tr.Sdk/TrConfiguration.cs

@@ -1,4 +1,4 @@
-namespace Hotline.CallCenter.Devices;
+namespace Tr.Sdk;
 
 public class TrConfiguration
 {

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

@@ -0,0 +1,13 @@
+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; }
+}

+ 18 - 0
src/Tr.Sdk/TrSdkStartupExtensions.cs

@@ -0,0 +1,18 @@
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tr.Sdk
+{
+    public static class TrSdkStartupExtensions
+    {
+        public static IServiceCollection AddTrSdk(this IServiceCollection services, string baseUrl, string apiKey, string apiSecret)
+        {
+            services.AddScoped<ITrClient, TrClient>(_ => new TrClient(baseUrl, apiKey, apiSecret));
+            return services;
+        }
+    }
+}

+ 8 - 0
src/Wex.Sdk/WexConfiguration.cs

@@ -0,0 +1,8 @@
+namespace Wex.Sdk;
+
+public class WexConfiguration
+{
+    public string Address { get; set; }
+    public string Username { get; set; }
+    public string Password { get; set; }
+}