ApiHelper.cs 1.7 KB

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