平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpaService.cs 4.6 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 
  2. //
  3. namespace SafeCampus.System;
  4. /// <summary>
  5. /// <inheritdoc cref="ISpaService"/>
  6. /// </summary>
  7. public class SpaService : DbRepository<SysResource>, ISpaService
  8. {
  9. private readonly IResourceService _resourceService;
  10. public SpaService(IResourceService resourceService)
  11. {
  12. _resourceService = resourceService;
  13. }
  14. /// <inheritdoc/>
  15. public async Task<SqlSugarPagedList<SysResource>> Page(SpaPageInput input)
  16. {
  17. var query = Context.Queryable<SysResource>().Where(it => it.Category == CateGoryConst.RESOURCE_SPA)//单页
  18. .WhereIF(!string.IsNullOrEmpty(input.MenuType), it => it.MenuType == input.MenuType)//根据菜单类型查询
  19. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey) || it.Path.Contains(input.SearchKey))//根据关键字查询
  20. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode);//排序
  21. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  22. return pageInfo;
  23. }
  24. /// <inheritdoc />
  25. public async Task Add(SpaAddInput input)
  26. {
  27. await CheckInput(input);//检查参数
  28. input.Code = RandomHelper.CreateRandomString(10);//code取随机值
  29. var sysResource = input.Adapt<SysResource>();//实体转换
  30. if (await InsertAsync(sysResource))//插入数据
  31. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_SPA);//刷新缓存
  32. }
  33. /// <inheritdoc />
  34. public async Task Edit(SpaEditInput input)
  35. {
  36. await CheckInput(input);//检查参数
  37. var sysResource = input.Adapt<SysResource>();//实体转换
  38. if (await UpdateAsync(sysResource))//更新数据
  39. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_SPA);//刷新缓存
  40. }
  41. /// <inheritdoc />
  42. public async Task Delete(BaseIdListInput input)
  43. {
  44. //获取所有ID
  45. var ids = input.Ids;
  46. if (ids.Count > 0)
  47. {
  48. //获取所有
  49. var resourceList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_SPA);
  50. //找到要删除的
  51. var sysresources = resourceList.Where(it => ids.Contains(it.Id)).ToList();
  52. //查找内置单页面
  53. var system = sysresources.Where(it => it.Code == SysResourceConst.SYSTEM).FirstOrDefault();
  54. if (system != null)
  55. throw Oops.Bah($"不可删除系统内置单页面:{system.Title}");
  56. //删除菜单
  57. await DeleteAsync(sysresources);
  58. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_SPA);//刷新缓存
  59. }
  60. }
  61. /// <inheritdoc />
  62. public async Task<SysResource> Detail(BaseIdInput input)
  63. {
  64. var sysResources = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_SPA);
  65. var resource = sysResources.Where(it => it.Id == input.Id).FirstOrDefault();
  66. return resource;
  67. }
  68. #region 方法
  69. /// <summary>
  70. /// 检查输入参数
  71. /// </summary>
  72. /// <param name="sysResource"></param>
  73. private async Task CheckInput(SysResource sysResource)
  74. {
  75. //判断菜单类型
  76. if (sysResource.MenuType == SysResourceConst.MENU)//如果是菜单
  77. {
  78. if (string.IsNullOrEmpty(sysResource.Name))
  79. {
  80. throw Oops.Bah("单页名称不能为空");
  81. }
  82. if (string.IsNullOrEmpty(sysResource.Component))
  83. {
  84. throw Oops.Bah("组件地址不能为空");
  85. }
  86. }
  87. else if (sysResource.MenuType == SysResourceConst.IFRAME || sysResource.MenuType == SysResourceConst.LINK)//如果是内链或者外链
  88. {
  89. // sysResource.Name = RandomHelper.CreateNum(10);//设置name为随机数
  90. sysResource.Name = null;//设置name为标题
  91. sysResource.Component = null;
  92. }
  93. else
  94. {
  95. throw Oops.Bah($"单页类型错误:{sysResource.MenuType}");//都不是
  96. }
  97. if (sysResource.IsHome)
  98. {
  99. var spas = await _resourceService.GetListByCategory(SysResourceConst.SPA);//获取所有单页
  100. if (spas.Any(it => it.IsHome && it.Id != sysResource.Id))//如果有多个主页
  101. {
  102. throw Oops.Bah("已存在首页,请取消其他主页后再试");
  103. }
  104. sysResource.IsHide = false;//如果是主页,则不隐藏
  105. sysResource.IsAffix = true;//如果是主页,则固定
  106. }
  107. //设置为单页
  108. sysResource.Category = CateGoryConst.RESOURCE_SPA;
  109. }
  110. #endregion 方法
  111. }