Procházet zdrojové kódy

Merge branch 'master' of http://git.12345lm.cn/Fengwo/hotline

Dun.Jason před 1 rokem
rodič
revize
9bd1c89c21
1 změnil soubory, kde provedl 89 přidání a 27 odebrání
  1. 89 27
      src/Hotline.YbEnterprise.Sdk/EnterpriseService.cs

+ 89 - 27
src/Hotline.YbEnterprise.Sdk/EnterpriseService.cs

@@ -2,10 +2,13 @@
 using Hotline.Share.Dtos;
 using RestSharp;
 using System.Collections;
+using System.Net;
 using System.Text;
 using Hotline.Share.Dtos.Enterprise;
 using XF.Domain.Cache;
 using XF.Domain.Exceptions;
+using Newtonsoft.Json.Linq;
+using Newtonsoft.Json;
 
 namespace Hotline.YbEnterprise.Sdk
 {
@@ -30,13 +33,18 @@ namespace Hotline.YbEnterprise.Sdk
         /// <returns></returns>
         public async Task<EnterpriseListData> GetEnterpriseList(string name = "", int current = 1, int size = 10, CancellationToken cancellationToken = default, ITypedCache<EnterpriseVo> cacheResponse = default)
         {
-            var request = new EnterpriseListRequest
-            {
-                EnterpriseName = name,
-                Current = current,
-                Size = size
-            };
-            var token = await cacheResponse.GetAsync("EnterpriseResponse", cancellationToken);
+            //var request = new EnterpriseListRequest
+            //{
+            //    EnterpriseName = name,
+            //    Current = current,
+            //    Size = size
+            //};
+            string postJson = "{";
+            postJson += "\"enterpriseName\":\"" + name + "\",";
+            postJson += "\"current\":\"" + current + "\",";
+            postJson += "\"size\":\"" + size + "\"";
+            postJson += "}";
+			var token = await cacheResponse.GetAsync("EnterpriseResponse", cancellationToken);
             if (token == null || (token != null && token.EndTime > DateTime.Now))
             {
                 token = await GetTokenAsync(cancellationToken);
@@ -44,15 +52,25 @@ namespace Hotline.YbEnterprise.Sdk
                 await cacheResponse.SetAsync("EnterpriseResponse", token, cancellationToken: cancellationToken);
             }
             var path = _config.AddressUrl + "platform/12345/selectEnterpriseList";
-            var rest = new RestRequest(path, Method.Post);
-            //rest.AddHeader("content-type", "application/json");
-            //rest.AddHeader("Blade-Auth", token.TokenType + " " + token.AccessToken);
-            rest.AddHeaders(new[] {
-             new KeyValuePair<string, string>("content-type", "application/json"),
-             new KeyValuePair<string, string>("Blade-Auth", token.TokenType + " " + token.AccessToken)
-            });
-            var res = await ExecuteAsync<EnterpriseListRequest, EnterpriseListResponse>(path, Method.Post, request, rest, cancellationToken);
-            return res == null ? null : res.data;
+            //var rest = new RestRequest(path, Method.Post);
+            ////rest.AddHeader("content-type", "application/json");
+            ////rest.AddHeader("Blade-Auth", token.TokenType + " " + token.AccessToken);
+            //rest.AddHeaders(new[] {
+            // new KeyValuePair<string, string>("content-type", "application/json"),
+            // new KeyValuePair<string, string>("Blade-Auth", token.TokenType + " " + token.AccessToken)
+            //});
+
+            List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
+            headers.Add(new KeyValuePair<string, string>("Blade-Auth", token.TokenType + " " + token.AccessToken));
+
+			string strResult = HttpPost(path, postJson, "UTF-8", "application/json", headers);
+            //var res = await ExecuteAsync<EnterpriseListRequest, EnterpriseListResponse>(path, Method.Post, request, rest, cancellationToken);
+            var res = new EnterpriseListResponse();
+			if (false == string.IsNullOrEmpty(strResult))
+			{
+				res = (EnterpriseListResponse)JsonConvert.DeserializeObject(strResult)!;
+			}
+			return res.data;
         }
 
         /// <summary>
@@ -69,20 +87,29 @@ namespace Hotline.YbEnterprise.Sdk
             authorization = authorization.Replace("@", "=");
 
             var path = _config.AddressUrl + "blade-auth/oauth/getAccessToken";
-            var rest = new RestRequest(path, Method.Post);
+            //var rest = new RestRequest(path, Method.Post);
             //rest.AddHeader("content-type", "application/json");
             //rest.AddHeader("Authorization", "Basic " + authorization);
             //rest.AddHeader("Tenant-Id", _config.TenantId);
-            rest.AddHeaders(new[] { 
-                new KeyValuePair<string, string>("content-type", "application/json"),
-	            new KeyValuePair<string, string>("Authorization", "Basic " + authorization),
-	            new KeyValuePair<string, string>("Tenant-Id", _config.TenantId)});
-			var res = await ExecuteAsync<string, EnterpriseServiceResponse>(path, Method.Post, "", rest, cancellationToken);
-
-            if (res is null)
-                throw new UserFriendlyException("获取token请求失败 authorization:" + authorization + " ,path:" + path + " , res:" + res.ToString());
+            //rest.AddHeaders(new[] { 
+            //    new KeyValuePair<string, string>("content-type", "application/json"),
+	           // new KeyValuePair<string, string>("Authorization", "Basic " + authorization),
+	           // new KeyValuePair<string, string>("Tenant-Id", _config.TenantId)});
+            string json = "{\"a\":\"1\"}";
+			//var res = await ExecuteAsync<string, EnterpriseServiceResponse>(path, Method.Post, "", rest, cancellationToken);
+			List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
+			headers.Add(new KeyValuePair<string, string>("Authorization", "Basic " + authorization));
+			headers.Add(new KeyValuePair<string, string>("Tenant-Id", _config.TenantId));
+			string strResult = HttpPost(path, json, "UTF-8", "application/json", headers);
+            EnterpriseServiceResponse? res = new EnterpriseServiceResponse();
+			if (false == string.IsNullOrEmpty(strResult))
+			{
+				 res = (EnterpriseServiceResponse)JsonConvert.DeserializeObject(strResult)!;
+			}
+			if (res is null)
+                throw new UserFriendlyException("获取token请求失败 authorization:" + authorization + " ,path:" + path + " , res:" + strResult);
             if (!res.success)
-                throw new UserFriendlyException("获取token请求失败  authorization:" + authorization + " ,path:" + path + " , res:" + System.Text.Json.JsonSerializer.Serialize(res));
+                throw new UserFriendlyException("获取token请求失败  authorization2:" + authorization + " ,path:" + path + " , res:" + System.Text.Json.JsonSerializer.Serialize(res));
             return res.data;
         }
         public async Task<TResponse> ExecuteAsync<TRequest, TResponse>(string path, Method httpMethod, TRequest request, RestRequest restRequest = null,
@@ -110,7 +137,42 @@ namespace Hotline.YbEnterprise.Sdk
             }
         }
 
-        public static class Encoder
+        public static string HttpPost(string strUrl, string strPar = "", string strEncoding = "UTF-8", string strContentType = "application/x-www-form-urlencoded", List<KeyValuePair<string, string>> headers = null)
+        {
+	        // 返回数据
+	        string strResult = "";
+	        // 网页客户端,用于打开链接,获取返回参数
+	        WebClient wc = new WebClient();
+	        try
+	        {
+		        byte[] responseData = null;
+		        wc.Headers.Add("Content-Type", strContentType);
+		        if (null != headers && headers.Count > 0)
+		        {
+			        headers.ForEach(p =>
+			        {
+				        wc.Headers.Add(p.Key, p.Value);
+			        });
+		        }
+		        byte[] postData = Encoding.GetEncoding(strEncoding).GetBytes(strPar);
+		        responseData = wc.UploadData(strUrl, "POST", postData);
+		        strResult = Encoding.GetEncoding(strEncoding).GetString(responseData);
+		        strResult = strResult.Trim();
+	        }
+	        catch (Exception)
+	        {
+		        strResult = "";
+	        }
+	        finally
+	        {
+		        wc.Dispose();
+		        wc = null;
+	        }
+
+	        return strResult;
+        }
+
+		public static class Encoder
         {
             /// <summary>
             /// 对string 进行 Base64 编码