using DotNetCore.CAP; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Syc.Authorize.JwtBearer; //using Syc.Basic.Web.Contracts; using Syc.Basic.Web.WMS.Dtos; using Syc.Basic.Web.WMS.Dtos.Auth; using Syc.Basic.Web.WMS.Dtos.Role; using Syc.Basic.Web.WMS.Dtos.User; using Syc.Basic.Web.WMS.Entitys; using Syc.Basic.Web.WMS.IServices; using Syc.Basic.Web.WMS.Repository; using Syc.Basic.Web.Services; using Syc.Core.Tools; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Syc.Basic.Web.WMS.Service { //[ApiExplorerSettings(GroupName = "鉴权/授权服务"), Authorize] [AllowAnonymous] public class AuthService : ApiService, IAuthService { private readonly IJwtTokenService jwtTokenService; private readonly IUserDomainService userDomainService; private readonly IRoleRepository roleRepository; private readonly IRepository menuRepository; private readonly ICapPublisher capPublisher; public AuthService( IJwtTokenService jwtTokenService ,IUserDomainService userDomainService , IRoleRepository roleRepository , IRepository menuRepository //, ICapPublisher capPublisher ) { this.jwtTokenService = jwtTokenService; this.userDomainService = userDomainService; this.roleRepository = roleRepository; this.menuRepository = menuRepository; this.capPublisher = capPublisher; } /// /// 账号密码登录 /// /// /// [HttpPost] public async Task LoginAsync(LoginInput input) { var userDO = await userDomainService.GetUserByAccountAsync(input.UserName); if (userDO is null) throw Oops.Oh($"{input.UserName} 用户不存在"); if (!userDO.CheckPassword(Tools.SecurityUtil.MD5Encrypt(input.Password, Encoding.ASCII))) throw Oops.Oh("密码有误,请重试"); if (!userDO.IsEnable) throw Oops.Oh("用户已被冻结,请联系系统管理员"); // var userInfo = ObjectMapper.Map(userDO.User(), new Identity()); var token = jwtTokenService.GenerateToken(userDO); return new AuthResultDto(token); } /// /// 注销 /// /// [AllowAnonymous] [HttpPost] public async Task Logout() { await capPublisher.PublishAsync("mes.test", "测试消息"); } /// /// 获取角色菜单 /// /// public async Task GetRoleMenus([FromQuery] string code) { if (code.IsNullOrWhiteSpace()) throw Oops.Oh("code 必填"); var role = await roleRepository.GetRoleByCode(code, true, false); if (role is null) throw Oops.Oh("角色不存在或已被删除"); return new RoleMenusDto() { code = role.Code, menuIds = role.Menus.Select(e => e.Id).ToArray() }; } /// /// 更新角色菜单 /// /// [HttpPost] public async Task UpdateRoleMenus(UpdateRoleMenusInput input) { if (input.code.Equals(ConstPool.AdministratorRoleCode)) throw Oops.Oh($"超级管理员禁止修改"); var role = await roleRepository.GetRoleByCode(input.code, true, false); if (role.IsNullOrEmpty()) throw Oops.Oh("角色不存在或已被删除"); var menus = await menuRepository.GetListAsync(e => input.menuIds.Contains(e.Id)); role.Menus.Clear(); role.Menus.AddRange(menus); await roleRepository.Update(role); } /// /// 获取当前用户菜单权限 /// /// /// public async Task> GetPerm() { return await userDomainService.GetPermissionByUserId(Identity.Id); } } }