SysCommonService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  7. using Org.BouncyCastle.Crypto.Parameters;
  8. using Org.BouncyCastle.Utilities.Encoders;
  9. using Swashbuckle.AspNetCore.SwaggerGen;
  10. namespace Admin.NET.Core.Service;
  11. /// <summary>
  12. /// 系统通用服务 🧩
  13. /// </summary>
  14. [ApiDescriptionSettings(Order = 101)]
  15. [AllowAnonymous]
  16. public class SysCommonService : IDynamicApiController, ITransient
  17. {
  18. private readonly IApiDescriptionGroupCollectionProvider _apiProvider;
  19. public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider)
  20. {
  21. _apiProvider = apiProvider;
  22. }
  23. /// <summary>
  24. /// 获取国密公钥私钥对 🏆
  25. /// </summary>
  26. /// <returns></returns>
  27. [DisplayName("获取国密公钥私钥对")]
  28. public SmKeyPairOutput GetSmKeyPair()
  29. {
  30. var kp = GM.GenerateKeyPair();
  31. var privateKey = Hex.ToHexString(((ECPrivateKeyParameters)kp.Private).D.ToByteArray()).ToUpper();
  32. var publicKey = Hex.ToHexString(((ECPublicKeyParameters)kp.Public).Q.GetEncoded()).ToUpper();
  33. return new SmKeyPairOutput
  34. {
  35. PrivateKey = privateKey,
  36. PublicKey = publicKey,
  37. };
  38. }
  39. /// <summary>
  40. /// 获取所有接口/动态API 🔖
  41. /// </summary>
  42. /// <returns></returns>
  43. [DisplayName("获取所有接口/动态API")]
  44. public List<ApiOutput> GetApiList()
  45. {
  46. var apiList = new List<ApiOutput>();
  47. foreach (var item in _apiProvider.ApiDescriptionGroups.Items)
  48. {
  49. foreach (var apiDescription in item.Items)
  50. {
  51. var displayName = apiDescription.TryGetMethodInfo(out MethodInfo apiMethodInfo) ? apiMethodInfo.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName : "";
  52. apiList.Add(new ApiOutput
  53. {
  54. GroupName = item.GroupName,
  55. DisplayName = displayName,
  56. RouteName = apiDescription.RelativePath
  57. });
  58. }
  59. }
  60. return apiList;
  61. }
  62. /// <summary>
  63. /// 下载标记错误的临时Excel(全局)
  64. /// </summary>
  65. /// <returns></returns>
  66. [DisplayName("下载标记错误的临时Excel(全局)")]
  67. public async Task<IActionResult> DownloadErrorExcelTemp([FromQuery] string fileName = null)
  68. {
  69. var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
  70. var resultStream = App.GetRequiredService<SysCacheService>().Get<MemoryStream>(CacheConst.KeyExcelTemp + userId);
  71. if (resultStream == null) throw Oops.Oh("错误标记文件已过期。");
  72. return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream")
  73. {
  74. FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记_" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx"
  75. });
  76. }
  77. /// <summary>
  78. /// 加密字符串 🔖
  79. /// </summary>
  80. /// <returns></returns>
  81. [SuppressMonitor]
  82. [DisplayName("加密字符串")]
  83. public dynamic EncryptPlainText([Required] string plainText)
  84. {
  85. return CryptogramUtil.Encrypt(plainText);
  86. }
  87. }