CommonService.cs 2.5 KB

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