SysCommonService.cs 4.0 KB

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