平安校园
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

131 Zeilen
4.6 KiB

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