SysCommonService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Azure.Core;
  7. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  8. using Org.BouncyCastle.Crypto.Parameters;
  9. using Org.BouncyCastle.Utilities.Encoders;
  10. using Swashbuckle.AspNetCore.SwaggerGen;
  11. namespace Admin.NET.Core.Service;
  12. /// <summary>
  13. /// 系统通用服务 🧩
  14. /// </summary>
  15. [ApiDescriptionSettings(Order = 101)]
  16. [AllowAnonymous]
  17. public class SysCommonService : IDynamicApiController, ITransient
  18. {
  19. private readonly IApiDescriptionGroupCollectionProvider _apiProvider;
  20. private readonly SqlSugarRepository<SysUser> _sysUserRep;
  21. private readonly CDConfigOptions _cdConfigOptions;
  22. private readonly UserManager _userManager;
  23. private readonly HttpClient _httpClient;
  24. public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider,
  25. SqlSugarRepository<SysUser> sysUserRep,
  26. IOptions<CDConfigOptions> giteeOptions,
  27. IHttpClientFactory httpClientFactory,
  28. UserManager userManager)
  29. {
  30. _sysUserRep = sysUserRep;
  31. _apiProvider = apiProvider;
  32. _userManager = userManager;
  33. _cdConfigOptions = giteeOptions.Value;
  34. _httpClient = httpClientFactory.CreateClient();
  35. }
  36. /// <summary>
  37. /// 获取国密公钥私钥对 🏆
  38. /// </summary>
  39. /// <returns></returns>
  40. [DisplayName("获取国密公钥私钥对")]
  41. public SmKeyPairOutput GetSmKeyPair()
  42. {
  43. var kp = GM.GenerateKeyPair();
  44. var privateKey = Hex.ToHexString(((ECPrivateKeyParameters)kp.Private).D.ToByteArray()).ToUpper();
  45. var publicKey = Hex.ToHexString(((ECPublicKeyParameters)kp.Public).Q.GetEncoded()).ToUpper();
  46. return new SmKeyPairOutput
  47. {
  48. PrivateKey = privateKey,
  49. PublicKey = publicKey,
  50. };
  51. }
  52. /// <summary>
  53. /// 获取所有接口/动态API 🔖
  54. /// </summary>
  55. /// <returns></returns>
  56. [DisplayName("获取所有接口/动态API")]
  57. public List<ApiOutput> GetApiList()
  58. {
  59. var apiList = new List<ApiOutput>();
  60. foreach (var item in _apiProvider.ApiDescriptionGroups.Items)
  61. {
  62. foreach (var apiDescription in item.Items)
  63. {
  64. var displayName = apiDescription.TryGetMethodInfo(out MethodInfo apiMethodInfo) ? apiMethodInfo.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName : "";
  65. apiList.Add(new ApiOutput
  66. {
  67. GroupName = item.GroupName,
  68. DisplayName = displayName,
  69. RouteName = apiDescription.RelativePath
  70. });
  71. }
  72. }
  73. return apiList;
  74. }
  75. /// <summary>
  76. /// 下载标记错误的临时Excel(全局)
  77. /// </summary>
  78. /// <returns></returns>
  79. [DisplayName("下载标记错误的临时Excel(全局)")]
  80. public async Task<IActionResult> DownloadErrorExcelTemp([FromQuery] string fileName = null)
  81. {
  82. var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
  83. var resultStream = App.GetRequiredService<SysCacheService>().Get<MemoryStream>(CacheConst.KeyExcelTemp + userId);
  84. if (resultStream == null) throw Oops.Oh("错误标记文件已过期。");
  85. return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream")
  86. {
  87. FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记_" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx"
  88. });
  89. }
  90. /// <summary>
  91. /// 加密字符串 🔖
  92. /// </summary>
  93. /// <returns></returns>
  94. [SuppressMonitor]
  95. [DisplayName("加密字符串")]
  96. public dynamic EncryptPlainText([Required] string plainText)
  97. {
  98. return CryptogramUtil.Encrypt(plainText);
  99. }
  100. /// <summary>
  101. /// 接口压测 🔖
  102. /// </summary>
  103. /// <returns></returns>
  104. [DisplayName("接口压测")]
  105. public async Task<StressTestOutput> StressTest(StressTestInput input)
  106. {
  107. // 限制仅超管用户才能使用此功能
  108. if (!_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.SA001);
  109. var stopwatch = new Stopwatch();
  110. var responseTimes = new List<double>();
  111. long totalRequests = 0, successfulRequests = 0, failedRequests = 0;
  112. stopwatch.Start();
  113. var semaphore = new SemaphoreSlim(input.MaxDegreeOfParallelism!.Value > 0 ? input.MaxDegreeOfParallelism.Value : Environment.ProcessorCount);
  114. var tasks = Enumerable.Range(0, input.NumberOfRounds!.Value * input.NumberOfRequests!.Value).Select(async _ =>
  115. {
  116. await semaphore.WaitAsync();
  117. try
  118. {
  119. var requestStopwatch = new Stopwatch();
  120. requestStopwatch.Start();
  121. // 构建完整的请求URI,包括路径参数和查询参数
  122. var uriBuilder = new UriBuilder(input.RequestUri);
  123. var queryString = HttpUtility.ParseQueryString(uriBuilder.Query);
  124. foreach (var param in input.PathParameters)
  125. {
  126. uriBuilder.Path = uriBuilder.Path.Replace($"{{{param.Key}}}", param.Value, StringComparison.OrdinalIgnoreCase);
  127. }
  128. foreach (var param in input.QueryParameters)
  129. {
  130. queryString[param.Key] = param.Value;
  131. }
  132. uriBuilder.Query = queryString.ToString() ?? string.Empty;
  133. var fullUri = uriBuilder.Uri.ToString();
  134. HttpRequestMessage request = input.RequestMethod.ToUpper() switch
  135. {
  136. "GET" => new HttpRequestMessage(HttpMethod.Get, fullUri),
  137. "PUT" => new HttpRequestMessage(HttpMethod.Put, fullUri),
  138. "POST" => new HttpRequestMessage(HttpMethod.Post, fullUri),
  139. "DELETE" => new HttpRequestMessage(HttpMethod.Delete, fullUri),
  140. _ => throw Oops.Bah("请求方式异常")
  141. };
  142. // 设置请求头
  143. foreach (var header in input.Headers)
  144. {
  145. request.Headers.Add(header.Key, header.Value);
  146. }
  147. if (nameof(HttpMethod.Get).EqualIgnoreCase(input.RequestMethod) && input.RequestParameters.Any())
  148. {
  149. var content = new FormUrlEncodedContent(input.RequestParameters);
  150. request.Content = content;
  151. }
  152. var response = await _httpClient.SendAsync(request);
  153. response.EnsureSuccessStatusCode(); // 抛出错误状态码异常
  154. requestStopwatch.Stop();
  155. responseTimes.Add(requestStopwatch.Elapsed.TotalMilliseconds);
  156. Interlocked.Increment(ref successfulRequests);
  157. }
  158. catch
  159. {
  160. Interlocked.Increment(ref failedRequests);
  161. }
  162. finally
  163. {
  164. Interlocked.Increment(ref totalRequests);
  165. semaphore.Release();
  166. }
  167. });
  168. await Task.WhenAll(tasks);
  169. stopwatch.Stop();
  170. var totalTimeInSeconds = stopwatch.Elapsed.TotalSeconds;
  171. var qps = totalTimeInSeconds > 0 ? totalRequests / totalTimeInSeconds : 0;
  172. var averageResponseTime = responseTimes.Any() ? responseTimes.Average() : 0;
  173. var minResponseTime = responseTimes.Any() ? responseTimes.Min() : 0;
  174. var maxResponseTime = responseTimes.Any() ? responseTimes.Max() : 0;
  175. return new StressTestOutput
  176. {
  177. TotalRequests = totalRequests,
  178. TotalTimeInSeconds = totalTimeInSeconds,
  179. SuccessfulRequests = successfulRequests,
  180. FailedRequests = failedRequests,
  181. QueriesPerSecond = qps,
  182. MinResponseTime = minResponseTime,
  183. MaxResponseTime = maxResponseTime,
  184. AverageResponseTime = averageResponseTime,
  185. Percentile10ResponseTime = CalculatePercentile(responseTimes, 0.1),
  186. Percentile25ResponseTime = CalculatePercentile(responseTimes, 0.25),
  187. Percentile50ResponseTime = CalculatePercentile(responseTimes, 0.5),
  188. Percentile75ResponseTime = CalculatePercentile(responseTimes, 0.75),
  189. Percentile90ResponseTime = CalculatePercentile(responseTimes, 0.9),
  190. Percentile99ResponseTime = CalculatePercentile(responseTimes, 0.99),
  191. Percentile999ResponseTime = CalculatePercentile(responseTimes, 0.999)
  192. };
  193. }
  194. /// <summary>
  195. /// 计算百分位请求耗时
  196. /// </summary>
  197. /// <param name="times">请求耗时列表</param>
  198. /// <param name="percentile">百分位</param>
  199. /// <returns></returns>
  200. private double CalculatePercentile(List<double> times, double percentile)
  201. {
  202. if (!times.Any()) return 0;
  203. var sortedTimes = times.OrderBy(t => t).ToList();
  204. var index = (int)Math.Ceiling(percentile * sortedTimes.Count) - 1;
  205. return sortedTimes[index < sortedTimes.Count ? index : sortedTimes.Count - 1];
  206. }
  207. }