CommonService.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. namespace Admin.NET.Core.Service;
  2. public class CommonService : ICommonService, IScoped
  3. {
  4. private readonly IHttpContextAccessor _httpContextAccessor;
  5. private readonly CodeGenOptions _codeGenOptions;
  6. public CommonService(IHttpContextAccessor httpContextAccessor,
  7. IOptions<CodeGenOptions> codeGenOptions)
  8. {
  9. _httpContextAccessor = httpContextAccessor;
  10. _codeGenOptions = codeGenOptions.Value;
  11. }
  12. /// <summary>
  13. /// 获取库表信息
  14. /// </summary>
  15. /// <returns></returns>
  16. public async Task<IEnumerable<EntityInfo>> GetEntityInfos()
  17. {
  18. var entityInfos = new List<EntityInfo>();
  19. var type = typeof(SugarTable);
  20. var types = new List<Type>();
  21. if (_codeGenOptions.EntityAssemblyNames != null)
  22. {
  23. foreach (var assemblyName in _codeGenOptions.EntityAssemblyNames)
  24. {
  25. Assembly asm = Assembly.Load(assemblyName);
  26. types.AddRange(asm.GetExportedTypes().ToList());
  27. }
  28. }
  29. Func<Attribute[], bool> IsMyAttribute = o =>
  30. {
  31. foreach (Attribute a in o)
  32. {
  33. if (a.GetType() == type)
  34. return true;
  35. }
  36. return false;
  37. };
  38. Type[] cosType = types.Where(o =>
  39. {
  40. return IsMyAttribute(Attribute.GetCustomAttributes(o, true));
  41. }
  42. ).ToArray();
  43. foreach (var c in cosType)
  44. {
  45. var sugarAttribute = c.GetCustomAttributes(type, true)?.FirstOrDefault();
  46. var des = c.GetCustomAttributes(typeof(DescriptionAttribute), true);
  47. var description = "";
  48. if (des.Length > 0)
  49. {
  50. description = ((DescriptionAttribute)des[0]).Description;
  51. }
  52. entityInfos.Add(new EntityInfo()
  53. {
  54. EntityName = c.Name,
  55. DbTableName = sugarAttribute == null ? c.Name : ((SugarTable)sugarAttribute).TableName,
  56. TableDescription = description
  57. });
  58. }
  59. return await Task.FromResult(entityInfos);
  60. }
  61. /// <summary>
  62. /// 获取Host
  63. /// </summary>
  64. /// <returns></returns>
  65. public string GetHost()
  66. {
  67. return $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host.Value}";
  68. }
  69. /// <summary>
  70. /// 获取文件URL
  71. /// </summary>
  72. /// <param name="sysFile"></param>
  73. /// <returns></returns>
  74. public string GetFileUrl(SysFile sysFile)
  75. {
  76. return $"{GetHost()}/{sysFile.FilePath}/{sysFile.Id + sysFile.Suffix}";
  77. }
  78. }