EnterpriseService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using Hotline.Enterprise;
  2. using Hotline.Share.Dtos;
  3. using RestSharp;
  4. using System.Collections;
  5. using System.Text;
  6. using Hotline.Share.Dtos.Enterprise;
  7. using XF.Domain.Cache;
  8. using XF.Domain.Exceptions;
  9. namespace Hotline.YbEnterprise.Sdk
  10. {
  11. public class EnterpriseService : IEnterpriseService
  12. {
  13. private readonly RestClient _client;
  14. private readonly EnterpriseConfig _config;
  15. public EnterpriseService(EnterpriseConfig config)
  16. {
  17. _client = new RestClient();
  18. _config = config;
  19. }
  20. /// <summary>
  21. /// 获取企业列表
  22. /// </summary>
  23. /// <param name="name"></param>
  24. /// <param name="current"></param>
  25. /// <param name="size"></param>
  26. /// <param name="cancellationToken"></param>
  27. /// <returns></returns>
  28. public async Task<EnterpriseListData> GetEnterpriseList(string name = "", int current = 1, int size = 10, CancellationToken cancellationToken =default, ITypedCache<EnterpriseVo> cacheResponse = default)
  29. {
  30. var request = new EnterpriseListRequest
  31. {
  32. EnterpriseName = name,
  33. Current = current,
  34. Size = size
  35. };
  36. var token = await cacheResponse.GetAsync("EnterpriseResponse", cancellationToken);
  37. if (token == null || (token != null && token.EndTime > DateTime.Now))
  38. {
  39. token = await GetTokenAsync(cancellationToken);
  40. token.EndTime = DateTime.Now.AddMinutes(45);
  41. await cacheResponse.SetAsync("EnterpriseResponse",token, cancellationToken: cancellationToken);
  42. }
  43. var path = _config.AddressUrl + "platform/12345/selectEnterpriseList";
  44. var rest = new RestRequest(path, Method.Post);
  45. rest.AddHeader("content-type", "application/json");
  46. rest.AddHeader("Blade-Auth", token.TokenType + " " + token.AccessToken);
  47. var res = await ExecuteAsync<EnterpriseListRequest, EnterpriseListResponse>(path, Method.Post, request, rest, cancellationToken);
  48. return res == null? null: res.data;
  49. }
  50. /// <summary>
  51. /// 获取TOKEN
  52. /// </summary>
  53. /// <param name="cancellationToken"></param>
  54. /// <returns></returns>
  55. /// <exception cref="UserFriendlyException"></exception>
  56. public async Task<EnterpriseVo> GetTokenAsync(CancellationToken cancellationToken) {
  57. string authorization = _config.ClientId + ":" + _config.ClientSecret;
  58. authorization = Encoder.Base64Code(authorization);
  59. authorization = authorization.Replace("@", "=");
  60. var path = _config.AddressUrl + "blade-auth/oauth/getAccessToken";
  61. var rest = new RestRequest(path, Method.Post);
  62. rest.AddHeader("content-type", "application/json");
  63. rest.AddHeader("Authorization", "Basic " + authorization);
  64. rest.AddHeader("Tenant-Id", _config.TenantId);
  65. var res = await ExecuteAsync<string,EnterpriseServiceResponse>(path, Method.Post,"", rest, cancellationToken);
  66. if (res is null)
  67. throw new UserFriendlyException("获取token请求失败");
  68. if (!res.success)
  69. throw new UserFriendlyException("获取token请求失败");
  70. return res.data;
  71. }
  72. public async Task<TResponse> ExecuteAsync<TRequest, TResponse>(string path, Method httpMethod, TRequest request, RestRequest restRequest = null,
  73. CancellationToken cancellationToken = default)
  74. where TRequest : class
  75. {
  76. var req = new RestRequest(path, httpMethod);
  77. if (httpMethod is Method.Get)
  78. {
  79. req.AddObject(request);
  80. }
  81. else
  82. {
  83. req.AddJsonBody(request);
  84. }
  85. try
  86. {
  87. var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
  88. return response.Data;
  89. }
  90. catch (Exception e)
  91. {
  92. throw new HttpRequestException($"企业服务数据错误,Error: {e.Message}");
  93. }
  94. }
  95. public static class Encoder
  96. {
  97. /// <summary>
  98. /// 对string 进行 Base64 编码
  99. /// </summary>
  100. /// <param name="strMessage">string 参数</param>
  101. /// <returns> Base64 编码</returns>
  102. public static string Base64Code(string strMessage)
  103. {
  104. bool flag = string.IsNullOrEmpty(strMessage);
  105. string result;
  106. if (flag)
  107. {
  108. result = "";
  109. }
  110. else
  111. {
  112. char[] array = new char[]
  113. {
  114. 'A',
  115. 'B',
  116. 'C',
  117. 'D',
  118. 'E',
  119. 'F',
  120. 'G',
  121. 'H',
  122. 'I',
  123. 'J',
  124. 'K',
  125. 'L',
  126. 'M',
  127. 'N',
  128. 'O',
  129. 'P',
  130. 'Q',
  131. 'R',
  132. 'S',
  133. 'T',
  134. 'U',
  135. 'V',
  136. 'W',
  137. 'X',
  138. 'Y',
  139. 'Z',
  140. 'a',
  141. 'b',
  142. 'c',
  143. 'd',
  144. 'e',
  145. 'f',
  146. 'g',
  147. 'h',
  148. 'i',
  149. 'j',
  150. 'k',
  151. 'l',
  152. 'm',
  153. 'n',
  154. 'o',
  155. 'p',
  156. 'q',
  157. 'r',
  158. 's',
  159. 't',
  160. 'u',
  161. 'v',
  162. 'w',
  163. 'x',
  164. 'y',
  165. 'z',
  166. '0',
  167. '1',
  168. '2',
  169. '3',
  170. '4',
  171. '5',
  172. '6',
  173. '7',
  174. '8',
  175. '9',
  176. '+',
  177. '/',
  178. '='
  179. };
  180. byte b = 0;
  181. ArrayList arrayList = new ArrayList(Encoding.Default.GetBytes(strMessage));
  182. int count = arrayList.Count;
  183. int num = count / 3;
  184. int num2 = count % 3;
  185. bool flag2 = num2 > 0;
  186. if (flag2)
  187. {
  188. for (int i = 0; i < 3 - num2; i++)
  189. {
  190. arrayList.Add(b);
  191. }
  192. num++;
  193. }
  194. StringBuilder stringBuilder = new StringBuilder(num * 4);
  195. for (int i = 0; i < num; i++)
  196. {
  197. byte[] array2 = new byte[]
  198. {
  199. (byte)arrayList[i * 3],
  200. (byte)arrayList[i * 3 + 1],
  201. (byte)arrayList[i * 3 + 2]
  202. };
  203. int[] array3 = new int[4];
  204. array3[0] = array2[0] >> 2;
  205. array3[1] = ((int)(array2[0] & 3) << 4 ^ array2[1] >> 4);
  206. bool flag3 = !array2[1].Equals(b);
  207. if (flag3)
  208. {
  209. array3[2] = ((int)(array2[1] & 15) << 2 ^ array2[2] >> 6);
  210. }
  211. else
  212. {
  213. array3[2] = 64;
  214. }
  215. bool flag4 = !array2[2].Equals(b);
  216. if (flag4)
  217. {
  218. array3[3] = (int)(array2[2] & 63);
  219. }
  220. else
  221. {
  222. array3[3] = 64;
  223. }
  224. stringBuilder.Append(array[array3[0]]);
  225. stringBuilder.Append(array[array3[1]]);
  226. stringBuilder.Append(array[array3[2]]);
  227. stringBuilder.Append(array[array3[3]]);
  228. }
  229. string text = stringBuilder.ToString();
  230. text = text.Replace("=", "@");
  231. text = text.Replace("+", "%2B");
  232. result = text;
  233. }
  234. return result;
  235. }
  236. }
  237. }
  238. }