|
@@ -9,239 +9,248 @@ using XF.Domain.Exceptions;
|
|
|
|
|
|
namespace Hotline.YbEnterprise.Sdk
|
|
|
{
|
|
|
- public class EnterpriseService : IEnterpriseService
|
|
|
- {
|
|
|
- private readonly RestClient _client;
|
|
|
- private readonly EnterpriseConfig _config;
|
|
|
+ public class EnterpriseService : IEnterpriseService
|
|
|
+ {
|
|
|
+ private readonly RestClient _client;
|
|
|
+ private readonly EnterpriseConfig _config;
|
|
|
|
|
|
- public EnterpriseService(EnterpriseConfig config)
|
|
|
- {
|
|
|
- _client = new RestClient();
|
|
|
- _config = config;
|
|
|
- }
|
|
|
+ public EnterpriseService(EnterpriseConfig config)
|
|
|
+ {
|
|
|
+ _client = new RestClient();
|
|
|
+ _config = config;
|
|
|
+ }
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 获取企业列表
|
|
|
- /// </summary>
|
|
|
- /// <param name="name"></param>
|
|
|
- /// <param name="current"></param>
|
|
|
- /// <param name="size"></param>
|
|
|
- /// <param name="cancellationToken"></param>
|
|
|
- /// <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);
|
|
|
- if (token == null || (token != null && token.EndTime > DateTime.Now))
|
|
|
- {
|
|
|
- token = await GetTokenAsync(cancellationToken);
|
|
|
- token.EndTime = DateTime.Now.AddMinutes(45);
|
|
|
- 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);
|
|
|
- var res = await ExecuteAsync<EnterpriseListRequest, EnterpriseListResponse>(path, Method.Post, request, rest, cancellationToken);
|
|
|
- return res == null? null: res.data;
|
|
|
- }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取企业列表
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="name"></param>
|
|
|
+ /// <param name="current"></param>
|
|
|
+ /// <param name="size"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <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);
|
|
|
+ if (token == null || (token != null && token.EndTime > DateTime.Now))
|
|
|
+ {
|
|
|
+ token = await GetTokenAsync(cancellationToken);
|
|
|
+ token.EndTime = DateTime.Now.AddMinutes(45);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 获取TOKEN
|
|
|
- /// </summary>
|
|
|
- /// <param name="cancellationToken"></param>
|
|
|
- /// <returns></returns>
|
|
|
- /// <exception cref="UserFriendlyException"></exception>
|
|
|
- public async Task<EnterpriseVo> GetTokenAsync(CancellationToken cancellationToken) {
|
|
|
+ /// <summary>
|
|
|
+ /// 获取TOKEN
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="UserFriendlyException"></exception>
|
|
|
+ public async Task<EnterpriseVo> GetTokenAsync(CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
|
|
|
- string authorization = _config.ClientId + ":" + _config.ClientSecret;
|
|
|
- authorization = Encoder.Base64Code(authorization);
|
|
|
- authorization = authorization.Replace("@", "=");
|
|
|
+ string authorization = _config.ClientId + ":" + _config.ClientSecret;
|
|
|
+ authorization = Encoder.Base64Code(authorization);
|
|
|
+ authorization = authorization.Replace("@", "=");
|
|
|
|
|
|
- var path = _config.AddressUrl + "blade-auth/oauth/getAccessToken";
|
|
|
- var rest = new RestRequest(path, Method.Post);
|
|
|
- rest.AddHeader("content-type", "application/json");
|
|
|
- rest.AddHeader("Authorization", "Basic " + authorization);
|
|
|
- rest.AddHeader("Tenant-Id", _config.TenantId);
|
|
|
- var res = await ExecuteAsync<string,EnterpriseServiceResponse>(path, Method.Post,"", rest, cancellationToken);
|
|
|
+ var path = _config.AddressUrl + "blade-auth/oauth/getAccessToken";
|
|
|
+ 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());
|
|
|
- if (!res.success)
|
|
|
- throw new UserFriendlyException("获取token请求失败 authorization:" + authorization + " ,path:" + path + " , res:" + res.ToString());
|
|
|
+ if (res is null)
|
|
|
+ throw new UserFriendlyException("获取token请求失败 authorization:" + authorization + " ,path:" + path + " , res:" + res.ToString());
|
|
|
+ if (!res.success)
|
|
|
+ throw new UserFriendlyException("获取token请求失败 authorization:" + 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,
|
|
|
- CancellationToken cancellationToken = default)
|
|
|
- where TRequest : class
|
|
|
- {
|
|
|
- var req = new RestRequest(path, httpMethod);
|
|
|
- if (httpMethod is Method.Get)
|
|
|
- {
|
|
|
- req.AddObject(request);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- req.AddJsonBody(request);
|
|
|
- }
|
|
|
+ }
|
|
|
+ public async Task<TResponse> ExecuteAsync<TRequest, TResponse>(string path, Method httpMethod, TRequest request, RestRequest restRequest = null,
|
|
|
+ CancellationToken cancellationToken = default)
|
|
|
+ where TRequest : class
|
|
|
+ {
|
|
|
+ var req = new RestRequest(path, httpMethod);
|
|
|
+ if (httpMethod is Method.Get)
|
|
|
+ {
|
|
|
+ req.AddObject(request);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ req.AddJsonBody(request);
|
|
|
+ }
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
|
|
|
- return response.Data;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- throw new HttpRequestException($"企业服务数据错误,Error: {e.Message}");
|
|
|
- }
|
|
|
- }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
|
|
|
+ return response.Data;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ throw new HttpRequestException($"企业服务数据错误,Error: {e.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|