CommonService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var localhost = $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host.Value}";
  68. return localhost;
  69. }
  70. /// <summary>
  71. /// 获取文件URL
  72. /// </summary>
  73. /// <param name="sysFile"></param>
  74. /// <returns></returns>
  75. public string GetFileUrl(SysFile sysFile)
  76. {
  77. return $"{GetHost()}/{sysFile.FilePath}/{sysFile.Id + sysFile.Suffix}";
  78. }
  79. }