添加项目文件。
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 添加按钮参数
|
||||
/// </summary>
|
||||
public class MobileButtonAddInput : MobileResource
|
||||
{
|
||||
/// <summary>
|
||||
/// 父ID
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "ParentId不能为空")]
|
||||
public override long? ParentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "Title不能为空")]
|
||||
public override string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编码
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "Code不能为空")]
|
||||
public override string Code { get; set; }
|
||||
}
|
||||
|
||||
public class MobileButtonEditInput : MobileButtonAddInput
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
[IdNotNull(ErrorMessage = "Id不能为空")]
|
||||
public override long Id { get; set; }
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 权限按钮服务
|
||||
/// </summary>
|
||||
public interface IMobileButtonService : ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加按钮
|
||||
/// </summary>
|
||||
/// <param name="input">添加参数</param>
|
||||
/// <returns></returns>
|
||||
Task Add(MobileButtonAddInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<long>> AddBatch(MobileButtonAddInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 删除按钮
|
||||
/// </summary>
|
||||
/// <param name="input">删除参数</param>
|
||||
/// <returns></returns>
|
||||
Task Delete(BaseIdListInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 编辑按钮
|
||||
/// </summary>
|
||||
/// <param name="input">编辑参数</param>
|
||||
/// <returns></returns>
|
||||
Task Edit(MobileButtonEditInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 按钮分页查询
|
||||
/// </summary>
|
||||
/// <param name="input">查询条件</param>
|
||||
/// <returns>按钮分页列表</returns>
|
||||
Task<SqlSugarPagedList<MobileResource>> Page(ButtonPageInput input);
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="IMobileButtonService"/>
|
||||
/// </summary>
|
||||
public class MobileMobileButtonService : DbRepository<MobileResource>, IMobileButtonService
|
||||
{
|
||||
private readonly ILogger<MobileMobileButtonService> _logger;
|
||||
private readonly IMobileResourceService _resourceService;
|
||||
private readonly IRelationService _relationService;
|
||||
|
||||
public MobileMobileButtonService(ILogger<MobileMobileButtonService> logger, IMobileResourceService resourceService,
|
||||
IRelationService relationService)
|
||||
{
|
||||
_logger = logger;
|
||||
_resourceService = resourceService;
|
||||
_relationService = relationService;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<SqlSugarPagedList<MobileResource>> Page(ButtonPageInput input)
|
||||
{
|
||||
var query = Context.Queryable<MobileResource>()
|
||||
.Where(it => it.ParentId == input.ParentId && it.Category == CateGoryConst.RESOURCE_BUTTON)
|
||||
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey) || it.Path.Contains(input.SearchKey))//根据关键字查询
|
||||
.OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")
|
||||
.OrderBy(it => it.SortCode);//排序
|
||||
var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Add(MobileButtonAddInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var MobileResource = input.Adapt<MobileResource>();//实体转换
|
||||
if (await InsertAsync(MobileResource))//插入数据
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<long>> AddBatch(MobileButtonAddInput input)
|
||||
{
|
||||
var MobileResources = new List<MobileResource>();//按钮列表
|
||||
var codeList = new List<string> { "Add", "Edit", "Delete", "BatchDelete", "Import", "Export", "BatchEdit" };//code后缀
|
||||
var titleList = new List<string> { "新增", "编辑", "删除", "批量删除", "导入", "导出", "批量编辑" };//title前缀
|
||||
var idList = new List<long>();//Id列表
|
||||
for (var i = 0; i < codeList.Count; i++)
|
||||
{
|
||||
var id = CommonUtils.GetSingleId();
|
||||
MobileResources.Add(new MobileResource
|
||||
{
|
||||
Id = id,
|
||||
Title = titleList[i] + input.Title,//标题等于前缀输入的值
|
||||
Code = input.Code + codeList[i],//code等于输入的值加后缀
|
||||
ParentId = input.ParentId,
|
||||
SortCode = i + 1
|
||||
});
|
||||
idList.Add(id);
|
||||
}
|
||||
//遍历列表
|
||||
foreach (var MobileResource in MobileResources)
|
||||
{
|
||||
await CheckInput(MobileResource);//检查按钮参数
|
||||
}
|
||||
//添加到数据库
|
||||
if (await InsertRangeAsync(MobileResources))//插入数据
|
||||
{
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
|
||||
return MobileResources.Select(it => it.Id).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<long>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Edit(MobileButtonEditInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var MobileResource = input.Adapt<MobileResource>();//实体转换
|
||||
//事务
|
||||
var result = await Tenant.UseTranAsync(async () =>
|
||||
{
|
||||
await UpdateAsync(MobileResource);//更新按钮
|
||||
});
|
||||
if (result.IsSuccess)//如果成功了
|
||||
{
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
|
||||
}
|
||||
else
|
||||
{
|
||||
//写日志
|
||||
_logger.LogError(result.ErrorMessage, result.ErrorException);
|
||||
throw Oops.Oh(ErrorCodeEnum.A0002);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Delete(BaseIdListInput input)
|
||||
{
|
||||
//获取所有ID
|
||||
var ids = input.Ids;
|
||||
//获取所有按钮集合
|
||||
var buttonList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_BUTTON);
|
||||
|
||||
#region 处理关系表角色资源信息
|
||||
|
||||
//获取所有菜单集合
|
||||
var menuList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
|
||||
//获取按钮的父菜单id集合
|
||||
var parentIds = buttonList.Where(it => ids.Contains(it.Id)).Select(it => it.ParentId.Value.ToString()).ToList();
|
||||
//获取关系表分类为SYS_ROLE_HAS_RESOURCE数据
|
||||
var roleResources = await _relationService.GetRelationByCategory(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);
|
||||
//获取相关关系表数据
|
||||
var relationList = roleResources
|
||||
.Where(it => parentIds.Contains(it.TargetId))//目标ID是父ID中
|
||||
.Where(it => it.ExtJson != null).ToList();//扩展信息不为空
|
||||
//遍历关系表
|
||||
relationList.ForEach(it =>
|
||||
{
|
||||
var relationRoleResuorce = it.ExtJson.ToJsonEntity<RelationRoleResource>();//拓展信息转实体
|
||||
var buttonInfo = relationRoleResuorce.ButtonInfo;//获取按钮信息
|
||||
if (buttonInfo.Count > 0)
|
||||
{
|
||||
var diffArr = buttonInfo.Where(it => !buttonInfo.Contains(it)).ToList();//找出不同的元素(即交集的补集)
|
||||
relationRoleResuorce.ButtonInfo = diffArr;//重新赋值按钮信息
|
||||
it.ExtJson = relationRoleResuorce.ToJson();//重新赋值拓展信息
|
||||
}
|
||||
});
|
||||
|
||||
#endregion 处理关系表角色资源信息
|
||||
|
||||
//事务
|
||||
var result = await Tenant.UseTranAsync(async () =>
|
||||
{
|
||||
await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除按钮
|
||||
if (relationList.Count > 0)
|
||||
{
|
||||
await Context.Updateable(relationList).UpdateColumns(it => it.ExtJson).ExecuteCommandAsync();//修改拓展信息
|
||||
}
|
||||
});
|
||||
if (result.IsSuccess)//如果成功了
|
||||
{
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
|
||||
await _relationService.RefreshCache(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);//关系表刷新角色资源缓存
|
||||
}
|
||||
else
|
||||
{
|
||||
//写日志
|
||||
_logger.LogError(result.ErrorMessage, result.ErrorException);
|
||||
throw Oops.Oh(ErrorCodeEnum.A0002);
|
||||
}
|
||||
}
|
||||
|
||||
#region 方法
|
||||
|
||||
/// <summary>
|
||||
/// 检查输入参数
|
||||
/// </summary>
|
||||
/// <param name="MobileResource"></param>
|
||||
private async Task CheckInput(MobileResource MobileResource)
|
||||
{
|
||||
//获取所有按钮和菜单
|
||||
var buttonList = await _resourceService.GetListAsync(new List<string> { CateGoryConst.RESOURCE_BUTTON, CateGoryConst.RESOURCE_MENU });
|
||||
//判断code是否重复
|
||||
if (buttonList.Any(it => it.Code == MobileResource.Code && it.Id != MobileResource.Id))
|
||||
throw Oops.Bah($"存在重复的按钮编码:{MobileResource.Code}");
|
||||
//判断菜单是否存在
|
||||
if (!buttonList.Any(it => it.Id == MobileResource.ParentId))
|
||||
throw Oops.Bah($"不存在的父级菜单:{MobileResource.ParentId}");
|
||||
MobileResource.Category = CateGoryConst.RESOURCE_BUTTON;//设置分类为按钮
|
||||
}
|
||||
|
||||
#endregion 方法
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 添加菜单参数
|
||||
/// </summary>
|
||||
public class MobileMenuAddInput : MobileResource
|
||||
{
|
||||
/// <summary>
|
||||
/// 父ID
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "ParentId不能为空")]
|
||||
public override long? ParentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "Title不能为空")]
|
||||
public override string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 菜单类型
|
||||
/// </summary>
|
||||
public override string MenuType { get; set; } = SysResourceConst.MENU;
|
||||
|
||||
/// <summary>
|
||||
/// 模块
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "Module不能为空")]
|
||||
public override long? Module { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 图标
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "Icon不能为空")]
|
||||
public override string Icon { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑菜单输入参数
|
||||
/// </summary>
|
||||
public class MobileMenuEditInput : MobileMenuAddInput
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
[IdNotNull(ErrorMessage = "Id不能为空")]
|
||||
public override long Id { get; set; }
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 菜单服务
|
||||
/// </summary>
|
||||
public interface IMobileMenuService : ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加菜单
|
||||
/// </summary>
|
||||
/// <param name="input">添加参数</param>
|
||||
/// <returns></returns>
|
||||
Task Add(MobileMenuAddInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 详情
|
||||
/// </summary>
|
||||
/// <param name="input">id</param>
|
||||
/// <returns>详细信息</returns>
|
||||
Task<MobileResource> Detail(BaseIdInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 构建菜单树形结构
|
||||
/// </summary>
|
||||
/// <param name="resourceList">菜单列表</param>
|
||||
/// <param name="parentId">父ID</param>
|
||||
/// <returns>菜单形结构</returns>
|
||||
List<MobileResource> ConstructMenuTrees(List<MobileResource> resourceList, long parentId = 0);
|
||||
|
||||
/// <summary>
|
||||
/// 获取菜单树
|
||||
/// </summary>
|
||||
/// <param name="input">菜单树查询参数</param>
|
||||
/// <returns>菜单树列表</returns>
|
||||
Task<List<MobileResource>> Tree(MenuTreeInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 编辑菜单
|
||||
/// </summary>
|
||||
/// <param name="input">菜单编辑参数</param>
|
||||
/// <returns></returns>
|
||||
Task Edit(MobileMenuEditInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 删除菜单
|
||||
/// </summary>
|
||||
/// <param name="input">删除菜单参数</param>
|
||||
/// <returns></returns>
|
||||
Task Delete(BaseIdListInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 改变菜单模块
|
||||
/// </summary>
|
||||
/// <param name="input">改变菜单模块参数</param>
|
||||
/// <returns></returns>
|
||||
Task ChangeModule(MenuChangeModuleInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 角色授权移动端资源
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
Task GrantRoleMobileResource(GrantResourceInput input);
|
||||
}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
using MoYu.EventBus;
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="IMobileMenuService"/>
|
||||
/// </summary>
|
||||
public class MobileMenuService : DbRepository<MobileResource>, IMobileMenuService
|
||||
{
|
||||
private readonly ILogger<MobileMenuService> _logger;
|
||||
private readonly IMobileResourceService _resourceService;
|
||||
private readonly IRelationService _relationService;
|
||||
private readonly IEventPublisher _eventPublisher;
|
||||
private readonly ISysRoleService _roleService;
|
||||
|
||||
public MobileMenuService(ILogger<MobileMenuService> logger, IMobileResourceService resourceService, IRelationService relationService,
|
||||
IEventPublisher eventPublisher, ISysRoleService roleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_resourceService = resourceService;
|
||||
_relationService = relationService;
|
||||
_eventPublisher = eventPublisher;
|
||||
_roleService = roleService;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public List<MobileResource> ConstructMenuTrees(List<MobileResource> resourceList, long parentId = 0)
|
||||
{
|
||||
//找下级资源ID列表
|
||||
var resources = resourceList.Where(it => it.ParentId == parentId).OrderBy(it => it.SortCode).ToList();
|
||||
if (resources.Count > 0)//如果数量大于0
|
||||
{
|
||||
var data = new List<MobileResource>();
|
||||
foreach (var item in resources)//遍历资源
|
||||
{
|
||||
item.Children = ConstructMenuTrees(resourceList, item.Id);//添加子节点
|
||||
data.Add(item);//添加到列表
|
||||
}
|
||||
return data;//返回结果
|
||||
}
|
||||
return new List<MobileResource>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<MobileResource>> Tree(MenuTreeInput input)
|
||||
{
|
||||
//获取所有菜单
|
||||
var mobileResources = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
|
||||
mobileResources = mobileResources.WhereIF(input.Module != null, it => it.Module.Value == input.Module.Value)//根据模块查找
|
||||
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title == input.SearchKey)//根据关键字查找
|
||||
.ToList();
|
||||
//构建菜单树
|
||||
var tree = ConstructMenuTrees(mobileResources);
|
||||
return tree;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Add(MobileMenuAddInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var mobileResource = input.Adapt<MobileResource>();//实体转换
|
||||
|
||||
if (await InsertAsync(mobileResource))//插入数据
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MENU);//刷新菜单缓存
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Edit(MobileMenuEditInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var mobileResource = input.Adapt<MobileResource>();//实体转换
|
||||
if (await UpdateAsync(mobileResource))//更新数据
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MENU);//刷新菜单缓存
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Delete(BaseIdListInput input)
|
||||
{
|
||||
//获取所有ID
|
||||
var ids = input.Ids;
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
//获取所有菜单和按钮
|
||||
var resourceList = await _resourceService.GetListAsync(new List<string> { CateGoryConst.RESOURCE_MENU, CateGoryConst.RESOURCE_BUTTON });
|
||||
//找到要删除的菜单
|
||||
var mobileResources = resourceList.Where(it => ids.Contains(it.Id)).ToList();
|
||||
//查找内置菜单
|
||||
var system = mobileResources.Where(it => it.Code == SysResourceConst.SYSTEM).FirstOrDefault();
|
||||
if (system != null)
|
||||
throw Oops.Bah($"不可删除系统菜单:{system.Title}");
|
||||
//需要删除的资源ID列表
|
||||
var resourceIds = new List<long>();
|
||||
//遍历菜单列表
|
||||
mobileResources.ForEach(it =>
|
||||
{
|
||||
//获取菜单所有子节点
|
||||
var child = _resourceService.GetChildListById(resourceList, it.Id, false);
|
||||
//将子节点ID添加到删除资源ID列表
|
||||
resourceIds.AddRange(child.Select(it => it.Id).ToList());
|
||||
resourceIds.Add(it.Id);//添加到删除资源ID列表
|
||||
});
|
||||
ids.AddRange(resourceIds);//添加到删除ID列表
|
||||
//事务
|
||||
var result = await Tenant.UseTranAsync(async () =>
|
||||
{
|
||||
await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除菜单和按钮
|
||||
await Context.Deleteable<SysRelation>()//关系表删除对应SYS_ROLE_HAS_RESOURCE
|
||||
.Where(it => it.Category == MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE
|
||||
&& resourceIds.Contains(SqlFunc.ToInt64(it.TargetId))).ExecuteCommandAsync();
|
||||
});
|
||||
if (result.IsSuccess)//如果成功了
|
||||
{
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MENU);//资源表菜单刷新缓存
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
|
||||
await _relationService.RefreshCache(MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE);//关系表刷新缓存
|
||||
}
|
||||
else
|
||||
{
|
||||
//写日志
|
||||
_logger.LogError(result.ErrorMessage, result.ErrorException);
|
||||
throw Oops.Oh(ErrorCodeEnum.A0002);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MobileResource> Detail(BaseIdInput input)
|
||||
{
|
||||
var mobileResources = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
|
||||
var resource = mobileResources.Where(it => it.Id == input.Id).FirstOrDefault();
|
||||
return resource;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task ChangeModule(MenuChangeModuleInput input)
|
||||
{
|
||||
var mobileResource = await Detail(new BaseIdInput { Id = input.Id });
|
||||
if (mobileResource != null)
|
||||
{
|
||||
if (mobileResource.Module == input.Module)//如果模块ID没变直接返回
|
||||
return;
|
||||
if (mobileResource.ParentId != 0)
|
||||
throw Oops.Bah($"非顶级菜单不可修改所属模块");
|
||||
//获取所有菜单和模块
|
||||
var resourceList = await _resourceService.GetListAsync(new List<string> { CateGoryConst.RESOURCE_MENU, CateGoryConst.RESOURCE_MODULE });
|
||||
if (!resourceList.Any(it => it.Category == CateGoryConst.RESOURCE_MODULE && it.Id == input.Module.Value))
|
||||
throw Oops.Bah($"不存在的模块");
|
||||
//获取所有菜单
|
||||
var menuList = resourceList.Where(it => it.Category == CateGoryConst.RESOURCE_MENU).ToList();
|
||||
//获取需要改变模块菜单的所有子菜单
|
||||
var childList = _resourceService.GetChildListById(menuList, input.Id);
|
||||
childList.ForEach(it => it.Module = input.Module);
|
||||
//更新数据
|
||||
await UpdateRangeAsync(childList);
|
||||
//刷新菜单缓存
|
||||
await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MENU);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task GrantRoleMobileResource(GrantResourceInput input)
|
||||
{
|
||||
var menuIds = input.GrantInfoList.Select(it => it.MenuId).ToList();//菜单ID
|
||||
var extJsons = input.GrantInfoList.Select(it => it.ToJson()).ToList();//拓展信息
|
||||
var relationRoles = new List<SysRelation>();//要添加的角色资源和授权关系表
|
||||
var sysRole = (await _roleService.GetListAsync()).Where(it => it.Id == input.Id).FirstOrDefault();//获取角色
|
||||
if (sysRole != null)
|
||||
{
|
||||
//遍历菜单列表
|
||||
for (var i = 0; i < menuIds.Count; i++)
|
||||
{
|
||||
//将角色资源添加到列表
|
||||
relationRoles.Add(new SysRelation
|
||||
{
|
||||
ObjectId = sysRole.Id,
|
||||
TargetId = menuIds[i].ToString(),
|
||||
Category = MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE,
|
||||
ExtJson = extJsons?[i]
|
||||
});
|
||||
}
|
||||
//事务
|
||||
var result = await Tenant.UseTranAsync(async () =>
|
||||
{
|
||||
var relatioRep = ChangeRepository<DbRepository<SysRelation>>();//切换仓储
|
||||
//如果不是代码生成,就删除老的
|
||||
if (!input.IsCodeGen)
|
||||
await relatioRep.DeleteAsync(it =>
|
||||
it.ObjectId == sysRole.Id && (it.Category == MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE
|
||||
|| it.Category == MobileConst.RELATION_SYS_USER_HAS_MOBILE_RESOURCE));
|
||||
await relatioRep.InsertRangeAsync(relationRoles);//添加新的
|
||||
});
|
||||
if (result.IsSuccess)//如果成功了
|
||||
{
|
||||
await _relationService.RefreshCache(MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE);//刷新关系缓存
|
||||
await _relationService.RefreshCache(MobileConst.RELATION_SYS_USER_HAS_MOBILE_RESOURCE);//刷新关系缓存
|
||||
await _eventPublisher.PublishAsync(EventSubscriberConst.CLEAR_USER_CACHE, new List<long> { input.Id });//发送事件清除角色下用户缓存
|
||||
}
|
||||
else
|
||||
{
|
||||
//写日志
|
||||
_logger.LogError(result.ErrorMessage, result.ErrorException);
|
||||
throw Oops.Oh(ErrorCodeEnum.A0003);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region 方法
|
||||
|
||||
/// <summary>
|
||||
/// 检查输入参数
|
||||
/// </summary>
|
||||
/// <param name="mobileResource"></param>
|
||||
private async Task CheckInput(MobileResource mobileResource)
|
||||
{
|
||||
mobileResource.Category = mobileResource.MenuType;//设置为菜单
|
||||
//获取所有菜单列表
|
||||
var menList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
|
||||
//判断是否有同级且同名的菜单
|
||||
if (menList.Any(it => it.ParentId == mobileResource.ParentId && it.Title == mobileResource.Title && it.Id != mobileResource.Id))
|
||||
throw Oops.Bah($"存在重复的菜单名称:{mobileResource.Title}");
|
||||
if (mobileResource.ParentId != 0)
|
||||
{
|
||||
//获取父级,判断父级ID正不正确
|
||||
var parent = menList.Where(it => it.Id == mobileResource.ParentId).FirstOrDefault();
|
||||
if (parent != null)
|
||||
{
|
||||
if (parent.Module != mobileResource.Module)//如果父级的模块和当前模块不一样
|
||||
throw Oops.Bah($"模块与上级菜单不一致");
|
||||
if (parent.Id == mobileResource.Id)
|
||||
throw Oops.Bah($"上级菜单不能选择自己");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Oops.Bah($"上级菜单不存在:{mobileResource.ParentId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 方法
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 添加模块输入
|
||||
/// </summary>
|
||||
public class MobileModuleAddInput : MobileResource
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑模块输入
|
||||
/// </summary>
|
||||
public class MobileModuleEditInput : MobileModuleAddInput
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
[IdNotNull(ErrorMessage = "Id不能为空")]
|
||||
public override long Id { get; set; }
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 模块管理服务
|
||||
/// </summary>
|
||||
public interface IMobileModuleService : ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加模块
|
||||
/// </summary>
|
||||
/// <param name="input">添加参数</param>
|
||||
/// <returns></returns>
|
||||
Task Add(MobileModuleAddInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 删除模块
|
||||
/// </summary>
|
||||
/// <param name="input">删除参数</param>
|
||||
/// <returns></returns>
|
||||
Task Delete(BaseIdListInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 编辑模块
|
||||
/// </summary>
|
||||
/// <param name="input">编辑参数</param>
|
||||
/// <returns></returns>
|
||||
Task Edit(MobileModuleEditInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <param name="input">分页参数</param>
|
||||
/// <returns></returns>
|
||||
Task<SqlSugarPagedList<MobileResource>> Page(ModulePageInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 详情
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
Task<MobileResource> Detail(BaseIdInput input);
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc cref="IMobileModuleService"/>
|
||||
/// </summary>
|
||||
public class MobileModuleService : DbRepository<MobileResource>, IMobileModuleService
|
||||
{
|
||||
private readonly ILogger<MobileModuleService> _logger;
|
||||
private readonly ISimpleCacheService _simpleCacheService;
|
||||
private readonly IMobileResourceService _mobileResourceService;
|
||||
private readonly IRelationService _relationService;
|
||||
|
||||
public MobileModuleService(ILogger<MobileModuleService> logger, ISimpleCacheService simpleCacheService,
|
||||
IMobileResourceService mobileResourceService,
|
||||
IRelationService relationService)
|
||||
{
|
||||
_logger = logger;
|
||||
_simpleCacheService = simpleCacheService;
|
||||
_mobileResourceService = mobileResourceService;
|
||||
_relationService = relationService;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<SqlSugarPagedList<MobileResource>> Page(ModulePageInput input)
|
||||
{
|
||||
var query = Context.Queryable<MobileResource>()
|
||||
.Where(it => it.Category == CateGoryConst.RESOURCE_MODULE)//模块
|
||||
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey))//根据关键字查询
|
||||
.OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")
|
||||
.OrderBy(it => it.SortCode);//排序
|
||||
var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Add(MobileModuleAddInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var sysResource = input.Adapt<MobileResource>();//实体转换
|
||||
if (await InsertAsync(sysResource))//插入数据
|
||||
await _mobileResourceService.RefreshCache(CateGoryConst.RESOURCE_MODULE);//刷新缓存
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Edit(MobileModuleEditInput input)
|
||||
{
|
||||
await CheckInput(input);//检查参数
|
||||
var sysResource = input.Adapt<MobileResource>();//实体转换
|
||||
if (await UpdateAsync(sysResource))//更新数据
|
||||
await _mobileResourceService.RefreshCache(CateGoryConst.RESOURCE_MODULE);//刷新缓存
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Delete(BaseIdListInput input)
|
||||
{
|
||||
//获取所有ID
|
||||
var ids = input.Ids;
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
//获取所有
|
||||
var resourceList = await _mobileResourceService.GetListAsync();
|
||||
//找到要删除的模块
|
||||
var sysresources = resourceList.Where(it => ids.Contains(it.Id)).ToList();
|
||||
//查找内置模块
|
||||
var system = sysresources.Where(it => it.Code == SysResourceConst.SYSTEM).FirstOrDefault();
|
||||
if (system != null)
|
||||
throw Oops.Bah($"不可删除系统内置模块:{system.Title}");
|
||||
//获取模块下的所有菜单Id列表
|
||||
var menuIds = resourceList.Where(it => ids.Contains(it.Module.ToLong()) && it.ParentId.ToLong() == SimpleAdminConst.ZERO)
|
||||
.Select(it => it.Id).ToList();
|
||||
//需要删除的资源ID列表
|
||||
var resourceIds = new List<long>();
|
||||
//遍历列表
|
||||
menuIds.ForEach(it =>
|
||||
{
|
||||
//获取菜单所有子节点
|
||||
var child = _mobileResourceService.GetChildListById(resourceList, it, false);
|
||||
//将子节点ID添加到删除ID列表
|
||||
resourceIds.AddRange(child.Select(it => it.Id).ToList());
|
||||
resourceIds.Add(it);//添加到删除ID列表
|
||||
});
|
||||
ids.AddRange(resourceIds);
|
||||
//事务
|
||||
var result = await Tenant.UseTranAsync(async () =>
|
||||
{
|
||||
await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除菜单和按钮
|
||||
await Context.Deleteable<SysRelation>()//关系表删除对应SYS_ROLE_HAS_MOBILE_RESOURCE
|
||||
.Where(it => it.Category == MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE
|
||||
&& resourceIds.Contains(SqlFunc.ToInt64(it.TargetId))).ExecuteCommandAsync();
|
||||
});
|
||||
if (result.IsSuccess)//如果成功了
|
||||
{
|
||||
await _mobileResourceService.RefreshCache();//资源表刷新缓存
|
||||
await _relationService.RefreshCache(MobileConst.RELATION_SYS_ROLE_HAS_MOBILE_RESOURCE);//关系表刷新缓存
|
||||
}
|
||||
else
|
||||
{
|
||||
//写日志
|
||||
_logger.LogError(result.ErrorMessage, result.ErrorException);
|
||||
throw Oops.Oh(ErrorCodeEnum.A0002);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MobileResource> Detail(BaseIdInput input)
|
||||
{
|
||||
var mobileResource = await GetFirstAsync(it => it.Id == input.Id);
|
||||
return mobileResource;
|
||||
}
|
||||
|
||||
#region 方法
|
||||
|
||||
/// <summary>
|
||||
/// 检查输入参数
|
||||
/// </summary>
|
||||
/// <param name="sysResource"></param>
|
||||
private async Task CheckInput(MobileResource sysResource)
|
||||
{
|
||||
var sysResourceList = await _mobileResourceService.GetListByCategory(CateGoryConst.RESOURCE_MODULE);
|
||||
//判断是否从存在重复模块
|
||||
var hasSameName = sysResourceList.Any(it => it.Title == sysResource.Title && it.Id != sysResource.Id);
|
||||
if (hasSameName)
|
||||
{
|
||||
throw Oops.Bah($"存在重复的模块:{sysResource.Title}");
|
||||
}
|
||||
//设置为模块
|
||||
sysResource.Category = CateGoryConst.RESOURCE_MODULE;
|
||||
}
|
||||
|
||||
#endregion 方法
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <summary>
|
||||
/// 资源服务
|
||||
/// </summary>
|
||||
public interface IMobileResourceService : ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取所有的菜单和模块以及单页面列表,并按分类和排序码排序
|
||||
/// </summary>
|
||||
/// <returns>所有的菜单和模块以及单页面列表</returns>
|
||||
Task<List<MobileResource>> GetaModuleAndMenuAndSpaList();
|
||||
|
||||
/// <summary>
|
||||
/// 根据资源ID获取所有下级资源
|
||||
/// </summary>
|
||||
/// <param name="resId">资源ID</param>
|
||||
/// <param name="isContainOneself">是否包含自己</param>
|
||||
/// <returns>资源列表</returns>
|
||||
Task<List<MobileResource>> GetChildListById(long resId, bool isContainOneself = true);
|
||||
|
||||
/// <summary>
|
||||
/// 根据资源ID获取所有下级资源
|
||||
/// </summary>
|
||||
/// <param name="sysResources">资源列表</param>
|
||||
/// <param name="resId">资源ID</param>
|
||||
/// <param name="isContainOneself">是否包含自己</param>
|
||||
/// <returns>资源列表</returns>
|
||||
List<MobileResource> GetChildListById(List<MobileResource> sysResources, long resId, bool isContainOneself = true);
|
||||
|
||||
/// <summary>
|
||||
/// 获取ID获取Code列表
|
||||
/// </summary>
|
||||
/// <param name="ids">id列表</param>
|
||||
/// <param name="category">分类</param>
|
||||
/// <returns>Code列表</returns>
|
||||
Task<List<string>> GetCodeByIds(List<long> ids, string category);
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源列表
|
||||
/// </summary>
|
||||
/// <param name="categorys">资源分类列表</param>
|
||||
/// <returns></returns>
|
||||
Task<List<MobileResource>> GetListAsync(List<string>? categorys = null);
|
||||
|
||||
/// <summary>
|
||||
/// 根据分类获取资源列表
|
||||
/// </summary>
|
||||
/// <param name="category">分类名称</param>
|
||||
/// <returns>资源列表</returns>
|
||||
Task<List<MobileResource>> GetListByCategory(string category);
|
||||
|
||||
/// <summary>
|
||||
/// 根据菜单ID获取菜单
|
||||
/// </summary>
|
||||
/// <param name="menuIds">菜单id列表</param>
|
||||
/// <returns>菜单列表</returns>
|
||||
Task<List<MobileResource>> GetMenuByMenuIds(List<long> menuIds);
|
||||
|
||||
/// <summary>
|
||||
/// 获取权限授权树
|
||||
/// </summary>
|
||||
/// <param name="routes">路由列表</param>
|
||||
/// <returns></returns>
|
||||
List<PermissionTreeSelector> PermissionTreeSelector(List<string> routes);
|
||||
|
||||
/// <summary>
|
||||
/// 刷新缓存
|
||||
/// </summary>
|
||||
/// <param name="category">分类名称</param>
|
||||
/// <returns></returns>
|
||||
Task RefreshCache(string category = null);
|
||||
|
||||
/// <summary>
|
||||
/// 角色授权资源树
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<List<ResTreeSelector>> ResourceTreeSelector();
|
||||
}
|
||||
+410
@@ -0,0 +1,410 @@
|
||||
// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
|
||||
//
|
||||
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
|
||||
// 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
|
||||
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
|
||||
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
|
||||
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
|
||||
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
|
||||
|
||||
using StringHelper = SimpleTool.StringHelper;
|
||||
|
||||
namespace SimpleAdmin.Plugin.Mobile;
|
||||
|
||||
/// <inheritdoc cref="IMobileResourceService"/>
|
||||
public class MobileResourceService : DbRepository<MobileResource>, IMobileResourceService
|
||||
{
|
||||
private readonly ISimpleCacheService _simpleCacheService;
|
||||
|
||||
public MobileResourceService(ISimpleCacheService simpleCacheService)
|
||||
{
|
||||
_simpleCacheService = simpleCacheService;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<string>> GetCodeByIds(List<long> ids, string category)
|
||||
{
|
||||
//根据分类获取所有
|
||||
var mobileResources = await GetListByCategory(category);
|
||||
//条件查询
|
||||
var result = mobileResources.Where(it => ids.Contains(it.Id)).Select(it => it.Code).ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<MobileResource>> GetListAsync(List<string>? categoryList = null)
|
||||
{
|
||||
//定义结果
|
||||
var mobileResources = new List<MobileResource>();
|
||||
|
||||
//定义资源分类列表,如果是空的则获取全部资源
|
||||
categoryList = categoryList != null
|
||||
? categoryList
|
||||
: new List<string>
|
||||
{ CateGoryConst.RESOURCE_MENU, CateGoryConst.RESOURCE_BUTTON, CateGoryConst.RESOURCE_SPA, CateGoryConst.RESOURCE_MODULE };
|
||||
//遍历列表
|
||||
foreach (var category in categoryList)
|
||||
{
|
||||
//根据分类获取到资源列表
|
||||
var data = await GetListByCategory(category);
|
||||
//添加到结果集
|
||||
mobileResources.AddRange(data);
|
||||
}
|
||||
return mobileResources;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<List<MobileResource>> GetaModuleAndMenuAndSpaList()
|
||||
{
|
||||
//获取所有的菜单和模块以及单页面列表,
|
||||
var mobileResources = await GetListAsync(new List<string>
|
||||
{ CateGoryConst.RESOURCE_MODULE, CateGoryConst.RESOURCE_MENU, CateGoryConst.RESOURCE_SPA });
|
||||
if (mobileResources != null)
|
||||
{
|
||||
//并按分类和排序码排序
|
||||
mobileResources = mobileResources.OrderBy(it => it.Category).ThenBy(it => it.SortCode).ToList();
|
||||
}
|
||||
return mobileResources;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task RefreshCache(string category = null)
|
||||
{
|
||||
//如果分类是空的
|
||||
if (category == null)
|
||||
{
|
||||
//删除全部key
|
||||
_simpleCacheService.DelByPattern(MobileConst.CACHE_MOBILE_RESOURCE);
|
||||
await GetListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
//否则只删除一个Key
|
||||
_simpleCacheService.Remove(MobileConst.CACHE_MOBILE_RESOURCE + category);
|
||||
await GetListByCategory(category);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<MobileResource>> GetChildListById(long resId, bool isContainOneself = true)
|
||||
{
|
||||
//获取所有机构
|
||||
var mobileResources = await GetListAsync();
|
||||
//查找下级
|
||||
var childLsit = GetResourceChilden(mobileResources, resId);
|
||||
if (isContainOneself)//如果包含自己
|
||||
{
|
||||
//获取自己的机构信息
|
||||
var self = mobileResources.Where(it => it.Id == resId).FirstOrDefault();
|
||||
if (self != null) childLsit.Insert(0, self);//如果机构不为空就插到第一个
|
||||
}
|
||||
return childLsit;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public List<MobileResource> GetChildListById(List<MobileResource> mobileResources, long resId, bool isContainOneself = true)
|
||||
{
|
||||
//查找下级
|
||||
var childLsit = GetResourceChilden(mobileResources, resId);
|
||||
if (isContainOneself)//如果包含自己
|
||||
{
|
||||
//获取自己的机构信息
|
||||
var self = mobileResources.Where(it => it.Id == resId).FirstOrDefault();
|
||||
if (self != null) childLsit.Insert(0, self);//如果机构不为空就插到第一个
|
||||
}
|
||||
return childLsit;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<MobileResource>> GetListByCategory(string category)
|
||||
{
|
||||
//先从Redis拿
|
||||
var mobileResources = _simpleCacheService.Get<List<MobileResource>>(MobileConst.CACHE_MOBILE_RESOURCE + category);
|
||||
if (mobileResources == null)
|
||||
{
|
||||
//redis没有就去数据库拿
|
||||
mobileResources = await base.GetListAsync(it => it.Category == category);
|
||||
if (mobileResources.Count > 0)
|
||||
{
|
||||
//插入Redis
|
||||
_simpleCacheService.Set(MobileConst.CACHE_MOBILE_RESOURCE + category, mobileResources);
|
||||
}
|
||||
}
|
||||
return mobileResources;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<ResTreeSelector>> ResourceTreeSelector()
|
||||
{
|
||||
var resourceTreeSelectors = new List<ResTreeSelector>();//定义结果
|
||||
//获取模块列表
|
||||
var moduleList = await GetListByCategory(CateGoryConst.RESOURCE_MODULE);
|
||||
//遍历模块
|
||||
foreach (var module in moduleList)
|
||||
{
|
||||
//将实体转换为ResourceTreeSelectorOutPut获取基本信息
|
||||
var resourceTreeSelector = module.Adapt<ResTreeSelector>();
|
||||
resourceTreeSelector.Menu = await GetRoleGrantResourceMenus(module.Id);
|
||||
resourceTreeSelectors.Add(resourceTreeSelector);
|
||||
}
|
||||
return resourceTreeSelectors;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public List<PermissionTreeSelector> PermissionTreeSelector(List<string> routes)
|
||||
{
|
||||
var permissions = new List<PermissionTreeSelector>();//权限列表
|
||||
|
||||
// 获取所有需要数据权限的控制器
|
||||
var controllerTypes =
|
||||
App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(RolePermissionAttribute), false));
|
||||
foreach (var controller in controllerTypes)
|
||||
{
|
||||
//获取数据权限特性
|
||||
var routeAttributes = controller.GetCustomAttributes<RouteAttribute>().ToList();
|
||||
if (routeAttributes == null)
|
||||
continue;
|
||||
var route = routeAttributes[0];//取第一个值
|
||||
var routeName = GetRouteName(controller.Name, route.Template);//赋值路由名称
|
||||
//如果路由包含在路由列表中
|
||||
if (routes.Contains(routeName))
|
||||
{
|
||||
//获取所有方法
|
||||
var menthods = controller.GetMethods();
|
||||
//遍历方法
|
||||
foreach (var menthod in menthods)
|
||||
{
|
||||
//获取忽略数据权限特性
|
||||
var ignoreRolePermission = menthod.GetCustomAttribute<IgnoreRolePermissionAttribute>();
|
||||
if (ignoreRolePermission == null)//如果是空的代表需要数据权限
|
||||
{
|
||||
//获取接口描述
|
||||
var displayName = menthod.GetCustomAttribute<DisplayNameAttribute>();
|
||||
if (displayName != null)
|
||||
{
|
||||
//默认路由名称
|
||||
var apiRoute = StringHelper.FirstCharToLower(menthod.Name);
|
||||
//获取get特性
|
||||
var requestGet = menthod.GetCustomAttribute<HttpGetAttribute>();
|
||||
if (requestGet != null)//如果是get方法
|
||||
apiRoute = requestGet.Template;
|
||||
else
|
||||
{
|
||||
//获取post特性
|
||||
var requestPost = menthod.GetCustomAttribute<HttpPostAttribute>();
|
||||
if (requestPost != null)//如果是post方法
|
||||
apiRoute = requestPost.Template;
|
||||
}
|
||||
apiRoute = route.Template + $"/{apiRoute}";
|
||||
var apiName = displayName.DisplayName;//如果描述不为空则接口名称用描述的名称
|
||||
//合并
|
||||
var permissionName = apiRoute + $"[{apiName}]";
|
||||
//添加到权限列表
|
||||
permissions.Add(new PermissionTreeSelector
|
||||
{
|
||||
ApiName = apiName,
|
||||
ApiRoute = apiRoute,
|
||||
PermissionName = permissionName
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return permissions;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<MobileResource>> GetMenuByMenuIds(List<long> menuIds)
|
||||
{
|
||||
//获取所有菜单
|
||||
var menuList = await GetListByCategory(CateGoryConst.RESOURCE_MENU);
|
||||
//获取菜单信息
|
||||
var menus = menuList.Where(it => menuIds.Contains(it.Id)).ToList();
|
||||
return menus;
|
||||
}
|
||||
|
||||
#region 方法
|
||||
|
||||
/// <summary>
|
||||
/// 获取路由地址名称
|
||||
/// </summary>
|
||||
/// <param name="controllerName">控制器地址</param>
|
||||
/// <param name="template">路由名称</param>
|
||||
/// <returns></returns>
|
||||
private string GetRouteName(string controllerName, string template)
|
||||
{
|
||||
if (!template.StartsWith("/"))
|
||||
template = "/" + template;//如果路由名称不是/开头则加上/防止控制器没写
|
||||
if (template.Contains("[controller]"))
|
||||
{
|
||||
controllerName = controllerName.Replace("Controller", "");//去掉Controller
|
||||
controllerName = StringHelper.FirstCharToLower(controllerName);//转首字母小写写
|
||||
template = template.Replace("[controller]", controllerName);//替换[controller]
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源所有下级
|
||||
/// </summary>
|
||||
/// <param name="resourceList">资源列表</param>
|
||||
/// <param name="parentId">父ID</param>
|
||||
/// <returns></returns>
|
||||
public List<MobileResource> GetResourceChilden(List<MobileResource> resourceList, long parentId)
|
||||
{
|
||||
//找下级资源ID列表
|
||||
var resources = resourceList.Where(it => it.ParentId == parentId).ToList();
|
||||
if (resources.Count > 0)//如果数量大于0
|
||||
{
|
||||
var data = new List<MobileResource>();
|
||||
foreach (var item in resources)//遍历资源
|
||||
{
|
||||
var res = GetResourceChilden(resourceList, item.Id);
|
||||
data.AddRange(res);//添加子节点;
|
||||
data.Add(item);//添加到列表
|
||||
}
|
||||
return data;//返回结果
|
||||
}
|
||||
return new List<MobileResource>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取上级
|
||||
/// </summary>
|
||||
/// <param name="resourceList"></param>
|
||||
/// <param name="parentId"></param>
|
||||
/// <returns></returns>
|
||||
public List<MobileResource> GetResourceParent(List<MobileResource> resourceList, long parentId)
|
||||
{
|
||||
//找上级资源ID列表
|
||||
var resources = resourceList.Where(it => it.Id == parentId).FirstOrDefault();
|
||||
if (resources != null)//如果数量大于0
|
||||
{
|
||||
var data = new List<MobileResource>();
|
||||
var parents = GetResourceParent(resourceList, resources.ParentId.Value);
|
||||
data.AddRange(parents);//添加子节点;
|
||||
data.Add(resources);//添加到列表
|
||||
return data;//返回结果
|
||||
}
|
||||
return new List<MobileResource>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取授权菜单
|
||||
/// </summary>
|
||||
/// <param name="moduleId">模块ID</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<ResTreeSelector.RoleGrantResourceMenu>> GetRoleGrantResourceMenus(long moduleId)
|
||||
{
|
||||
var roleGrantResourceMenus = new List<ResTreeSelector.RoleGrantResourceMenu>();//定义结果
|
||||
var allMenuList = (await GetListByCategory(CateGoryConst.RESOURCE_MENU)).Where(it => it.Module == moduleId).ToList();//获取所有菜单列表
|
||||
var allButtonList = await GetListByCategory(CateGoryConst.RESOURCE_BUTTON);//获取所有按钮列表
|
||||
var parentMenuList = allMenuList.Where(it => it.ParentId == SimpleAdminConst.ZERO).ToList();//获取一级目录
|
||||
|
||||
//遍历一级目录
|
||||
foreach (var parent in parentMenuList)
|
||||
{
|
||||
//如果是目录则去遍历下级
|
||||
if (parent.MenuType == SysResourceConst.CATALOG)
|
||||
{
|
||||
//获取所有下级菜单
|
||||
var menuList = GetChildListById(allMenuList, parent.Id, false);
|
||||
if (menuList.Count > 0)//如果有菜单
|
||||
{
|
||||
//遍历下级菜单
|
||||
foreach (var menu in menuList)
|
||||
{
|
||||
//如果菜单类型是菜单
|
||||
if (menu.MenuType == SysResourceConst.MENU)
|
||||
{
|
||||
//获取菜单下按钮集合并转换成对应实体
|
||||
var buttonList = allButtonList.Where(it => it.ParentId == menu.Id).ToList();
|
||||
var buttons = buttonList.Adapt<List<ResTreeSelector.RoleGrantResourceButton>>();
|
||||
roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
|
||||
{
|
||||
Id = menu.Id,
|
||||
ParentId = parent.Id,
|
||||
ParentName = parent.Title,
|
||||
Module = moduleId,
|
||||
Title = GetRoleGrantResourceMenuTitle(menuList, menu),//菜单名称需要特殊处理因为有二级菜单
|
||||
Button = buttons
|
||||
});
|
||||
}
|
||||
else if (menu.MenuType == SysResourceConst.LINK || menu.MenuType == SysResourceConst.IFRAME)//如果是内链或者外链
|
||||
{
|
||||
//直接加到资源列表
|
||||
roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
|
||||
{
|
||||
Id = menu.Id,
|
||||
ParentId = parent.Id,
|
||||
ParentName = parent.Title,
|
||||
Module = moduleId,
|
||||
Title = menu.Title
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//否则就将自己加到一级目录里面
|
||||
roleGrantResourceMenus.Add(new ResTreeSelector.RoleGrantResourceMenu
|
||||
{
|
||||
Id = parent.Id,
|
||||
ParentId = parent.Id,
|
||||
ParentName = parent.Title,
|
||||
Module = moduleId
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//就将自己加到一级目录里面
|
||||
var roleGrantResourcesButtons = new ResTreeSelector.RoleGrantResourceMenu
|
||||
{
|
||||
Id = parent.Id,
|
||||
ParentId = parent.Id,
|
||||
ParentName = parent.Title,
|
||||
Module = moduleId,
|
||||
Title = parent.Title
|
||||
};
|
||||
//如果菜单类型是菜单
|
||||
if (parent.MenuType == SysResourceConst.MENU)
|
||||
{
|
||||
//获取菜单下按钮集合并转换成对应实体
|
||||
var buttonList = allButtonList.Where(it => it.ParentId == parent.Id).ToList();
|
||||
roleGrantResourcesButtons.Button = buttonList.Adapt<List<ResTreeSelector.RoleGrantResourceButton>>();
|
||||
}
|
||||
roleGrantResourceMenus.Add(roleGrantResourcesButtons);
|
||||
}
|
||||
}
|
||||
return roleGrantResourceMenus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取授权菜单类菜单名称
|
||||
/// </summary>
|
||||
/// <param name="menuList">菜单列表</param>
|
||||
/// <param name="menu">当前菜单</param>
|
||||
/// <returns></returns>
|
||||
private string GetRoleGrantResourceMenuTitle(List<MobileResource> menuList, MobileResource menu)
|
||||
{
|
||||
//查找菜单上级
|
||||
var parentList = GetResourceParent(menuList, menu.ParentId.Value);
|
||||
//如果有父级菜单
|
||||
if (parentList.Count > 0)
|
||||
{
|
||||
var titles = parentList.Select(it => it.Title).ToList();//提取出父级的name
|
||||
var title = string.Join("- ", titles) + $"-{menu.Title}";//根据-分割,转换成字符串并在最后加上菜单的title
|
||||
return title;
|
||||
}
|
||||
else
|
||||
{
|
||||
return menu.Title;//原路返回
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 方法
|
||||
}
|
||||
Reference in New Issue
Block a user