using LifePayment.Application.Contracts;
using LifePayment.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Identity;
using ZeroD.Util;
namespace LifePayment.HttpApi
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class UserRoleController : AbpController
{
private readonly IAccountService _accountService;
private readonly IUserRoleService _userRoleService;
private readonly IIdentityUserAppService _identityUserService;
private readonly IIdentityRoleAppService _identityRoleService;
public UserRoleController(
IAccountService accountService,
IUserRoleService userRoleService,
IIdentityUserAppService identityUserService,
IIdentityRoleAppService identityRoleService)
{
_accountService = accountService;
_userRoleService = userRoleService;
_identityUserService = identityUserService;
_identityRoleService = identityRoleService;
}
///
/// 新增后台管理账户
///
///
///
[HttpPost]
public async Task CreateBackClientUser(CreateBackClientUserInput input)
{
return await _accountService.CreateAccount(ObjectMapper.Map(input), isAdminCreate: true);
}
///
/// 后台管理账户列表
///
///
///
[HttpPost]
public async Task> GetBackClientUsers(GetBackClientUsersInput input)
{
return await _userRoleService.GetBackClientUsers(input);
}
///
/// 后台管理账户编辑
///
///
///
[HttpPost]
public async Task UpdateBackClientUser(UpdateBackClientUserInput input)
{
await _identityUserService.UpdateAsync(input.Id, new IdentityUserUpdateDto
{
Name = input.Name,
PhoneNumber = input.PhoneNumber,
UserName = input.UserName,
RoleNames = input.RoleNames,
});
return await _userRoleService.UpdateBackClientUser(input);
}
///
/// 删除后台管理账户
///
/// 用户Id
///
[HttpGet]
public async Task DeleteBackClientUser(Guid id)
{
await _identityUserService.DeleteAsync(id);
return await _userRoleService.DeleteBackClientUser(id);
}
///
/// 新增角色
///
///
///
[HttpPost]
public async Task CreateRole(CreateBaseRoleInput input)
{
if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
{
input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
}
else
{
input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
}
var res = await _identityRoleService.CreateAsync(new IdentityRoleCreateDto
{
Name = input.Name,
Sequence = input.Sequence,
Note = input.Remark,
});
var newInput = ObjectMapper.Map(input);
newInput.Id = res.Id;
await _userRoleService.CreateRole(newInput);
return res.Id;
}
///
/// 角色列表
///
///
///
[HttpPost]
public async Task> GetRoles(GetRolesInput input)
{
return await _userRoleService.GetRoles(input);
}
///
/// 角色编辑
///
///
///
[HttpPost]
public async Task UpdateRole(CreateOrUpdateRoleInput input)
{
if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
{
input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
}
else
{
input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
}
await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
{
Name = input.Name,
Sequence = input.Sequence,
Note = input.Remark,
});
return await _userRoleService.UpdateRole(input);
}
///
/// 角色启用/禁用
///
///
///
[HttpPost]
public async Task RoleEnableOrForbid(RoleEnableOrForbidInput input)
{
await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
{
IsSetEnable = true,
IsEnable = input.IsEnable,
Name = input.Name,
});
return await _userRoleService.RoleEnableOrForbid(input.Id, input.IsEnable);
}
///
/// 删除角色
///
/// 角色Id
///
[HttpGet]
public async Task DeleteRole(Guid id)
{
await _identityRoleService.DeleteAsync(id);
return await _userRoleService.DeleteRole(id);
}
}
}