1
0

ApiHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Admin.Core.Common.Attributes;
  2. using Admin.Core.Common.Helpers;
  3. using Admin.Core.Repository;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. namespace Admin.Core.Logs
  10. {
  11. /// <summary>
  12. /// Api帮助类
  13. /// </summary>
  14. [SingleInstance]
  15. public class ApiHelper
  16. {
  17. private List<ApiHelperDto> _apis;
  18. private static readonly object _lockObject = new object();
  19. public List<ApiHelperDto> GetApis()
  20. {
  21. if (_apis != null && _apis.Any())
  22. return _apis;
  23. lock (_lockObject)
  24. {
  25. if (_apis != null && _apis.Any())
  26. return _apis;
  27. _apis = new List<ApiHelperDto>();
  28. var filePath = Path.Combine(AppContext.BaseDirectory, "Db/Data/data.json").ToPath();
  29. var jsonData = FileHelper.ReadFile(filePath);
  30. var apis = JsonConvert.DeserializeObject<Data>(jsonData).Apis;
  31. foreach (var api in apis)
  32. {
  33. var parentLabel = apis.FirstOrDefault(a => a.Id == api.ParentId)?.Label;
  34. _apis.Add(new ApiHelperDto
  35. {
  36. Label = parentLabel.NotNull() ? $"{parentLabel} / {api.Label}" : api.Label,
  37. Path = api.Path?.ToLower().Trim('/')
  38. });
  39. }
  40. return _apis;
  41. }
  42. }
  43. }
  44. public class ApiHelperDto
  45. {
  46. /// <summary>
  47. /// 接口名称
  48. /// </summary>
  49. public string Label { get; set; }
  50. /// <summary>
  51. /// 接口地址
  52. /// </summary>
  53. public string Path { get; set; }
  54. }
  55. }