157 lines
5.1 KiB
C#
157 lines
5.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
//using Syc.Basic.Web.Contracts;
|
|
using Syc.Basic.Web.Dtos;
|
|
using Syc.Basic.Web.WMS;
|
|
using Syc.Basic.Web.WMS.Dtos;
|
|
using Syc.Basic.Web.WMS.Dtos.Role;
|
|
using Syc.Basic.Web.WMS.Entitys;
|
|
using Syc.Basic.Web.WMS.IServices;
|
|
using Syc.Basic.Web.WMS.Repository;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reflection.Emit;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
namespace Syc.Basic.Web.Service
|
|
{
|
|
[ApiExplorerSettings(GroupName = "角色服务"), Authorize]
|
|
public class RoleService: ApiService
|
|
{
|
|
private readonly IUserDomainService userDomainService;
|
|
private readonly IRepository<Role> roleRepository;
|
|
private readonly IRepository<Menu> menuRepository;
|
|
private readonly IRoleRepository repository;
|
|
|
|
public RoleService(IUserDomainService userDomainService
|
|
,IRepository<Role> roleRepository
|
|
,IRepository<Menu> menuRepository
|
|
,IRoleRepository repository)
|
|
{
|
|
this.userDomainService = userDomainService;
|
|
this.roleRepository = roleRepository;
|
|
this.menuRepository = menuRepository;
|
|
this.repository = repository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户拥有的角色的所有菜单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<MenuListDto> GetMenus()
|
|
{
|
|
var userDO = await userDomainService.GetUserByAccountAsync(Identity.Account);
|
|
var menuList = (await userDomainService.GetMenus(userDO)).OrderBy(o => o.Order).ToList();
|
|
|
|
// //查出第一级
|
|
List<RouteItem> menuItems = ObjectMapper.Map(menuList, new List<RouteItem>());
|
|
|
|
return new MenuListDto()
|
|
{
|
|
data = menuItems
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 角色列表分页
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<object> GetList()
|
|
{
|
|
var roles = await roleRepository.GetListAsync();
|
|
var result = ObjectMapper.Map(roles, new List<RoleDto>());
|
|
return new
|
|
{
|
|
data = result,
|
|
total = result.Count
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 递归查询所有子级
|
|
/// </summary>
|
|
/// <param name="menu"></param>
|
|
/// <param name="menus"></param>
|
|
/// <returns></returns>
|
|
[NonAction]
|
|
public List<RouteItem> Findchildren(Menu menu,List<Menu> menus)
|
|
{
|
|
if(menu is null)
|
|
return null;
|
|
|
|
var result = new List<RouteItem>();
|
|
var list = menus.Where(e => e.PId == menu.Id).ToList();
|
|
foreach (var item in list)
|
|
{
|
|
var routeItem = ObjectMapper.Map(item, new RouteItem());
|
|
routeItem.children = Findchildren(item, menus);
|
|
result.Add(routeItem);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除角色
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task Del(DelRoleInput delRoleInput)
|
|
{
|
|
if (await CodeExisteAsync(delRoleInput.code,false))
|
|
{
|
|
var role = await repository.GetRoleByCode(delRoleInput.code, false, false);
|
|
await roleRepository.DeleteAsync(role);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编号是否已存在
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[NonAction]
|
|
public async Task<bool> CodeExisteAsync(string code,bool throwEx = true)
|
|
{
|
|
if (await repository.CodeExiste(code))
|
|
{
|
|
if(throwEx)
|
|
throw Oops.Oh($"角色编号 {code} 已存在,请勿重复使用");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增角色
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task Add(UpdateRoleInput updateRoleInput)
|
|
{
|
|
if (!(await CodeExisteAsync(updateRoleInput.code)))
|
|
{
|
|
var defaultMenu = await menuRepository.GetAsync(e => e.Name == "Dashboard");
|
|
var role = ObjectMapper.Map(updateRoleInput, new Role());
|
|
role.Menus.Add(defaultMenu);
|
|
await roleRepository.InsertAsync(role);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑角色
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task Edit(UpdateRoleInput updateRoleInput)
|
|
{
|
|
if (updateRoleInput.code.Equals(ConstPool.AdministratorRoleCode))
|
|
throw Oops.Oh($"超级管理员禁止修改");
|
|
if ((await roleRepository.CountAsync(e => e.Code == updateRoleInput.code) > 1))
|
|
throw Oops.Oh("角色编号重复");
|
|
var role = ObjectMapper.Map(updateRoleInput, new Role());
|
|
await roleRepository.UpdateAsync(role);
|
|
}
|
|
}
|
|
}
|