0
0

ApiHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. namespace Admin.Core.Logs
  8. {
  9. /// <summary>
  10. /// Api帮助类
  11. /// </summary>
  12. public class ApiHelper
  13. {
  14. private List<ApiHelperDto> _apis;
  15. private static readonly object _lockObject = new object();
  16. public List<ApiHelperDto> GetApis()
  17. {
  18. if (_apis != null && _apis.Any())
  19. return _apis;
  20. lock (_lockObject)
  21. {
  22. if (_apis != null && _apis.Any())
  23. return _apis;
  24. _apis = new List<ApiHelperDto>();
  25. var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"Db\Data\data.json");
  26. var jsonData = FileHelper.ReadFile(filePath);
  27. var apis = JsonConvert.DeserializeObject<Data>(jsonData).Apis;
  28. foreach (var api in apis)
  29. {
  30. var parentLabel = apis.FirstOrDefault(a => a.Id == api.ParentId)?.Label;
  31. _apis.Add(new ApiHelperDto
  32. {
  33. Label = parentLabel.NotNull() ? $"{parentLabel} / {api.Label}" : api.Label,
  34. Path = api.Path?.ToLower().Trim('/')
  35. });
  36. }
  37. return _apis;
  38. }
  39. }
  40. }
  41. public class ApiHelperDto
  42. {
  43. public string Label { get; set; }
  44. public string Path { get; set; }
  45. }
  46. }