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.
 
 

242 lines
8.5 KiB

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Znyc.Cloudcar.Admin.AspNetCore.Controllers;
using Znyc.Cloudcar.Admin.AspNetCore.Entitys;
using Znyc.Cloudcar.Admin.AspNetCore.Mvc;
using Znyc.Cloudcar.Admin.Commons.Entitys;
using Znyc.Cloudcar.Admin.Commons.Log;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IServices;
namespace Znyc.Cloudcar.Admin.WebApi.Controllers
{
/// <summary>
/// 角色权限接口
/// </summary>
[ApiController]
[Route("api/Security/[controller]")]
public class RoleAuthorizeController : AreaApiController<RoleAuthorizeEntity, RoleAuthorizeOutputDto,
RoleAuthorizeInputDto, IRoleAuthorizeService, long>
{
private readonly IMenuService menuService;
private readonly IOrganizeService organizeService;
private readonly IRoleDataService roleDataService;
/// <summary>
/// </summary>
/// <param name="service"></param>
/// <param name="_menuService"></param>
/// <param name="_roleDataService"></param>
/// <param name="_organizeService"></param>
public RoleAuthorizeController(IRoleAuthorizeService service, IMenuService _menuService,
IRoleDataService _roleDataService, IOrganizeService _organizeService) : base(service)
{
_service = service;
menuService = _menuService;
roleDataService = _roleDataService;
organizeService = _organizeService;
}
/// <summary>
/// 新增前处理数据
/// </summary>
/// <param name="info"></param>
protected override void OnBeforeInsert(RoleAuthorizeEntity info)
{
info.Id = 0;
info.CreatedTime = DateTime.Now;
info.CreatedUserId = CurrentUser.UserId;
if (info.SortCode == null)
{
info.SortCode = 99;
}
}
/// <summary>
/// 在更新数据前对数据的修改操作
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected override void OnBeforeUpdate(RoleAuthorizeEntity info)
{
}
/// <summary>
/// 在软删除数据前对数据的修改操作
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected override void OnBeforeSoftDelete(RoleAuthorizeEntity info)
{
}
/// <summary>
/// 角色分配权限树
/// </summary>
/// <param name="roleId"></param>
/// <param name="itemType"></param>
/// <returns></returns>
[HttpGet("GetRoleAuthorizeFunction")]
[FunctionAuthorize("List")]
public async Task<IActionResult> GetRoleAuthorizeFunction(long roleId, string itemType)
{
CommonResult result = new CommonResult();
// roleId = "'" + roleId + "'";
List<long> resultlist = new List<long>();
IEnumerable<RoleAuthorizeEntity> list = _service.GetListRoleAuthorizeByRoleId(roleId.ToString(), itemType);
foreach (RoleAuthorizeEntity info in list)
{
resultlist.Add(info.ItemId);
}
result.ResData = resultlist;
result.ErrCode = ErrCode.successCode;
return ToJsonContent(result);
}
/// <summary>
/// 保存角色权限
/// </summary>
/// <param name="roleinfo">功能权限</param>
/// <returns></returns>
[HttpPost("SaveRoleAuthorize")]
[FunctionAuthorize("List")]
public async Task<IActionResult> SaveRoleAuthorize(RoleAuthorizeDataInputDto roleinfo)
{
CommonResult result = new CommonResult();
try
{
List<RoleAuthorizeEntity> inList = new List<RoleAuthorizeEntity>();
foreach (int item in roleinfo.RoleFunctios)
{
MenuEntity menu = menuService.Get(item);
if (menu != null)
{
RoleAuthorizeEntity info = new RoleAuthorizeEntity
{
ObjectId = roleinfo.RoleId,
ItemType = menu.MenuType == "C" || menu.MenuType == "M" ? 1 : 2,
ObjectType = 1,
ItemId = menu.Id
};
OnBeforeInsert(info);
inList.Add(info);
}
}
List<RoleDataEntity> roleDataList = new List<RoleDataEntity>();
foreach (string item in roleinfo.RoleData)
{
RoleDataEntity info = new RoleDataEntity
{
RoleId = roleinfo.RoleId,
AuthorizeData = item,
DType = "dept"
};
roleDataList.Add(info);
}
foreach (int item in roleinfo.RoleSystem)
{
RoleAuthorizeEntity info = new RoleAuthorizeEntity
{
ObjectId = roleinfo.RoleId,
ItemType = 0,
ObjectType = 1,
ItemId = item
};
OnBeforeInsert(info);
inList.Add(info);
}
result.Success = await _service.SaveRoleAuthorize(roleinfo.RoleId, inList, roleDataList);
if (result.Success)
{
result.ErrCode = ErrCode.successCode;
}
}
catch (Exception ex)
{
result.ErrMsg = ex.Message;
}
return ToJsonContent(result);
}
private List<RoleAuthorizeEntity> SubFunction(List<ModuleFunctionOutputDto> list, int roleId)
{
List<RoleAuthorizeEntity> inList = new List<RoleAuthorizeEntity>();
foreach (ModuleFunctionOutputDto item in list)
{
RoleAuthorizeEntity info = new RoleAuthorizeEntity
{
ObjectId = roleId,
ItemType = 1,
ObjectType = 1,
ItemId = item.Id
};
OnBeforeInsert(info);
inList.Add(info);
inList.Concat(SubFunction(item.Children, roleId));
}
return inList;
}
/// <summary>
/// 获取功能菜单适用于Vue Tree树形
/// </summary>
/// <returns></returns>
[HttpGet("GetAllFunctionTree")]
[FunctionAuthorize("List")]
public async Task<IActionResult> GetAllFunctionTree()
{
CommonResult result = new CommonResult();
try
{
List<ModuleFunctionOutputDto> list = await _service.GetAllFunctionTree();
result.Success = true;
result.ErrCode = ErrCode.successCode;
result.ResData = list;
}
catch (Exception ex)
{
Log4NetHelper.Error("获取菜单异常", ex);
result.ErrMsg = ErrCode.err40110;
result.ErrCode = "40110";
}
return ToJsonContent(result);
}
/// <summary>
/// 获取功能菜单适用于Vue 树形列表
/// </summary>
/// <param name="systemTypeId">子系统Id</param>
/// <returns></returns>
//[HttpGet("GetAllFunctionTreeTable")]
//[FunctionAuthorize("List")]
//public async Task<IActionResult> GetAllFunctionTreeTable(string systemTypeId)
//{
// CommonResult result = new CommonResult();
// try
// {
// List<FunctionTreeTableOutputDto> list = await menuService.GetAllFunctionTreeTable(systemTypeId);
// result.Success = true;
// result.ErrCode = ErrCode.successCode;
// result.ResData = list;
// }
// catch (Exception ex)
// {
// Log4NetHelper.Error("获取菜单异常", ex);
// result.ErrMsg = ErrCode.err40110;
// result.ErrCode = "40110";
// }
// return ToJsonContent(result);
//}
}
}