CommonService.cs 3.0 KB

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